我有一个要求,必须在 .zip 文件夹中创建 1 个 xml 文件。
您能否告诉我如何使用 java/scala 实现此目的?
另外请告诉我,我可以直接更新 .zip 文件夹 Array[Byte] 并为我的新 xml 文件添加额外的 Array[Byte] 吗?
最佳答案
如果您使用的是 Java 7 及更高版本,那么您应该使用 Zip File System
The following code sample shows how to create a zip file system and copy a file to the new zip file system.
import java.util.*;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.*;
public class ZipFSPUser {
public static void main(String [] args) throws Throwable {
Map<String, String> env = new HashMap<>();
env.put("create", "true");
// locate file system by using the syntax
// defined in java.net.JarURLConnection
URI uri = URI.create("jar:file:/codeSamples/zipfs/zipfstest.zip");
try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {
Path externalTxtFile = Paths.get("/codeSamples/zipfs/SomeTextFile.txt");
Path pathInZipfile = zipfs.getPath("/SomeTextFile.txt");
// copy a file into the zip file
Files.copy( externalTxtFile,pathInZipfile,
StandardCopyOption.REPLACE_EXISTING );
}
}
}
这篇文章可以帮助您:Appending files to a zip file with Java
关于java - 使用 Scala 在 .zip 文件夹中创建一个新文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32733371/