zip压缩包工具





import java.io.*;
import java.net.URLEncoder;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;

import javax.servlet.http.HttpServletResponse;

public class Zip4jUtils {

    /**
     * @param zipPath    创建zip文件的 文件路径+zip名称
     * @param folderPath 待压缩文件夹或者待压缩文件
     */
    public static void zipFile(String zipPath, String folderPath) throws
            ZipException {

        File file = new File(folderPath);
        final ZipFile zipFile = new ZipFile(zipPath); // 創建zip包,指定了zip路徑和zip名稱
        final ZipParameters parameters = new ZipParameters(); // 设置zip包的一些参数集合
        parameters.setEncryptFiles(false); // 是否设置密码(此处设置为:是)
        parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // 压缩方式(默认值)
        parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); // 普通级别(参数很多)
        parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); // 加密级别

        if (file.isDirectory()) {
            zipFile.createZipFileFromFolder(new File(folderPath), parameters, false, -1L);
        } else {
            zipFile.createZipFile(file, parameters);
        }
    }
    /**
     * @param zipPath    创建zip文件的 文件路径+zip名称
     * @param folderPath 待压缩文件夹或者待压缩文件
     * @param passWord   密碼
     */
    public static void zipFile(String zipPath, String folderPath, String passWord) throws
            ZipException {

        File file = new File(folderPath);
        final ZipFile zipFile = new ZipFile(zipPath); // 創建zip包,指定了zip路徑和zip名稱
        final ZipParameters parameters = new ZipParameters(); // 设置zip包的一些参数集合
        parameters.setEncryptFiles(true); // 是否设置密码(此处设置为:是)
        parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // 压缩方式(默认值)
        parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); // 普通级别(参数很多)
        parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); // 加密级别

        parameters.setPassword(passWord); // 压缩包密码为123456
        if (file.isDirectory()) {
            zipFile.createZipFileFromFolder(new File(folderPath), parameters, false, -1L);
        } else {
            zipFile.createZipFile(file, parameters);
        }
    }

    /**
     * 解压文件
     *
     * @param zipPath    压缩文件路径
     * @param folderPath 解压位置
     * @param passWord   密码
     */
    public static void unZip(String zipPath, String folderPath, String passWord) throws
            ZipException {

        final ZipFile zipFile = new ZipFile(zipPath); // 根据路径取得需要解压的Zip文件
        if (zipFile.isEncrypted()) { // 判断文件是否加码
            zipFile.setPassword(passWord); // 密码为123456
        }
        zipFile.extractAll(folderPath); // 压缩包文件解压路径
    }

    public static HttpServletResponse downloadZip(File file, HttpServletResponse response) {
        if (file.exists() == false) {
            System.out.println("待压缩的文件目录:" + file + "不存在.");
        } else {
            try {
                // 以流的形式下载文件。
                InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
                byte[] buffer = new byte[fis.available()];
                fis.read(buffer);
                fis.close();
                // 清空response
                response.reset();

                OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
                response.setContentType("application/octet-stream");

                //如果输出的是中文名的文件,在此处就要用URLEncoder.encode方法进行处理
                response.setHeader("Content-Disposition", "attachment;filename="
                        + new String(file.getName().getBytes("GB2312"), "ISO8859-1"));
                toClient.write(buffer);
                toClient.flush();
                toClient.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            } finally {
                try {
                    File f = new File(file.getPath());
                    if(f.delete()){
                        System.out.println("文件删除成功");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return response;
    }

    public static HttpServletResponse downloadZip(File file, HttpServletResponse response, String
            userAgent) {
        if (userAgent == null) {
            downloadZip(file, response);
        }
        if (file.exists() == false) {
            System.out.println("待压缩的文件目录:" + file + "不存在.");
        } else {
            try {
                // 以流的形式下载文件。
                InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
                byte[] buffer = new byte[fis.available()];
                fis.read(buffer);
                fis.close();
                // 清空response
                response.reset();

                OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
                if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
                    response.setHeader("Content-Disposition", "attachment;filename="
                            + URLEncoder.encode(file.getName(), "UTF-8"));
                } else {
                    response.setHeader("Content-Disposition",
                            "attachment;filename=" + new String(file.getName().getBytes("UTF-8"),
                                    "iso8859-1"));
                }
                toClient.write(buffer);
                toClient.flush();
                toClient.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            } finally {
                try {
                    File f = new File(file.getPath());
                    if(f.delete()){
                        System.out.println("文件删除成功");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return response;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值