gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:13:38 26 4
gpt4 key购买 nike

我想创建一个 zip 来存储两个具有相同名称的不同文件,但由于

我无法(使用 java.util.zip.ZipOutputStream)

java.util.zip.ZipException: duplicate entry:

异常。我知道这是可能的,但我需要建议我可以为此目的使用哪个库。谢谢!

更新我正在使用的代码:

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.class.getDeclaredField("names");
namesField.setAccessible(true);
HashSet<String> names = (HashSet<String>) namesField.get(out);

并在每次 putNextEntry 调用后清除 names

关于Java - 创建一个包含重复条目的 ZIP 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39958486/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com