[英]Java copy files into zip with Files
如何使用java.nio.files.Files
將文件從D:\\test\\
文件夾復制到D:\\test.zip
? 我收到了NoSuchFileException: D:\\Ausbildungsnachweise\\Ausbildungsnachweis_Technisch_Form.doc -> D:\\test.zip\\Ausbildungsnachweis_Technisch_Form.doc
作為例外。 我認為我使用的是正確的路徑,但是不知道為什么會發生此錯誤,因為路徑確實存在。
我的整個方法:
Map<String, String> env = Collections.singletonMap("create", "true");
Path dir = Paths.get(destinationFolder.getAbsolutePath());
Path destination = dir.resolve(zipNameGenerator.getName() + fileEnding);
URI uri = URI.create("jar:" + destination.toUri());
try {
FileSystem fs;
if (overwriteZipWithSameName || !Files.exists(destination)) {
fs = FileSystems.newFileSystem(uri, env);
} else {
fs = FileSystems.getFileSystem(uri);
}
for (String file : sourceFolder.list(filenameFilter)) {
Files.copy(sourceFolder.toPath().resolve(file), fs.getPath(file));
}
} catch (IOException e1) {
e1.printStackTrace();
}
URI uri = new URI("jar:file:/D:/test.zip");
不,應該是jar:file:///D:/test.zip
。 但是最簡便的方法是:
Path dir = Paths.get("D:\\");
Path file = dir.resolve("test.zip");
URI uri = URI.create("jar:" + file.toUri());
同樣,您在哪里:
Paths.get(sourceFolder + "\\" + file)
這樣做更方便:
sourceFolder.resolve(file)
(例如,如果將來在Linux上運行該應用程序,則“ \\”將不起作用。)
此外,您可以像這樣使您的地圖更簡潔:
Map<String, String> env = Collections.singletonMap("create", "true");
更新:
按照更新的代碼和下面的注釋,我創建了一個工作的主類,如下所示,在盡可能多地重用現有代碼的同時,在必要時進行了更正。 請注意,即使是現有的zip文件,也需要使用newFileSystem()
。 getFileSystem()
調用假定您已經在代碼中的其他位置創建了FileSystem
對象,該對象由文件系統提供程序緩存,並且FileSystems.getFileSystem()
返回對現有對象的引用。 另外,FileSystem是Closeable的,因此請在try-with-resources語句中使用它。
完整的課程,可以正常工作:
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.URI;
import java.nio.file.*;
import java.util.Collections;
import java.util.Map;
public class SOQn47577298 {
private final static Map<String, String> CREATE_TRUE =
Collections.singletonMap("create", "true");
interface ZipNameGenerator {
String getName();
}
static void copyFileToZip(
File sourceFolder,
FilenameFilter filenameFilter,
File destinationFolder,
ZipNameGenerator zipNameGenerator,
String fileEnding,
boolean overwriteZipWithSameName) throws IOException {
Path dir = Paths.get(destinationFolder.getAbsolutePath());
Path destination = dir.resolve(zipNameGenerator.getName() + fileEnding);
URI uri = URI.create("jar:" + destination.toUri());
final Map<String, String> env;
if (overwriteZipWithSameName || !Files.exists(destination)) {
env = CREATE_TRUE;
} else {
env = Collections.emptyMap();
}
try (FileSystem fs = FileSystems.newFileSystem(uri, env)){
for (String file : sourceFolder.list(filenameFilter)) {
Path source = sourceFolder.toPath().resolve(file);
Path dest = fs.getPath(file);
Files.copy(source, dest);
}
}
}
public static void main(String[] args) throws IOException {
final File source = new File("D:\\Ausbildungsnachweise");
final FilenameFilter nameFilter = (dir, name) -> name.endsWith(".doc");
final File dest = new File("D:\\");
final ZipNameGenerator zipNameGnr = () -> "test";
final String fileEnding = ".zip";
final boolean overwrite = false;
copyFileToZip(source, nameFilter, dest, zipNameGnr, fileEnding, overwrite);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.