如何在不重写整个zip的情况下修改很大的zip中的单个文件? [英] How to modify a single file inside a very large zip without re-writing the entire zip?

查看:120
本文介绍了如何在不重写整个zip的情况下修改很大的zip中的单个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含大型文件的大型zip文件。 zip归档文件中有一些元数据文本文件需要修改。但是,无法提取整个zip并重新压缩。我需要在zip文件中找到目标文本文件,对其进行编辑,并可能将更改附加到zip文件中。文本文件的文件名始终相同,因此可以进行硬编码。这可能吗?有更好的方法吗?

I have large zip files that contain huge files. There are "metadata" text files within the zip archives that need to be modified. However, it is not possible to extract the entire zip and re-compress it. I need to locate the target text file inside the zip, edit it, and possibly append the change to the zip file. The file name of the text file is always the same, so it can be hard-coded. Is this possible? Is there a better way?

推荐答案

有两种方法。首先,如果您只是想避免重新压缩整个zip文件,则可以使用任何现有的zip实用程序来更新存档中的单个文件。这将需要有效地复制整个存档,并使用替换的条目创建一个新的存档,然后删除旧的zip文件。这不会重新压缩未替换的数据,因此它应该相对较快。至少与复制zip档案大约需要相同的时间。

There are two approaches. First, if you're just trying to avoid recompression of the entire zip file, you can use any existing zip utility to update a single file in the archive. This will entail effectively copying the entire archive and creating a new one with the replaced entry, then deleting the old zip file. This will not recompress the data not being replaced, so it should be relatively fast. At least, about the same time required to copy the zip archive.

如果要避免复制整个zip文件,则可以有效地删除想要的条目通过将zip文件的本地和中央标头中的名称(保持名称相同的长度)更改为您将不会使用的名称,这表明应该忽略该文件。例如。用波浪号替换名称的第一个字符。然后,您可以在更新的文本文件后附加一个新条目。这需要在zip文件的末尾重写中心目录,该目录非常小。

If you want to avoid copying the entire zip file, then you can effectively delete the entry you want to replace by changing the name within the local and central headers in the zip file (keeping the name the same length) to a name that you won't use otherwise and that indicates that the file should be ignored. E.g. replacing the first character of the name with a tilde. Then you can append a new entry with the updated text file. This requires rewriting the central directory at the end of the zip file, which is pretty small.

(另一个建议是不要引用中心目录中不需要的条目目录不一定会起作用,具体取决于用于读取zip文件的实用程序。一些实用程序将读取zip文件条目信息的本地标头,而忽略中央目录;其他实用程序则相反,因此本地和中央条目信息应保持同步。)

(A suggestion in another answer to not refer to the unwanted entry in the central directory will not necessarily work, depending on the utility being used to read the zip file. Some utilities will read the local headers for the zip file entry information, and ignore the central directory. Other utilities will do the opposite. So the local and central entry information should be kept in sync.)

这篇关于如何在不重写整个zip的情况下修改很大的zip中的单个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆