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