zip文件压缩工具类

zip文件压缩工具类 

支持文件夹和文件压缩

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public class ZipUtil {

    /**
     * 压缩文件/文件夹
     *
     */
    public static void compress(String srcFilePath, String destFilePath) {
        File src = new File(srcFilePath);
        if (!src.exists()) {
            throw new RuntimeException(srcFilePath + "不存在");
        }
        File zipFile = new File(destFilePath);
        FileOutputStream fos = null;
        CheckedOutputStream cos = null;
        ZipOutputStream zos = null;
        String baseDir = "";
        try {
            fos = new FileOutputStream(zipFile);
            cos = new CheckedOutputStream(fos, new CRC32());
            zos = new ZipOutputStream(cos);
            compressbyType(src, zos, baseDir);
        } catch (Exception e) {
            log.error("ZipUtil::compress error",e);
        } finally {
            try {
                if (null != zos) {
                    zos.close();
                }
                if (null != cos) {
                    cos.close();
                }
                if (null != fos) {
                    fos.close();
                }
            } catch (Exception e) {
                log.error("ZipUtil::compress close error",e);
            }
        }
    }

    /**
     * 根据类型压缩文件或文件夹
     */
    private static void compressbyType(File src, ZipOutputStream zos, String baseDir) {
        if (!src.exists()) {
            return;
        }
        if (src.isFile()) {
            compressFile(src, zos, baseDir);
        } else if (src.isDirectory()) {
            compressDir(src, zos, baseDir);
        }
    }


    /**
     * 压缩文件
     */
    private static void compressFile(File file, ZipOutputStream zos, String baseDir) {
        if (!file.exists()) {
            return;
        }
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(file));
            ZipEntry entry = new ZipEntry(baseDir + file.getName());
            zos.putNextEntry(entry);
            int count;
            byte[] buf = new byte[8019];
            while ((count = bis.read(buf)) != -1) {
                zos.write(buf, 0, count);
            }
        } catch (Exception e) {
            log.error("ZipUtil::compressFile error",e);
        } finally {
            try {
                if (null != bis) {
                    bis.close();
                }
            } catch (Exception e) {
                log.error("ZipUtil::compressFile close error",e);
            }
        }
    }

    /**
     * 压缩文件夹
     *
     */
    private static void compressDir(File dir, ZipOutputStream zos, String baseDir) {
        if (!dir.exists()) {
            return;
        }
        File[] files = dir.listFiles();
        if(files.length == 0){
            try {
                zos.putNextEntry(new ZipEntry(baseDir + dir.getName() + File.separator));
            } catch (Exception e) {
                log.error("ZipUtil::compressDir error",e);
            }
        }
        for (File file : files) {
            compressbyType(file, zos, baseDir + dir.getName() + File.separator);
        }
    }


    /**
     * 解压文件/文件夹
     */
    public static boolean decompress(String srcPath, String dest) throws Exception {
        File file = new File(srcPath);
        if (!file.exists()) {
            throw new RuntimeException(srcPath + "所指文件不存在");
        }
        ZipFile zf = null;
        ZipEntry entry = null;
        InputStream is = null;
        FileOutputStream fos = null;
        try {
            zf = new ZipFile(file);
            Enumeration entries = zf.entries();
            while (entries.hasMoreElements()) {
                entry = (ZipEntry) entries.nextElement();
                if (entry.isDirectory()) {
                    String dirPath = dest + File.separator + entry.getName();
                    File dir = new File(dirPath);
                    dir.mkdirs();
                } else {
                    // 表示文件
                    File f = new File(dest + File.separator + entry.getName());
                    if (!f.exists()) {
                        String dirs = f.getParent();
                        File parentDir = new File(dirs);
                        parentDir.mkdirs();
                    }
                    f.createNewFile();
                    // 将压缩文件内容写入到这个文件中
                    is = zf.getInputStream(entry);
                    fos = new FileOutputStream(f);
                    int count;
                    byte[] buf = new byte[8192];
                    while ((count = is.read(buf)) != -1) {
                        fos.write(buf, 0, count);
                    }
                    fos.close();
                    is.close();
                }
            }
        } catch (Exception e) {
            log.error("ZipUtil::decompress error",e);
            return false;
        } finally {
            if (null != fos) {
                fos.close();
            }
            if (null != is) {
                is.close();
            }
            if (null != zf) {
                zf.close();
            }
        }
        return true;
    }

}

测试使用

public static void main(String[] args) {
    String srcFilePath = "D:\\测试.doc";
    String destFilePath= "D:\\测试.zip";;
    ZipUtil.compress(srcFilePath,destFilePath);
}
  • 20
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值