Java将文件复制到zip文件中

时间:2017-11-30 15:46:18

标签: java file-copying

如何使用D:\test\将文件从D:\test.zip文件夹复制到java.nio.files.Files?我得到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();
}

1 个答案:

答案 0 :(得分:2)

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是可关闭的,因此请在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);
    }
}