繁体   English   中英

如何用Java复制zip文件

[英]How to copy a zip file in Java

我有一个在运行时创建的zip文件,需要将其复制到另一个目录,但是每当我运行代码时,我都会得到DirectoryNotEmptyException 我需要指定一些额外的参数以复制到非空目录中吗?

这是布局

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
{}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM