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

查看:640
本文介绍了在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引用,这是我的实例变量类)包含所有这些文件,当我想使用存档中的某些文件时,我仅将该文件解压缩到一个文件夹,该文件夹的结构与存档中的文件夹(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 \ Archive stucure \ Archive结构的另一个文件夹副本\ Excel File.xlsm 到 \ Archive stucure \ Archive结构的另一个文件夹副本\ Excel File.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!!!

先谢谢! :)...

thanks in advance! :)...

推荐答案

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

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天全站免登陆