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

发布时间:2020-11-03 15:22:17 作者:Leah
来源:亿速云 阅读:402

本篇文章为大家展示了使用Java导出ZIP压缩包的实现方法,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

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;

/**
 * @author wx
 * @date 2020/10/29 5:19 下午
 */
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压缩包的实现方法,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. mysql压缩包(.zip)安装
  2. 怎么在Java中向zip压缩包追加文件

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java zip ava

上一篇:IDEA java出现无效的源发行版14怎么解决

下一篇:Python QT组件库qtwidgets的使用方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》