Add file to folder located in java zip file

I am trying to add a txt file to a folder located in a zip file. First, I extracted the entire contents of the zip file, and then added the txt file, and then returned it back. Then I read about the nio method, which I can change the zip without extracting it. Using this method, I can add the txt file to the main zip folder, but I cannot go deeper.

There is a res folder in the test.zip file.

Here is my code:

Path txtFilePath = Paths.get("\\test\\prefs.txt"); Path zipFilePath = Paths.get("\\test\\testing.zip"); FileSystem fs; try { fs = FileSystems.newFileSystem(zipFilePath, null); Path fileInsideZipPath = fs.getPath("res/prefs.txt"); //when I remover "res/" code works. Files.copy(txtFilePath, fileInsideZipPath); fs.close(); } catch (IOException e) { e.printStackTrace(); } 

I get the following exception:

 java.nio.file.NoSuchFileException: res/ 
+4
source share
2 answers

(edit to give a real answer)

make:

 fs.getPath("res").resolve("prefs.txt") 

instead:

 fs.getPath("res/prefs.txt") 

The .resolve() method will do the right thing with regard to file delimiters, etc.

+2
source

fs.getPath("res/prefs.txt") should work, and you don't need to split it into fs.getPath("res").resolve("prefs.txt") , as the answer states.

The java.nio.file.NoSuchFileException: res/ exception is a bit confusing because it mentions the file but the directory is actually missing.

I had a similar problem and all I had to do was:

 if (fileInsideZipPath.getParent() != null) Files.createDirectories(fileInsideZipPath.getParent()); 

See full example:

 @Test public void testAddFileToArchive() throws Exception { Path fileToAdd1 = rootTestFolder.resolve("notes1.txt"); addFileToArchive(archiveFile, "notes1.txt", fileToAdd1); Path fileToAdd2 = rootTestFolder.resolve("notes2.txt"); addFileToArchive(archiveFile, "foo/bar/notes2.txt", fileToAdd2); . . . } public void addFileToArchive(Path archiveFile, String pathInArchive, Path srcFile) throws Exception { FileSystem fs = FileSystems.newFileSystem(archiveFile, null); Path fileInsideZipPath = fs.getPath(pathInArchive); if (fileInsideZipPath.getParent() != null) Files.createDirectories(fileInsideZipPath.getParent()); Files.copy(srcFile, fileInsideZipPath, StandardCopyOption.REPLACE_EXISTING); fs.close(); } 

If I remove the Files.createDirectories() bit and make sure that you run a clear test directory, I get:

 java.nio.file.NoSuchFileException: foo/bar/ at com.sun.nio.zipfs.ZipFileSystem.checkParents(ZipFileSystem.java:863) at com.sun.nio.zipfs.ZipFileSystem.newOutputStream(ZipFileSystem.java:528) at com.sun.nio.zipfs.ZipPath.newOutputStream(ZipPath.java:792) at com.sun.nio.zipfs.ZipFileSystemProvider.newOutputStream(ZipFileSystemProvider.java:285) at java.nio.file.Files.newOutputStream(Files.java:216) at java.nio.file.Files.copy(Files.java:3016) at java.nio.file.CopyMoveHelper.copyToForeignTarget(CopyMoveHelper.java:126) at java.nio.file.Files.copy(Files.java:1277) at my.home.test.zipfs.TestBasicOperations.addFileToArchive(TestBasicOperations.java:111) at my.home.test.zipfs.TestBasicOperations.testAddFileToArchive(TestBasicOperations.java:51) 
+1
source

Source: https://habr.com/ru/post/1485960/


All Articles