【发布时间】:2015-05-14 09:49:57
【问题描述】:
我正在尝试将字节数组中的文件名“内容”写入现有的 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?