当前位置: 移动技术网 > IT编程>开发语言>Java > Java将文件夹保留目录打包为 ZIP 压缩包并下载的教程详解

Java将文件夹保留目录打包为 ZIP 压缩包并下载的教程详解

2020年08月14日  | 移动技术网IT编程  | 我要评论
上周做了一个需求,要求将数据库保存的 html 界面取出后将服务器下的css和js文件一起打包压缩为zip文件,返回给前台;在数据库中保存的是html标签,查出后,我把这些内容写入css和js等其他文

上周做了一个需求,要求将数据库保存的 html 界面取出后将服务器下的css和js文件一起打包压缩为zip文件,返回给前台;在数据库中保存的是html标签,查出后,我把这些内容写入css和js等其他文件所在目录的一个文件内,然后将这整个文件夹压缩打包下载,解决过程中遇到了下载出来后并没有保存层级目录,在查了好久方法后完成了如下版本,已经可以正常下载并保留层级目录。

//zip文件包压缩下载
 @override
 public void downloadzip(string id,httpservletresponse response) {
 		string zippath = "你的路径";
 file file = new file(zippath,"");//创建指定目录和文件名称的文件对象
 bufferedwriter bw = null;//创建缓冲流
 try {
 	//校验文件目录是否存在,文件是否存在
  chenkfile(file,zippath);
  //这一步是我将指定内容从数据库写入文件
  moduleinfo moduleinfo = moduledao.getbymodid(id);
  
  bw = new bufferedwriter(new filewriter(file));
  //把内容写入临时文件中
  bw.write(moduleinfo.getcontent());
  //此处不能删除,要关闭一次 不关闭无法写入内容 导致压缩包内文件无内容
  bw.flush();
  bw.close();
  //将目标文件压缩为zip并下载
  ziputil.zip(zippath,response);
  //删除文件(防止下一次压缩时有重复文件名)
  file.delete();
 } catch (exception e) {
  log.error("html压缩"+e.getmessage(),e);
 }finally {
 	//这是我写的io流关闭工具类 如需要可以看我关于io流关闭的文章
  iocloseutils.ioclose(bw);
 }
 }

 //判断文件目录和文件是否存在 如否则新建
 public void chenkfile(file file,string path){
 try {
  if (file.exists()){//如果目录存在
  if (!file.isdirectory()){//如果文件不存在
   file.createnewfile();//创建文件
  }
  }else {//如果目录不存在
  file file1 = new file(path);//创建指定目录文件对象
  file1.mkdirs();//创建目录
  file.createnewfile();//创建文件
  }
 } catch (ioexception e) {
  log.error(e.getmessage(),e);
 }
 }
 public static void zip(string sourcefilename, httpservletresponse response){
 zipoutputstream out = null;
 bufferedoutputstream bos = null;
 try {
  //将zip以流的形式输出到前台
  response.setheader("content-type", "application/octet-stream");
  response.setcharacterencoding("utf-8");
  // 设置浏览器响应头对应的content-disposition
  //参数中 testzip 为压缩包文件名,尾部的.zip 为文件后缀
  response.setheader("content-disposition",
   "attachment;filename=" + new string("testzip".getbytes("gbk"), "iso8859-1")+".zip");
  //创建zip输出流
  out = new zipoutputstream(response.getoutputstream());
  //创建缓冲输出流
  bos = new bufferedoutputstream(out);
  file sourcefile = new file(sourcefilename);
  //调用压缩函数
  compress(out, bos, sourcefile, sourcefile.getname());
  out.flush();
  log.info("压缩完成");
 } catch (exception e) {
  log.error("zip压缩异常:"+e.getmessage(),e);
 } finally {
  iocloseutils.ioclose(bos,out);
 }
 }

 public static void compress(zipoutputstream out, bufferedoutputstream bos, file sourcefile, string base){
 fileinputstream fos = null;
 bufferedinputstream bis = null;
 try {
  //如果路径为目录(文件夹)
  if (sourcefile.isdirectory()) {
  //取出文件夹中的文件(或子文件夹)
  file[] flist = sourcefile.listfiles();
  if (flist.length == 0) {//如果文件夹为空,则只需在目的地zip文件中写入一个目录进入点
   out.putnextentry(new zipentry(base + "/"));
  } else {//如果文件夹不为空,则递归调用compress,文件夹中的每一个文件(或文件夹)进行压缩
   for (int i = 0; i < flist.length; i++) {
   compress(out, bos, flist[i], base + "/" + flist[i].getname());
   }
  }
  } else {//如果不是目录(文件夹),即为文件,则先写入目录进入点,之后将文件写入zip文件中
  out.putnextentry(new zipentry(base));
  fos = new fileinputstream(sourcefile);
  bis = new bufferedinputstream(fos);

  int tag;
  //将源文件写入到zip文件中
  while ((tag = bis.read()) != -1) {
   out.write(tag);
  }

  bis.close();
  fos.close();
  }
 } catch (exception e) {
  e.printstacktrace();
 } finally {
  iocloseutils.ioclose(bis,fos);
 }
 }

如图下载后如图所示,名为 testzip.zip 的文件 层级目录都有保留

在这里插入图片描述

总结

到此这篇关于java将文件夹保留目录打包为 zip 压缩包并下载的教程详解的文章就介绍到这了,更多相关java将文件打包为zip压缩包下载内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网