Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java使用文件将文件复制到zip中_Java_File Copying - Fatal编程技术网

Java使用文件将文件复制到zip中

Java使用文件将文件复制到zip中,java,file-copying,Java,File Copying,如何使用java.nio.files.files将文件从D:\test\文件夹复制到D:\test.zip?我得到了NoSuchFileException:D:\Ausbildungsnachweise\Ausbildungsnachweis\u technicsh\u Form.doc->D:\test.zip\Ausbildungsnachweis\u technicsh\u Form.doc作为例外。我认为我使用的是正确的路径,但不知道为什么会发生此错误,因为路径确实存在 我的整个方法是:

如何使用
java.nio.files.files
将文件从
D:\test\
文件夹复制到
D:\test.zip
?我得到了
NoSuchFileException:D:\Ausbildungsnachweise\Ausbildungsnachweis\u technicsh\u Form.doc->D:\test.zip\Ausbildungsnachweis\u technicsh\u 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();
}
Map env=Collections.singletonMap(“create”、“true”);
Path dir=Path.get(destinationFolder.getAbsoleTPath());
Path destination=dir.resolve(zipNameGenerator.getName()+fileEnding);
URI=URI.create(“jar:+destination.toUri());
试一试{
文件系统fs;
如果(覆盖IPWithSamename | |!Files.exists(目标)){
fs=FileSystems.newFileSystem(uri,env);
}否则{
fs=FileSystems.getFileSystem(uri);
}
用于(字符串文件:sourceFolder.list(filenameFilter)){
copy(sourceFolder.toPath().resolve(文件),fs.getPath(文件));
}
}捕获(IOE1异常){
e1.printStackTrace();
}
不,应该是
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");

能否尝试将文件:/D:/test.zip更改为文件://D://test.zip。只有2个百分点你为什么不使用ZipEntry?@DavidBrossard这是个人喜好的问题,但我发现zip文件系统比ZipEntry更容易使用。谢谢你的提示,非常感谢。遗憾的是,我无法从
URI
创建文件。它抛出IllegalArgumentException:URI不是分层的。我需要我的目的地文件,以检查该文件是否已存在,或者是否需要新文件one@XtremeBaumer这不应该是个问题。你从哪里得到这个错误?是在调用
Files.exists(uri)
时发生的吗?您不应该向Files.exists传递uri;只需传递一个路径(前面没有jar:in)。好的,现在它可以正确地创建一个新的zip文件(如果不存在),但是我还需要打开现有的zip文件。如果我尝试这样做,它会在
fs=FileSystems.getFileSystem(uri)上抛出
FileSystemNotFoundException
。我的复制也没什么用。(更新代码)你能举一个复制文件的工作示例吗?使用
Files.copy(sourceFolder.toPath().resolve(file)、新文件(destination.resolve(file.toString()).toPath())我得到这个异常
NoSuchFileException:D:\ausbillingsnachweise\ausbillingsnachweis\u Technisch\u Form.doc->D:\test.zip\ausbillingsnachweis\u technicsch\u Form.doc
。源文件存在,设计目录也存在。我更新的代码(包括自包含的演示应用程序类)对您有用吗?
Map<String, String> env = Collections.singletonMap("create", "true");
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);
    }
}