【发布时间】:2013-01-16 19:34:43
【问题描述】:
我已按照 this thread,使用那里的代码,我已经能够将文件添加到 zip 文件中,而无需解压缩和重新压缩它,但是我有一个问题,让我向您展示我的代码:
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,也是我的类的一个实例变量。但是给我这个错误:
应对: C:\Dir1\Dir2\Dir3\存档结构的开始\存档结构的另一个文件夹副本\An Excel File.xlsm 到 \存档结构的开始\存档结构的另一个文件夹副本\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)
堆栈跟踪的其余部分是我的类。
我已经能够在存档 (zip) 的根目录中写入,但是每当我尝试在存档 (zip) 内的文件夹中写入时,它都会失败,正如您在堆栈跟踪中所注意到的那样,它说java.nio.file.NoSuchFileException: Begin of Archive stucure\Another folder replica of the archive structure\,如果我要复制的文件在名称之前停止,我完全确定zip中的路径存在并且拼写正确,只是不想写(我正在尝试使用 Files.copy 和 Files.move) 存档中的文件,我已经被困了一个月,我不知道还能做什么,任何建议将不胜感激!!!
提前致谢! :)...
【问题讨论】:
-
也许 Windows 也使用反斜杠
` should be replaced by standard slashes/`。 -
不,事实上我正在使用
File.separator进行这项工作,所以这不是问题......