[英]Replacing files in multilevel zip folders
我的要求是替換zip文件中的幾個文件。
該zip文件又包含多個zip文件和文件夾。 它最多可以壓縮4個或更多級別的zip文件。
我在不同的源目錄中有一組文件。 我想復制這些文件並在zip文件中替換,方法是將源目錄中的文件名與zip文件中的文件名進行匹配。
有人可以幫忙嗎?
謝謝,Deleep
一種方法是使用臨時文件存儲ZIP並將其作為FileSystems打開,因此您無需提取所有文件並重新壓縮所有內容
FileSystem firstZip = FileSystems.newFileSystem(URI.create("jar:file:/root/firstZip.zip"),
new HashMap<>(), null);
//Get a zip inside the first one
Path path = firstZip.getPath("FOLDER/ZIP_NAME.zip");
//Get the second zip input stream and store the zip in a temp file
InputStream in = firstZip.provider().newInputStream(path);
Path secondPath = Paths.get("/root/temp/tempFile.tmp");
Files.copy(in, secondPath);
//open the second zip
FileSystem secondZip = FileSystems.newFileSystem(new URI("jar:" + secondPath.toUri().toString()),
new HashMap<>(), null);
//iterate files in the second zip
DirectoryStream<Path> ds = Files.newDirectoryStream(secondPath);
for(Path p: ds){
//something
}
//delete or update files inside the second zip
//Files.delete(seconZip.getPath("aFile.txt"));
//Files.copy(anInputStream, secondZip.getPath("destFoldet/aFile.txt"));
//clse the seconzip
secondZip.close();
//update the second zip inside first one
Files.copy(secondPath, firstZip.provider().newOutputStream(path));
//delete temp file
Files.delete(secondPath);
//close first zip
firstZip.close();
另一個想法是jboss-vfs 。 從未嘗試過,但我認為這可能會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.