Java-创建包含重复条目的ZIP文件

Java-创建包含重复条目的ZIP文件,java,zip,Java,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\\u

我想创建一个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();

我能够通过反射api绕过限制:

Field namesField = ZipOutputStream.getDeclaredField("names");
namesField.setAccessible(true);
HashSet<String> names = (HashSet<String>) namesField.get(out);
fieldnamesfield=ZipOutputStream.getDeclaredField(“名称”);
namesField.setAccessible(true);
HashSet names=(HashSet)namesField.get(out);
并在每次调用后清除
名称
调用

尝试(FileOutputStream fos=newfileoutputstream(zipFile);
try (FileOutputStream fos = new FileOutputStream(zipFile);
            ZipOutputStream zos = new ZipOutputStream(fos)) {

        HashSet<String> names = new HashSet<String>();
        for (String filePath : fileList) {
            if(names.add(filePath))
            {
            String name = filePath.substring(directory.getAbsolutePath()
                    .length() + 1, filePath.length());
            ZipEntry zipEntry = new ZipEntry(name);

            zos.putNextEntry(zipEntry);
            try (FileInputStream fis = new FileInputStream(filePath)) {
                byte[] buffer = new byte[1024];
                int length;
                while ((length = fis.read(buffer)) >= 0) {
                    zos.write(buffer, 0, length);
                }

            } catch (Exception e) {
                e.printStackTrace();
                throw new Exception();
            }
            zos.closeEntry();
            }

        }
        names.clear();
    } catch (IOException e) {
        e.printStackTrace();
        throw new Exception();
    }
ZipoutStream zos=新ZipoutStream(fos)){ HashSet name=新的HashSet(); for(字符串文件路径:文件列表){ if(names.add(filePath)) { String name=filePath.substring(directory.getAbsolutePath() .length()+1,filePath.length()); ZipEntry ZipEntry=新ZipEntry(名称); 佐斯·普特内森特里(齐彭特里); try(FileInputStream fis=newfileinputstream(filePath)){ 字节[]缓冲区=新字节[1024]; 整数长度; 而((长度=fis.read(缓冲区))>=0){ 写入(缓冲区,0,长度); } }捕获(例外e){ e、 printStackTrace(); 抛出新异常(); } zos.closeEntry(); } } name.clear(); }捕获(IOE异常){ e、 printStackTrace(); 抛出新异常(); }

通过使用HashSet解决了我的zip重复条目问题

请在帖子中包含一个,我认为当前的Oracle JVM实现不可能。“你为什么要这么做?”吸血鬼补充道,“你知道这是可能的,但你刚刚证明了这是不可能的。”。下定决心。@JIV,要绕过签名限制(签名检查器验证第一个实体,但安装的是最后一个实体),您一定很想保存这些重复条目,以了解这种技术!(另外,谢谢!非常方便!)