1

I have a zip file that gets created at runtime that I need to copy to another directory however whenever I run my code I get a DirectoryNotEmptyException. Is there some extra parameter I need to specify to copy into a non-empty directory?

Here's the layout

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
{}
  • You need to do iteration/recursion IE: Copying one file over at a time. Check out Apache's FileUtils lib. – proulxs Sep 26 '14 at 18:26
  • See this answer: http://stackoverflow.com/a/1946311/2514228 The author suggests that copying zip files is easier to do as a read/write of bytes. – Micah Smith Sep 26 '14 at 18:28
  • @proulxs Okay that's what I feared, I was hoping since it's zipped it would read it as one "file" instead of a directory but alas –  Sep 26 '14 at 18:30

1 Answers1

0

the destination needs to contain the full path of the file that will eventually be there.

so you say if you needed to copy /home/dauser/faq.txt to /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
{}
bhowden
  • 669
  • 7
  • 15
  • and here -> http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#copy(java.nio.file.Path,%20java.nio.file.Path,%20java.nio.file.CopyOption...) – bhowden Sep 26 '14 at 18:37