温馨提示×

温馨提示×

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

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

怎么在Java中向zip压缩包追加文件

发布时间:2021-06-11 16:15:42 来源:亿速云 阅读:400 作者:Leah 栏目:编程语言

怎么在Java中向zip压缩包追加文件,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

1、一个zip文件的压缩和解压工具类

pom.xml加入依赖包,如下:

  <dependency>
   <groupId>org.apache.ant</groupId>
   <artifactId>ant</artifactId>
   <version>1.10.7</version>
  </dependency>

工具类代码:

package com.example.demo;

import java.io.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipException;

import org.apache.tools.zip.*;

public class ZipUtil {

 private static int BUFFERSIZE = 1024;

 /**
  * 压缩
  *
  * @param paths
  * @param fileName
  */
 public static void zip(List<String> paths, String fileName) {
  ZipOutputStream zos = null;
  try {
   zos = new ZipOutputStream(new FileOutputStream(fileName));
   for (String filePath : paths) {
    // 递归压缩文件
    File file = new File(filePath);
    String relativePath = file.getName();
    if (file.isDirectory()) {
     relativePath += File.separator;
    }
    zipFile(file, relativePath, zos);
   }
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   try {
    if (zos != null) {
     zos.close();
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }

 public static void zipFile(File file, String relativePath, ZipOutputStream zos) {
  InputStream is = null;
  try {
   if (!file.isDirectory()) {
    ZipEntry zp = new ZipEntry(relativePath);
    zos.putNextEntry(zp);
    is = new FileInputStream(file);
    byte[] buffer = new byte[BUFFERSIZE];
    int length = 0;
    while ((length = is.read(buffer)) >= 0) {
     zos.write(buffer, 0, length);
    }
    zos.setEncoding("gbk");//解决文件名中文乱码
    zos.flush();
    zos.closeEntry();
   } else {
    String tempPath = null;
    for (File f : file.listFiles()) {
     tempPath = relativePath + f.getName();
     if (f.isDirectory()) {
      tempPath += File.separator;
     }
     zipFile(f, tempPath, zos);
    }
   }
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   try {
    if (is != null) {
     is.close();
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }

 /**
  * 解压缩
  *
  * @param fileName
  * @param path
  */
 public static List<String> unzip(String fileName, String path) {
  FileOutputStream fos = null;
  InputStream is = null;
  List<String> filePaths = new ArrayList<String>();
  try {
   ZipFile zf = new ZipFile(new File(fileName));
   Enumeration en = zf.getEntries();
   while (en.hasMoreElements()) {
    ZipEntry zn = (ZipEntry) en.nextElement();
    if (!zn.isDirectory()) {
     is = zf.getInputStream(zn);
     File f = new File(path + zn.getName());
     File file = f.getParentFile();
     file.mkdirs();
     fos = new FileOutputStream(path + zn.getName());
     int len = 0;
     byte bufer[] = new byte[BUFFERSIZE];
     while (-1 != (len = is.read(bufer))) {
      fos.write(bufer, 0, len);
     }
     fos.close();
     filePaths.add(path + zn.getName());
    }
   }
  } catch (ZipException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   try {
    if (null != is) {
     is.close();
    }
    if (null != fos) {
     fos.close();
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  return filePaths;
 }
 }

2、测试

有如下目录结构:

D:\测试\文档.zip

D:\测试\说明.pdf

把“说明.pdf”添加到“文档.zip”里面,生成一个新压缩包“文档(新).zip”。

package com.example.demo;

import java.io.File;
import java.util.List;

public class ZipUtilTest {
 public static void main(String[] args) {
  //解压
  List<String> files = ZipUtil.unzip("D:/测试/文档.zip", "D:/测试/");
  //集合添加文件
  files.add("D:/测试/说明.pdf");
  //压缩
  ZipUtil.zip(files,"D:/测试/文档(新).zip");
  //保留说明.pdf
  files.remove(files.size()-1);
  //删除上面解压出来的文件
  for(String f : files){
   File file = new File(f);
   if(file.exists()){
    file.delete();
   }
  }
 }
}

看完上述内容,你们掌握怎么在Java中向zip压缩包追加文件的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

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

AI