我想从字节数组写入文件名“内容”到现有的zip文件中。从字节数组写入文件到zip文件
我已经管理到目前为止写一个文本文件\添加一个特定的文件到同一个zip。 我想要做的是同样的事情,而不是一个文件,代表一个文件的字节数组。我正在编写这个程序,以便它能够在服务器上运行,所以我无法在某处创建物理文件并将其添加到压缩文件中,这一切都必须发生在内存中。
这是我迄今没有“将字节数组写入文件”部分的代码。
public static void test(File zip, byte[] toAdd) throws IOException {
Map<String, String> env = new HashMap<>();
env.put("create", "true");
Path path = Paths.get(zip.getPath());
URI uri = URI.create("jar:" + path.toUri());
try (FileSystem fs = FileSystems.newFileSystem(uri, env)) {
Path nf = fs.getPath("avlxdoc/content");
try (BufferedWriter writer = Files.newBufferedWriter(nf, StandardOpenOption.CREATE)) {
//write file from byte[] to the folder
}
}
}
(我尝试使用的BufferedWriter,但它似乎没有工作...)
谢谢!
因为这应该是在内存中,你有没有考虑使用[memoryfilesystem(https://开头的github .COM /马绍尔/ memoryfilesystem)? – fge