zip格式打包导出工具类

服务器端zip文件生成

打包导出相关配置文件及复用,供下载、导入配置
(配合文件加密使用)

代码如下:

package com.lin.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public class ZipUtil {

    private static final String ZIP_NAME = "要生成的压缩包名字";

    /**
     * 压缩多个文件成一个zip文件
     * @param srcFiles  源文件列表
     * @param zipFile  压缩后的文件zipFiles
     */
    public static void zipFiles(File[] srcFiles, File zipFile) {
        byte[] buffer = new byte[1024];
        try {
            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));
            for (File file:srcFiles) {
                FileInputStream fileInputStream = new FileInputStream(file);
                out.putNextEntry(new ZipEntry(file.getName()));
                int length;
                while ((length = fileInputStream.read(buffer)) > 0) {
                    out.write(buffer, 0, length);
                }
                out.closeEntry();
                fileInputStream.close();
            }
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 解压缩
     * @param srcZipFile  需要解压缩的文件
     * @param desDir  解压后的目标目录
     */
    public static void unZipFile(File srcZipFile, String desDir) {
        try {
            ZipFile zipFile = new ZipFile(srcZipFile);
            for (Enumeration entries = zipFile.entries(); entries.hasMoreElements();) {
                ZipEntry entry = (ZipEntry) entries.nextElement();
                String zipEntryName = entry.getName();
                InputStream in = zipFile.getInputStream(entry);
                OutputStream out = new FileOutputStream(desDir + zipEntryName);
                byte[] buf1 = new byte[1024];
                int len;
                while ((len = in.read(buf1)) > 0) {
                    out.write(buf1, 0, len);
                }
                in.close();
                out.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 调用执行压缩
     * @param filename 压缩包名字
     * @throws IOException
     */
    public static void excuteZip(HttpServletRequest request, HttpServletResponse response, String filename,
                                         String pdfPath) throws IOException {
        String zipPath = request.getSession().getServletContext().getRealPath(ZIP_NAME);
        //创建文件夹
        File folder = new File(zipPath);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        folder.createNewFile();
        String filePath = pdfPath;
        File file = new File(filePath);
        // 获取文件夹下所有文件信息
        File[] srcfile = file.listFiles();
        // 压缩包存放位置
        File zipfile = new File(zipPath + "/" + filename + ".zip");
        // 调用压缩方法
        ZipUtil.zipFiles(srcfile, zipfile);

        // 删除生成的文件夹和文件
        String downPath = zipPath + "/" + filename + ".zip";
        ZipUtil.download(downPath, response);
        //删除源文件夹及其内容
        ZipUtil.deleteAll(file);
        //删除下载完的压缩包存储临时文件夹及其内容
        File zfile = new File(zipPath);
        ZipUtil.deleteAll(zfile);
    }

    /**
     * 下载服务器下生成的文件
     * @param path
     * @param response
     */
    public static void download(String path, HttpServletResponse response) {
        InputStream in = null;
        OutputStream out = null;
        try {
            File file = new File(path);
            /* 读取要下载的文件,保存到文件输入流 */
            in = new FileInputStream(file);
            /* 设置响应头,控制浏览器下载该文件 */
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/octet-stream");
            response.setHeader("Content-disposition",
                    "attachment; filename=" + new String(file.getName().getBytes("UTF-8"), "ISO8859-1"));
            out = new BufferedOutputStream(response.getOutputStream());
            byte buffer[] = new byte[2048];
            int len = 0;
            while ((len = in.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }
            out.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 删除文件夹及其下面的所有文件
     * @param file
     */
    public static void deleteAll(File file) {
        if (file.isFile() || file.list().length == 0) {
            file.delete();
        } else {
            for (File f : file.listFiles()) {
                // 递归删除文件夹下的每一个文件
                deleteAll(f);
            }
            // 删除文件夹
            file.delete();
        }
    }

    /**
     * 按路径删除文件夹及其下面的所有文件
     * @param path
     */
    public static void deleteAll(String path) {
        deleteAll(new File(path));
    }
}
发布了8 篇原创文章 · 获赞 6 · 访问量 2607

猜你喜欢

转载自blog.csdn.net/linhao_obj/article/details/89949321
今日推荐