2016-10-10 135 views
1

我想創建存儲具有相同名稱的兩個不同的文件的zip ZIP文件,但我無法(使用java.util.zip.ZipOutputStream)由於爪哇 - 創建重複的條目

的java.util .zip.ZipException:重複條目:

異常。我知道這是可能的,但我需要一個建議,我可以用於這個目的的圖書館。謝謝!

UPD我正在使用的代碼:

File zipFile = new File("C:\\Users\\user\\Desktop\\old.zip"); 
File outFile = new File("C:\\Users\\user\\Desktop\\new.zip"); 
if(!outFile.exists()) { 
    outFile.getParentFile().mkdirs(); 
    outFile.createNewFile(); 
} 

byte[] buf = new byte[1024]; 
ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFile)); 
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFile)); 

ZipEntry entry = zin.getNextEntry(); 
while (entry != null) { 
    String name = entry.getName(); 
    out.putNextEntry(new ZipEntry(name)); 
    int len; 
    while ((len = zin.read(buf)) > 0) { 
     out.write(buf, 0, len); 
    } 
    entry = zin.getNextEntry(); 

    if("file".equals(name)) { 
     File fakeFile = new File("C:\\Users\\user\\Desktop\\file"); 
     InputStream in = new FileInputStream(fakeFile); 
     out.putNextEntry(new ZipEntry("file")); 
     while ((len = in.read(buf)) > 0) { 
      out.write(buf, 0, len); 
     } 
     out.closeEntry(); 
     in.close(); 
    } 
} 
zin.close(); 
out.close(); 
+0

請包括[ MCVE](http://stackoverflow.com/help/mcve)在您的文章 – Vampire

+0

我不認爲它可能與當前的Oracle JVM實現。爲什麼你需要這樣做? – JIV

+0

@吸血鬼,加了 – Ivan

回答

0

我能夠繞過限制直通反射API:

Field namesField = ZipOutputStream.getDeclaredField("names"); 
namesField.setAccessible(true); 
HashSet<String> names = (HashSet<String>) namesField.get(out); 

而且每個putNextEntry通話後結算names