[英]How to convert a byte array to ZIP file and download it using Java?
[英]How to zip a byte array using ZipFileSystem [java]
如何使用 ZipFileSystem 創建帶有字節數組的ZipFileSystem
文件?
Zip 文件系統提供程序中的示例顯示了如何將 zip 設置為現有的,但我似乎找不到從 memory 字節數組中創建一個的方法。
我沒有意識到我對目標 zip 文件和 zip 文件中的文件使用了相同的值。
修改原始示例我有這個工作:
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 pathInZipfile = zipfs.getPath("/SomeTextFile.txt");
// copy a file into the zip file
Files.write(pathInZipfile, new byte[]{'h', 'i'} );
}
}
}
現在/codeSamples/zipfs/zipfstest.zip
文件是用/SomeTextFile.txt
hi
里面創建的,當我解壓縮它時,內容確實很好
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.