java游戏修改存档_在Java中的ZIP存档中修改文本文件

小编典典

您几乎完全正确。一种可能的原因,该文件显示为已损坏,是您可能已使用过

zos.putNextEntry(entryIn)

在其他部分也是如此。这将在zip文件中创建一个新条目,其中包含来自现有zip文件的信息。现有信息包含条目名称(文件名)及其CRC等信息。

然后,当您尝试更新文本文件并关闭zip文件时,由于条目中定义的CRC与您尝试写入的对象的CRC不同,它将引发错误。

另外,如果您要替换的文本的长度与现有文本的长度不同(即您要替换的文本),则可能会出错

key1 = value1

key1 = val1

归结为您要写入的缓冲区的长度与指定的缓冲区长度不同的问题。

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

以下代码确保即使替换的数据长度小于原始长度,也不会发生IndexOutOfBoundsExceptions。

(len

2020-09-26

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要更新ZipArchive文件,您需要执行以下步骤: 1. 首先,您需要打开ZipArchive文件并获取到需要更新的文件的引用。 2. 然后,使用Java IO的FileOutputStream类或者Java NIO的FileChannel类打开此文件。 3. 将需要更新的文件写入到此文件。 4. 关闭文件流。 5. 最后,将更新后的ZipArchive文件保存。 以下是示例代码: ```java import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class UpdateZipArchive { public static void main(String[] args) throws IOException { // Open the existing zip archive Path zipPath = Paths.get("myarchive.zip"); try (ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipPath.toFile()))) { // Get the entry of the file to be updated ZipEntry entry = zipOut.getEntry("file-to-be-updated.txt"); // Create a new entry with the same name ZipEntry newEntry = new ZipEntry("file-to-be-updated.txt"); // Write the contents of the updated file to the new entry Path updatedFilePath = Paths.get("updated-file.txt"); byte[] updatedFileBytes = Files.readAllBytes(updatedFilePath); newEntry.setSize(updatedFileBytes.length); zipOut.putNextEntry(newEntry); zipOut.write(updatedFileBytes); zipOut.closeEntry(); // Add all other entries from the original archive to the updated archive for (ZipEntry oldEntry : zipOut) { if (!oldEntry.getName().equals("file-to-be-updated.txt")) { zipOut.putNextEntry(oldEntry); zipOut.closeEntry(); } } } } } ``` 此代码将从名为“myarchive.zip”的文件获取名为“file-to-be-updated.txt”的文件的引用,并将其替换为名为“updated-file.txt”的新文件。最后,它将保存更新后的ZipArchive文件。请注意,此代码仅更新了一个文件,如果您需要更新多个文件,则需要相应地更改代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值