我正在尝试获取 Zip 存档中的特定文件,将其解压缩、加密,然后将其放回存档中以替换原始文件。
这是我到目前为止所尝试过的..
public static boolean encryptXML(File ZipArchive, String key) throws ZipException, IOException, Exception {
ZipFile zipFile = new ZipFile(ZipArchive);
List<FileHeader> fileHeaderList = zipFile.getFileHeaders();
for (FileHeader fh : fileHeaderList)
{
if (fh.getFileName().equals("META-INF/file.xml"))
{
Path tempdir = Files.createTempDirectory("Temp");
zipFile.extractFile(fh, tempdir.toString());
File XMLFile = new File(tempdir.toFile(), fh.getFileName());
// Encrypting XMLFile, Ignore this part
// Here, Replace the original XMLFile inside ZipArchive with the encrypted one <<<<<<<<
return true;
}
}
return false;
}
我被困在代码的替换部分,无论如何我可以做到这一点而不必提取整个 Zip 存档吗?
感谢任何帮助,提前致谢。
最佳答案
不确定这是否会对您有帮助,因为您使用的是不同的库,但 ZT Zip 中的解决方案将是以下内容。
ZipUtil.unpackEntry(new File("/tmp/demo.zip"), "foo.txt", new File("foo.txt"));
// encrypt the foo.txt
ZipUtil.replaceEntry(new File("/tmp/demo.zip"), "foo.txt", new File("foo.txt"));
这将解压 foo.txt
文件,然后在对其进行加密后,您可以用新条目替换之前的条目。
关于java - 替换 Zip 存档中的特定文件,而不用 Java 提取整个存档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29941469/