我的要求是替换zip文件中的几个文件。
zip文件又包含多个zip文件和文件夹。它可以支持4个或更多级别的zip文件。
我在不同的源目录中有一组文件。我想复制这些文件并替换到zip文件中,方法是将源目录中的文件名与zip文件中的文件名进行匹配。
有人能帮帮忙吗?
谢谢,德利普
发布于 2016-05-12 19:14:43
一种方法是使用临时文件来存储压缩文件,并将其作为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。我从来没有试过,但我认为这可能会有帮助。
https://stackoverflow.com/questions/37165381
复制相似问题