有个需求,从某个接口下载的一个zip压缩包,往里面添加一个说明文件。搜索了一下,没有找到往zip直接添加文件的方法,最终解决方法是先解压、再压缩。
具体过程如下:
1、一个zip文件的压缩和解压工具类
压缩和解压工具类来自https://www.iteye.com/blog/songfeng-123-2243016,但是原文代码因为用的是Java自带的java.util.zip,有中文乱码的bug,所以需要修改部分代码,并且修改为引用org.apache.tools.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), "UTF-8"); 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(); } } } }