我想压缩文件内使用java FileSystem目录。当只有很少的文件时工作正常,但当超过100个文件时失败。在ZIP中的多个文件zip错误ZIP文件系统
这是我在程序中使用的代码:
Map<String, String> env = new HashMap<>();
env.put("create", "true");
URI uri = URI.create("jar:file://10.0.8.31/Shared/testFile.zip");
long bytesRead = 0;
File dir = new File("D:\\Shared\\DPXSequence");
try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {
for (File sourceF : dir.listFiles()) {
Path externalFile = Paths.get(sourceF.getAbsolutePath());
Path pathInZipfile = zipfs.getPath("/" + sourceF.getName());
// copy a file into the zip file
Files.copy(externalFile, pathInZipfile, StandardCopyOption.REPLACE_EXISTING);
}
}
catch(Exception e) {
System.out.println("Error : "+e.toString());
}
这是我收到的错误:
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
我哪里做错了吗? 我认为Files.copy()
执行完成之前,它实际上压缩并复制到目标文件夹。这是造成这个问题吗?
也许这个[回复](http://stackoverflow.com/a/23861949/2287893)by @ diego-giagio回答有助于克服OOM。 – MZerau