如何在Java中复制zip文件
我有一个在运行时创建的zip文件,我需要将它复制到另一个目录,但是每当我运行代码时,我都会得到一个如何在Java中复制zip文件,java,Java,我有一个在运行时创建的zip文件,我需要将它复制到另一个目录,但是每当我运行代码时,我都会得到一个目录notemptyexception。是否需要指定一些额外的参数才能复制到非空目录中 这是布局图 Path sourceZip = new File(path).toPath(); String destinDir = new File(System.getProperty("user.dir")).getParent(); Path target = Paths.get(destinDir);
目录notemptyexception
。是否需要指定一些额外的参数才能复制到非空目录中
这是布局图
Path sourceZip = new File(path).toPath();
String destinDir = new File(System.getProperty("user.dir")).getParent();
Path target = Paths.get(destinDir);
try {
Files.copy(sourceZip, target, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) //DirectoryNotEmptyException occurs here
{}
目标需要包含最终将在那里的文件的完整路径 所以你说如果你需要把/home/dauser/faq.txt复制到/home/faq.txt
File file = new File(path);
Path sourceZip = file.toPath();
StringBuilder sb = new StringBuilder();
sb.append(new File(System.getProperty("user.dir")).getParent());
sb.append("/");
sb.append(file.getName());
Path target = Paths.get(sb.toString());
try {
Files.copy(sourceZip, target, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) //DirectoryNotEmptyException occurs here
{}
您需要进行迭代/递归,即:一次复制一个文件。查看Apache的FileUtils库。看看这个答案:作者建议,通过读取/写入字节来复制zip文件更容易。@proulxs好的,这就是我担心的,我希望,因为它已压缩,所以它将作为一个“文件”而不是一个目录来读取,但在这里->