1

I'm trying to rename a directory in a ZIP file using Java 7 NIO.

The zip file is very simple: it contains a "TestFolder" directory, which contains a "test.txt" file.

I want to rename TestFolder to NewFolder.

Here is my code:

public class Test {

    public static void main(String[] args) {
        Path test = Paths.get("C:\\myZipFile.zip");

        try (FileSystem zipfsWin = FileSystems.newFileSystem(test, null)) {
            Path oldFolderName = zipfsWin.getPath("/TestFolder");
            Path newFolderName = zipfsWin.getPath("/NewFolder");
            Files.move(oldFolderName, newFolderName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

The problem is that after running this code, the TestFolder is still here, with its text file and I have a new empty NewFolder directory.

What am I missing?

Ben
  • 6,321
  • 9
  • 40
  • 76
  • Nope: Process finished with exit code 0 – Ben Apr 29 '16 at 15:51
  • Yes, it's normal, you can't move non-empty directories like that. You need to do it recursively, see the linked question. The fact that you're in a ZIP or not doesnt matter. – Tunaki Apr 29 '16 at 16:00
  • Really? Renaming a folder has to be done recursively by hand?! That's not really straightforward... I thought Java NIO in Java 7 was supposed to simplify your life... – Ben Apr 29 '16 at 16:04
  • Look at the usage examples on the [FileVisitor](https://docs.oracle.com/javase/7/docs/api/java/nio/file/FileVisitor.html) javadoc... it might be useful to you (the 2nd example in particular) – Matthew Diana Apr 29 '16 at 16:09
  • Thank you. The thing is if I want to rename a folder containing thousands of files, I fear it will be very inefficient. I though renaming would be more straightforward – Ben Apr 29 '16 at 16:29
  • According to this https://www.google.fr/url?sa=t&source=web&rct=j&url=https://docs.oracle.com/javase/tutorial/essential/io/move.html&ved=0ahUKEwjHr-iRo7TMAhUsLMAKHdlPBpYQFggfMAE&usg=AFQjCNGoAeluXmr_9vf-QqeBM2FB19MkrQ&sig2=WlGAtOU5TltF-VdqBrbIsw it should be possible to rename a non empty folder... On certain filesystems. I guess zip is not one of them – Ben Apr 29 '16 at 16:51

0 Answers0