Java ZipUtils工具类

public abstract class ZipUtils {

    /**
     * 压缩成ZIP 方法1
     *
     * @param src 压缩文件夹路径
     * @param out 压缩文件输出流
     */

    public static void toZip(String src, String out) {

        try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(out))) {
            File file = new File(src);
            compress(file, zos, file.getName());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 压缩成ZIP 方法1
     *
     * @param src 压缩文件夹路径
     * @param out 压缩文件输出流
     */

    public static void toZip(String src, OutputStream out) {

        try (ZipOutputStream zos = new ZipOutputStream(out)) {
            File file = new File(src);
            compress(file, zos, file.getName());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 压缩成ZIP 方法2
     *
     * @param files 需要压缩的文件列表
     * @param out   压缩文件输出流
     */

    public static void toZip(List<File> files, OutputStream out) {

        try (ZipOutputStream zos = new ZipOutputStream(out)) {
            for (File srcFile : files) {
                zos.putNextEntry(new ZipEntry(srcFile.getName()));
                FileInputStream in = new FileInputStream(srcFile);
                copy(in, zos);
                zos.closeEntry();
                in.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    /**
     * 递归压缩方法
     *
     * @param file 源文件
     * @param zos  zip输出流
     * @param name 压缩后的名称
     */

    private static void compress(File file, ZipOutputStream zos, String name) throws Exception {

        if (file.isFile()) {
            zos.putNextEntry(new ZipEntry(name));
            FileInputStream in = new FileInputStream(file);
            copy(in, zos);
            zos.closeEntry();
            in.close();
        } else {
            File[] listFiles = file.listFiles();
            if (listFiles == null || listFiles.length == 0) {
                zos.putNextEntry(new ZipEntry(name + File.separator));
                zos.closeEntry();
            } else {
                for (File target : listFiles) {
                    compress(target, zos, name + File.separator + target.getName());
                }
            }
        }
    }

    private static boolean copy(InputStream in, OutputStream out, int size) {

        BufferedInputStream bis = new BufferedInputStream(in);
        byte[] buf = new byte[size];

        try {
            while (bis.read(buf) != -1) {
                out.write(buf);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return true;
    }

    private static boolean copy(InputStream in, OutputStream out) {
        return copy(in, out, 8192);
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值