java 写入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);

}

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! :)...

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Java ,您可以使用 java.util.zip的类来创建压缩文件(也称为 ZIP 包)。 要将文件写入 ZIP 包,您需要使用 ZipOutputStream 类。首先,创建一个 FileOutputStream ,然后将其作为参数传递给 ZipOutputStream 的构造函数。然后,调用 putNextEntry() 方法来创建一个新的 ZIP 条目,并使用 write() 方法将数据写入 ZIP 条目。最后,调用 closeEntry() 和 close() 方法来关闭 ZIP 条目和流。 以下是一个示例,该示例将文本写入名为 "test.txt" 的文件,并将该文件添加到名为 "test.zip" 的 ZIP: ``` import java.io.*; import java.util.zip.*; public class Main { public static void main(String[] args) throws IOException { String text = "This is a test."; // Create a buffer for reading the files byte[] buffer = new byte[1024]; // Create the ZIP file String outFilename = "test.zip"; ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename)); // Create a new ZIP entry with the file name ZipEntry entry = new ZipEntry("test.txt"); out.putNextEntry(entry); // Transfer bytes from the file to the ZIP file InputStream in = new ByteArrayInputStream(text.getBytes()); int len; while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } // Complete the entry out.closeEntry(); in.close(); // Complete the ZIP file out.close(); } } ``` ### 回答2: 在Java,可以使用java.util.zip包来实现文件压缩和解压缩的功能。下面我将简要介绍如何使用ZipOutputStream类将文件写入ZIP。 首先,我们需要创建一个ZipOutputStream对象,该对象可以将数据写入ZIP文件。创建ZipOutputStream对象时,需要指定一个OutputStream对象用于指定数据存储位置,例如将数据写入文件或者内存。 然后,我们需要向ZipOutputStream对象逐个写入文件。这里我们需要使用ZipEntry类来表示每个要写入ZIP文件。可以通过调用ZipOutputStream的putNextEntry方法来添加新的ZipEntry对象。接着,我们可以使用ZipOutputStream的write方法将文件的内容写入到当前ZipEntry对象所表示的文件。 在将所有文件写入ZIP包后,我们需要调用ZipOutputStream的closeEntry方法来关闭当前ZipEntry。最后,我们需要调用ZipOutputStream的close方法来关闭ZipOutputStream对象,以确保所有数据都已成功写入ZIP。 需要注意的是,如果要压缩多个文件,可以重复上述步骤,并在每个文件写入完成后关闭当前ZipEntry。另外,对于文件夹,可以使用File类的isDirectory方法来判断,然后使用mkdir方法创建一个新的ZipEntry对象并添加ZipOutputStream。 总结起来,使用java.util.zip包的ZipOutputStream类可以方便地将文件写入ZIP。步骤包括创建ZipOutputStream对象、使用ZipEntry类表示文件写入文件内容、关闭当前ZipEntry,并在所有文件写入完成后关闭ZipOutputStream对象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值