在 zip 中写入(修改或添加)文件 [英] writing (modifying or adding) a file inside a zip

查看:43
本文介绍了在 zip 中写入(修改或添加)文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已按照说明进行操作这个线程,使用那里的代码能够在不解压缩和重新压缩的情况下将文件添加到 zip 文件,但我有一个问题,让我向您展示我的代码:

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);
}

我想要做的是,我有很多文件用于同一个项目,该项目是一个存档(ZIP,在代码中由名为 projectArchiveFile 的 java.io.File 引用,这是我的class) 包含所有这些文件,当我想使用我的档案中的某个文件时,我只将该文件解压缩到一个文件夹中,该文件夹的结构与我的档案(ZIP、projectArchiveFile)中的结构相同,即对该文件夹的引用是一个名为 transactionalProjectFolder 的 java.io.File,也是我的类的一个实例变量.但是给了我这个错误:

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:

应对:C:\Dir1\Dir2\Dir3\Begin of Archive stucure\存档结构的另一个文件夹副本\An Excel File.xlsm到\存档结构的开始\存档结构的另一个文件夹副本\一个 Excel 文件.xlsm

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.

我已经能够在存档(zip)的根目录中写入,但是每当我尝试在存档(zip)内的文件夹中写入时,它都会失败,正如您在堆栈跟踪中所注意到的,它说 java.nio.file.NoSuchFileException: Begin of Archive stucure\Another folder replica of the archive structure\ 并且它在名称之前停止,如果我要复制的文件,我完全确定 zip 中的路径存在并且拼写正确,只是不想在存档中写入(我尝试使用 Files.copy 和 Files.move)文件,我一直被困在这一个月了,我不知道还能做什么,任何建议将不胜感激!!!

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!!!

提前致谢!:)...

推荐答案

即使您确定路径存在,我还是会在故障排除时添加下面的行并查看它创建的目录结构.

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

该错误表明 zip 中缺少目录.可能是您使用了 zip 等不支持的一些奇怪的文件夹名称.

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!!!");

这篇关于在 zip 中写入(修改或添加)文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆