import java.io.File; import java.io.IOException; import java.net.URI; import java.nio.file.FileSystem; import java.nio.file.FileSystems; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; import java.util.HashMap; import java.util.Map; import java.util.Random; public class JavaZipFileSystemMove { static private Random random = new Random(); static private final String FILE_PREFIX = "tempFile"; static private final String SUBFOLDER_NAME = "subfolder"; static public void main(String[] params) { File tempFile = null; FileSystem fileSystem = null; try { tempFile = File.createTempFile("JavaFileSystemTestMove", null); tempFile.delete(); URI fileURI = tempFile.toURI(); URI zip = new URI("jar:file", fileURI.getHost(), fileURI.getRawPath(), fileURI.getRawFragment()); // Create test data Map env = new HashMap(); env.put("create", "true"); fileSystem = FileSystems.newFileSystem(zip, env); byte[] data = new byte[1024*6]; Path rootPath = fileSystem.getRootDirectories().iterator().next(); Path subDir = rootPath.resolve(SUBFOLDER_NAME); subDir = Files.createDirectory(subDir); // Create 5 files for (int i=0; i<5; i++) { random.nextBytes(data); Path fileInZip = subDir.resolve(getFileName(i)); Files.write(fileInZip, data); } // Write data to disk fileSystem.close(); fileSystem = FileSystems.newFileSystem(zip, new HashMap()); System.out.println("Zip structure before file move:"); listFilesAndDirectories(fileSystem); rootPath = fileSystem.getRootDirectories().iterator().next(); subDir = rootPath.resolve(SUBFOLDER_NAME); Files.move(subDir.resolve(getFileName(0)), rootPath.resolve(getFileName(0))); System.out.println("Zip structure after file move:"); listFilesAndDirectories(fileSystem); } catch (Exception e) { e.printStackTrace(); } finally { if (fileSystem != null) { try {fileSystem.close();} catch (IOException eio) {} } if (tempFile != null) tempFile.delete(); } } static private String getFileName(int i) { return FILE_PREFIX+i+".tmp"; } static private void listFilesAndDirectories(FileSystem fileSystem) throws IOException { for (Path path : fileSystem.getRootDirectories()) { Files.walkFileTree(path, new SimpleFileVisitor() { @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { System.out.println(dir); return super.preVisitDirectory(dir, attrs); } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { System.out.println(file); return super.visitFile(file, attrs); } }); } } }