栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java实现导出ZIP压缩包的方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java实现导出ZIP压缩包的方法

最近接触到一个需求要求压缩导出文件,于是乎便要致力于研究一下工具类啦,故也诞生了此篇文章。
下面代码中,溪源也将import导入的依赖也贴出来了,避免大家引入错误。

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;


public class FileZipUtil {

  private static void handlerFile(ZipOutputStream zip, File file, String dir) throws Exception {
    //如果当前的是文件夹,则进行进一步处理
    if (file.isDirectory()) {
      //得到文件列表信息
      File[] fileArray = file.listFiles();
      if (fileArray == null) {
 return;
      }
      //将文件夹添加到下一级打包目录
      zip.putNextEntry(new ZipEntry(dir + "/"));
      dir = dir.length() == 0 ? "" : dir + "/";
      //递归将文件夹中的文件打包
      for (File f : fileArray) {
 handlerFile(zip, f, dir + f.getName());
      }
    } else {
      //当前的是文件,打包处理
      //文件输入流
      BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
      ZipEntry entry = new ZipEntry(dir);
      zip.putNextEntry(entry);
      zip.write(FileUtils.readFileToByteArray(file));
      IOUtils.closeQuietly(bis);
      zip.flush();
      zip.closeEntry();
    }
  }

  private static byte[] createZip(String sourceFilePath) throws Exception{
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ZipOutputStream zip = new ZipOutputStream(outputStream);
    //将目标文件打包成zip导出
    File file = new File(sourceFilePath);
    handlerFile(zip, file,"");
    IOUtils.closeQuietly(zip);
    return outputStream.toByteArray();
  }


  public static void exportZip(HttpServletResponse response, String sourceFilePath) {
  //文件名以时间戳作为前缀
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    String filePrefix = sdf.format(new Date());
    String downloadName = filePrefix + ".zip";
    //将文件进行打包下载
    try {
      OutputStream out = response.getOutputStream();
      //接收压缩包字节
      byte[] data = createZip(sourceFilePath);
      response.reset();
      response.addHeader("Access-Control-Allow-Origin", "*");
      response.setHeader("Access-Control-Expose-Headers", "*");
      response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + downloadName);
      response.addHeader("Content-Length", "" + data.length);
      response.setContentType("application/octet-stream;charset=UTF-8");
      IOUtils.write(data, out);
      out.flush();
      out.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

客户端调用方法:

 @GetMapping("/exportFile")
  public Result exportFile(HttpServletResponse response) {
    //第二个参数为:要压缩文件的地址
    FileZipUtil.exportZip(response, "/Users/Downloads");
  }

到此这篇关于Java实现导出ZIP压缩包的方法的文章就介绍到这了,更多相关Java导出ZIP压缩包内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/130719.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号