JAVA中的文件解压和压缩

public class DeCompressUtils {

/**
     * 压缩zip文件
     *
     * @param zipFilePath
     *            压缩后zip包路径
     * @param lstFilesPath
     *            所要压缩的文件list
     * @throws Exception
     */
    public static void zip(String zipFilePath, List<String> lstFilesPath) throws Exception {
        // 判断是否为zip文件
        if (zipFilePath.length() <= 4 || !zipFilePath.toLowerCase().endsWith(".zip")) {
            Exception ex = new Exception("zip文件格式错误!");
            throw ex;
        }

        // 压缩处理
        ZipFile zf = null;
        try {
            zf = new ZipFile(zipFilePath);
            ArrayList<File> lstFiles = new ArrayList<File>();
            for (String strFilePath : lstFilesPath) {
                lstFiles.add(new File(strFilePath));
            }

            ZipParameters parameters = new ZipParameters();
            // 默认,对应好压的标准
            parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
            // 压缩级别
            parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
            zf.addFiles(lstFiles, parameters);
        } catch (ZipException e) {
            e.printStackTrace();
            Exception ex = new Exception("文件压缩zip时失败!");
            throw ex;
        }

    }

    /**
     * 解压zip文件
     *
     * @param zipFilePath
     *            zip文件路径
     * @param folderPath
     *            解压后文件路径
     * @throws Exception
     */
    public static void unZip(String zipFilePath, String folderPath) throws Exception {
        // 判断是否为zip文件  注意:folderPath传值为dealPath + File.separator
        if (zipFilePath.length() <= 4 || !zipFilePath.toLowerCase().endsWith(".zip")) {
            Exception ex = new Exception(zipFilePath + ":zip文件格式错误!");
            throw ex;
        }

        // 解压处理
        ZipFile zf = null;
        try {
            zf = new ZipFile(zipFilePath);
            zf.extractAll(folderPath);
        } catch (ZipException e) {
            e.printStackTrace();
            Exception ex = new Exception(zipFilePath + ":zip文件解压时失败!");
            throw ex;
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值