writing (modifying or adding) a file inside a zip

こ雲淡風輕ζ 提交于 2019-12-11 02:37:30

问题


I've followed the instructions in this thread, using the code in there I've been able to add a file to a zip file without uncompressing and recompresing it, but i have a problem, let me show you my code:

private void saveFileIntoProjectArchive(Path pathOfFile) {
    this.projectArchiveFile.setWritable(true, false);
    Path zipFilePath = Paths.get(this.projectArchiveFile.getAbsolutePath()),
            pathToSaveInsideZIP = null;
    FileSystem fs;
    try {
        fs = FileSystems.newFileSystem(zipFilePath, null);
        pathToSaveInsideZIP = fs.getPath(pathOfFile.toString().substring((int) this.transactionalProjectFolder.getAbsolutePath().length()));
        System.out.println("Coping from:\n\t"+pathOfFile+"\nto\n\t"+pathToSaveInsideZIP);
        Files.copy(pathOfFile, pathToSaveInsideZIP, REPLACE_EXISTING);
        System.out.println("Done!!!");
        fs.close();
    } catch (java.nio.file.NoSuchFileException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    projectArchiveFile.setWritable(false, false);
}

what I'm trying to do is, i have many files for a same project, that project is an archive (ZIP, which in the code is referenced by a java.io.File called projectArchiveFile, an instance variable of my class) containing all of those files, when I want to work with certain file inside my archive, i uncompress only that file into a folder which has an structure identical to the one inside my archive (ZIP, projectArchiveFile), the reference to that folder is a java.io.File called transactionalProjectFolder, also an instance variable of my class. But is giving me this error:

coping from: C:\Dir1\Dir2\Dir3\Begin of Archive stucure\Another folder replica of the archive structure\An Excel File.xlsm to \Begin of Archive stucure\Another folder replica of the archive structure\An Excel File.xlsm

java.nio.file.NoSuchFileException: Begin of Archive stucure\Another folder replica of the archive structure\ at com.sun.nio.zipfs.ZipFileSystem.checkParents(ZipFileSystem.java:846)
at com.sun.nio.zipfs.ZipFileSystem.newOutputStream(ZipFileSystem.java:515)
at com.sun.nio.zipfs.ZipPath.newOutputStream(ZipPath.java:783)
at com.sun.nio.zipfs.ZipFileSystemProvider.newOutputStream(ZipFileSystemProvider.java:276)
at java.nio.file.Files.newOutputStream(Files.java:170)
at java.nio.file.Files.copy(Files.java:2826)
at java.nio.file.CopyMoveHelper.copyToForeignTarget(CopyMoveHelper.java:126)
at java.nio.file.Files.copy(Files.java:1222)

the rest of the stack trace are my classes.

I've been able to write in the root of the archive(zip), but whenever i try to write inside of a folder which is inside of the archive (zip) it fails, as you can notice in the stack trace, it says that java.nio.file.NoSuchFileException: Begin of Archive stucure\Another folder replica of the archive structure\ and it stops right before the name if the file which I'm trying to copy, I'M ENTIRELY SURE that the path inside the zip exist and it is appropriately spelled it just do not want to write (I've trying with Files.copy and Files.move) the file inside the archive, I've been stuck in this for a month, I don't know what else to do, any suggestion will be appreciated!!!

thanks in advance! :)...


回答1:


Even tho you are sure the path exists, I would for troubleshooting add below row and see what directory structure it creates.

The error indicate the directories are missing inside the zip. Could be you are using some weird folder names not supported by zip etc..

System.out.println("Coping from:\n\t"+pathOfFile+"\nto\n\t"+pathToSaveInsideZIP);

Files.createDirectories(pathToSaveInsideZIP);  // add this row

Files.copy(pathOfFile, pathToSaveInsideZIP, StandardCopyOption.REPLACE_EXISTING);
System.out.println("Done!!!");



回答2:


I'll recommend you to try out the TrueZip it's easy to use, it exposes any archive into a virtual file system, then you can append, delete or edit any file inside the archive easily.



来源:https://stackoverflow.com/questions/14647152/writing-modifying-or-adding-a-file-inside-a-zip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!