java修改zip文件中的文件在哪里,在Java中的ZIP存档中修改文本文件

My use case requires me to open a txt file, say abc.txt which is inside a zip archive which contains key-value pairs in the form

key1=value1

key2=value2

.. and so on where each key-value pair is in a new line.

I have to change one value corresponding to a certain key and put the text file back in a new copy of the archive. How do I do this in java?

My attempt so far:

ZipFile zipFile = new ZipFile("test.zip");

final ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("out.zip"));

for(Enumeration e = zipFile.entries(); e.hasMoreElements(); ) {

ZipEntry entryIn = (ZipEntry) e.nextElement();

if(!entryIn.getName().equalsIgnoreCase("abc.txt")){

zos.putNextEntry(entryIn);

InputStream is = zipFile.getInputStream(entryIn);

byte [] buf = new byte[1024];

int len;

while((len = (is.read(buf))) > 0) {

zos.write(buf, 0, len);

}

}

else{

// I'm not sure what to do here

// Tried a few things and the file gets corrupt

}

zos.closeEntry();

}

zos.close();

解决方案

You had almost got it right. One possible reason, the file was shown as corrupted is that you might have used

zos.putNextEntry(entryIn)

in the else part as well. This creates a new entry in the zip file containing information from the existing zip file. Existing information contains entry name(file name) and its CRC among other things.

And then, when u try to update the text file and close the zip file, it will throw an error as the CRC defined in the entry and the CRC of the object you are trying to write differ.

Also u might get an error if the length of the text that you are trying to replace is different than the one existing i.e. you are trying to replace

key1=value1

with

key1=val1

This boils down to the problem that the buffer you are trying to write to has length different than the one specified.

ZipFile zipFile = new ZipFile("test.zip");

final ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("out.zip"));

for(Enumeration e = zipFile.entries(); e.hasMoreElements(); ) {

ZipEntry entryIn = (ZipEntry) e.nextElement();

if (!entryIn.getName().equalsIgnoreCase("abc.txt")) {

zos.putNextEntry(entryIn);

InputStream is = zipFile.getInputStream(entryIn);

byte[] buf = new byte[1024];

int len;

while((len = is.read(buf)) > 0) {

zos.write(buf, 0, len);

}

}

else{

zos.putNextEntry(new ZipEntry("abc.txt"));

InputStream is = zipFile.getInputStream(entryIn);

byte[] buf = new byte[1024];

int len;

while ((len = (is.read(buf))) > 0) {

String s = new String(buf);

if (s.contains("key1=value1")) {

buf = s.replaceAll("key1=value1", "key1=val2").getBytes();

}

zos.write(buf, 0, (len < buf.length) ? len : buf.length);

}

}

zos.closeEntry();

}

zos.close();

The following code ensures that even if data that is replaced is of less length than the original length, no IndexOutOfBoundsExceptions occur.

(len < buf.length) ? len : buf.length

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用JavaZipFile和ZipOutputStream类来修改一个zip文件文件。首先,使用ZipFile类打开zip文件,并获取到需要修改文件ZipEntry对象。然后,使用ZipOutputStream类创建一个新的zip文件,在其添加修改后的文件内容。最后,将修改后的zip文件保存到磁盘上,并删除原始的zip文件。 下面是一个示例代码,演示如何使用Java修改zip文件文件: ```java import java.io.*; import java.util.zip.*; public class ZipFileModifier { public static void modifyFileInZip(String zipFilePath, String fileToModifyPath, String newContent) throws IOException { // Open the zip file File zipFile = new File(zipFilePath); ZipFile zip = new ZipFile(zipFile); // Get the entry for the file to modify ZipEntry fileToModify = zip.getEntry(fileToModifyPath); // Create a new zip file File newZipFile = new File(zipFile.getParentFile(), "modified.zip"); ZipOutputStream out = new ZipOutputStream(new FileOutputStream(newZipFile)); // Copy all entries from the original zip file to the new zip file, except for the file to modify Enumeration<? extends ZipEntry> entries = zip.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); if (!entry.equals(fileToModify)) { out.putNextEntry(entry); InputStream in = zip.getInputStream(entry); byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } out.closeEntry(); in.close(); } } // Add the modified file to the new zip file out.putNextEntry(new ZipEntry(fileToModifyPath)); out.write(newContent.getBytes()); out.closeEntry(); // Close the streams out.close(); zip.close(); // Delete the original zip file and rename the new zip file to the original name if (zipFile.delete()) { newZipFile.renameTo(zipFile); } } } ``` 上面的代码,modifyFileInZip方法接受三个参数:zipFilePath表示zip文件的路径,fileToModifyPath表示需要修改文件的路径,newContent表示修改后的文件内容。该方法会首先打开zip文件,获取到需要修改文件ZipEntry对象。然后,创建一个新的zip文件,并从原始zip文件复制所有文件到新zip文件,除了需要修改文件。最后,将修改后的文件内容添加到新zip文件,并将新zip文件保存到磁盘上。最后,删除原始的zip文件,并将新zip文件重命名为原始zip文件的名称。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值