Java实现合并zip压缩文件


  背景:

  最近在做的一个项目,需要用到将多个Zip压缩文件合并成一个新的Zip压缩文件,如果多个压缩文件里面有相同文件则只合并第一个源压缩包里面的文件,后面若有相同的文件则跳过执行合并。

  代码实现:

  

 1 /**
 2      * 合并压缩文件
 3      * @param tartetZipFile
 4      * @param sourceZipFiles
 5      * @return
 6      */
 7     public static boolean Marge(String tartetZipFile,String... sourceZipFiles) {
 8         boolean flag = true;
 9         ZipOutputStream out = null;
10         List<ZipInputStream> ins = new ArrayList<ZipInputStream>();
11         try{
12             out = new ZipOutputStream(new FileOutputStream(tartetZipFile));
13             HashSet<String> names = new HashSet<String>();
14             for(String sourceZipFile : sourceZipFiles){
15                 
16                 ZipFile zipFile = new ZipFile(sourceZipFile,Charset.forName("GBK"));
17                 ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(sourceZipFile));
18                 ins.add(zipInputStream);
19                 ZipEntry ze;
20                 Enumeration<? extends ZipEntry> enumeration = zipFile.entries();
21                 while(enumeration.hasMoreElements()){
22                     ze = enumeration.nextElement();
23                     if(ze.isDirectory()){
24                         
25                     }else {
26                         if(names.contains(ze.getName())){
27                             
28                             continue;
29                         }
30                         
31                         ZipEntry oze = new ZipEntry(ze.getName());
32                         out.putNextEntry(oze);
33                         if(ze.getSize()>0){
34                             DataInputStream dis = new DataInputStream(zipFile.getInputStream(ze));
35                             int len = 0;
36                             byte[] bytes = new byte[1024];
37                             while((len = dis.read(bytes))>0){
38                                 out.write(bytes,0,len);
39                             }
40                             out.closeEntry();
41                             out.flush();
42                         }
43                         names.add(oze.getName());
44                     }
45                     
46                 }
47                 zipInputStream.closeEntry();
48                 flag = true;
49             }
50         }catch(Exception ex){
51             ex.printStackTrace();
52             flag = false;
53         }
54         finally {
55             try{
56                 if(out != null){
57                     out.close();
58                 }
59             }catch(Exception ex){
60                 ex.printStackTrace();
61             }
62             try{
63                 for(ZipInputStream in : ins){
64                     if(in != null){
65                         in.close();
66                     }
67                 }
68             }catch(IOException ex){
69                 ex.printStackTrace();
70             }
71         }
72         return flag;
73     }
View Code

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM