本网站(662p.com)打包出售,且带程序代码数据,662p.com域名,程序内核采用TP框架开发,需要联系扣扣:2360248666 /wx:lianweikj
精品域名一口价出售:1y1m.com(350元) ,6b7b.com(400元) , 5k5j.com(380元) , yayj.com(1800元), jiongzhun.com(1000元) , niuzen.com(2800元) , zennei.com(5000元)
需要联系扣扣:2360248666 /wx:lianweikj
Java中往zip压缩包追加文件
mylove136 · 548浏览 · 发布于2019-12-11 +关注

有个需求,从某个接口下载的一个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));
           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();
           }
       }
   }
}


相关推荐

PHP实现部分字符隐藏

沙雕mars · 1325浏览 · 2019-04-28 09:47:56
Java中ArrayList和LinkedList区别

kenrry1992 · 908浏览 · 2019-05-08 21:14:54
Tomcat 下载及安装配置

manongba · 970浏览 · 2019-05-13 21:03:56
JAVA变量介绍

manongba · 962浏览 · 2019-05-13 21:05:52
什么是SpringBoot

iamitnan · 1086浏览 · 2019-05-14 22:20:36
加载中

0评论

评论
分类专栏
小鸟云服务器
扫码进入手机网页