如何更新 zip 存档中的一个文件

发布于 2024-10-13 20:40:31 字数 347 浏览 6 评论 0原文

是否可以在不解压的情况下替换 zip 文件中的文件?

要更新的文件是一个 XML 文件,位于一个巨大的 zip 存档中。要更新此 XML 文件,我必须解压缩存档,删除旧的 XML 文件,添加新文件,然后重新压缩。这需要相当长的时间。因此希望能够通过脚本替换该 XML。我已经有了一个可以检查 XML 更新的工具。

使用 zip 命令
抱歉,我会使用 zip 命令来执行类似的操作,但问题是该脚本实际上适用于 Android 手机,而 zip 不是我可以使用的命令,不幸的是,抱歉我遗漏了这一点。如果可以的话,我肯定会使用 zip,但我只为 droid 解压,然后 busybox 中有 tar,但 tar 不能满足我的需要。

Is it possible to replace a file in a zip file without unzipping?

The file to update is an XML file that resides in a huge zip archive. To update this XML file, I have to unzip the archive, delete the old XML file, add the new one and then rezip. This takes a considerable amount of time. So want to be able to replace that one XML through a script. I already have the one that checks for updates on the XML I have.

using zip command
Sorry, I would use the zip command to do things like that but the problem is the script is actually for an android phone and zip is not a command I can use unfortunately sorry I left that out. I would have used zip definitely if i could but I only have unzip for droid and then there is tar in busybox but tar doesn't do what I need.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

寒冷纷飞旳雪 2024-10-20 20:40:31

尝试以下操作:

zip [zipfile] [file to update] 

示例:

$ zip test.zip test/test.txt
updating: test/test.txt (stored 0%)

Try the following:

zip [zipfile] [file to update] 

An example:

$ zip test.zip test/test.txt
updating: test/test.txt (stored 0%)
意中人 2024-10-20 20:40:31

我发现 Linux zip 文件对于替换 zip 中的单个文件来说很麻烦。 Java 开发工具包中的 jar 实用程序可能更容易。考虑更新 JAR 文件(只是一个 zip 文件)中的 WEB/web.xml 的常见任务:

jar -uf path/to/myapp.jar -C path/to/dir WEB-INF/web.xml

这里,path/to/dir 是指向包含 WEB-INF 目录(该目录又包含 web.xml)。

I've found the Linux zip file to be cumbersome for replacing a single file in a zip. The jar utility from the Java Development Kit may be easier. Consider the common task of updating WEB/web.xml in a JAR file (which is just a zip file):

jar -uf path/to/myapp.jar -C path/to/dir WEB-INF/web.xml

Here, path/to/dir is the path to a directory containing the WEB-INF directory (which in turn contains web.xml).

烟织青萝梦 2024-10-20 20:40:31

来自 zip(1)

当给定现有 zip 存档的名称时,zip 将替换 zip 存档中的同名条目或添加新名称的条目。

因此,只需像平常一样使用 zip 命令即可创建一个仅包含该文件的新 .zip 文件,但您指定的 .zip 文件名将是现有存档。

From zip(1):

When given the name of an existing zip archive, zip will replace identically named entries in the zip archive or add entries for new names.

So just use the zip command as you normally would to create a new .zip file containing only that one file, except the .zip filename you specify will be the existing archive.

给妤﹃绝世温柔 2024-10-20 20:40:31

使用更新标志:-u

示例:

zip -ur existing.zip myFolder

此命令将压缩并添加 myFolder(及其内容)到 existing.zip.


高级用法:

更新标志实际上会将传入文件与现有文件进行比较,并添加新文件或更新现有文件。

因此,如果您想添加/更新 zip 文件中的特定子目录,只需根据需要更新源代码,然后使用 -u 标志重新压缩整个源代码。只有更改过的文件才会被压缩。

如果您无权访问源文件,可以解压缩 zip 文件,然后更新所需的文件,然后使用 -u 标志重新压缩。同样,只有更改的文件才会被压缩。

示例:

原始源结构

ParentDir
├── file1.txt
├── file2.txt
├── ChildDir
│   ├── file3.txt
│   ├── Logs
│   │   ├── logs1.txt
│   │   ├── logs2.txt
│   │   ├── logs3.txt

更新的源结构

ParentDir
├── file1.txt
├── file2.txt
├── ChildDir
│   ├── file3.txt
│   ├── Logs
│   │   ├── logs1.txt
│   │   ├── logs2.txt
│   │   ├── logs3.txt 
│   │   ├── logs4.txt <-- NEW FILE 

用法

$ zip -ur existing.zip ParentDir 
> updating: ParentDir/ChildDir/Logs (stored 0%)
>   adding: ParentDir/ChildDir/Logs/logs4.txt (stored 96%)

Use the update flag: -u

Example:

zip -ur existing.zip myFolder

This command will compress and add myFolder (and it's contents) to the existing.zip.


Advanced Usage:

The update flag actually compares the incoming files against the existing ones and will either add new files, or update existing ones.

Therefore, if you want to add/update a specific subdirectory within the zip file, just update the source as desired, and then re-zip the entire source with the -u flag. Only the changed files will be zipped.

If you don't have access to the source files, you can unzip the zip file, then update the desired files, and then re-zip with the -u flag. Again, only the changed files will be zipped.

Example:

Original Source Structure

ParentDir
├── file1.txt
├── file2.txt
├── ChildDir
│   ├── file3.txt
│   ├── Logs
│   │   ├── logs1.txt
│   │   ├── logs2.txt
│   │   ├── logs3.txt

Updated Source Structure

ParentDir
├── file1.txt
├── file2.txt
├── ChildDir
│   ├── file3.txt
│   ├── Logs
│   │   ├── logs1.txt
│   │   ├── logs2.txt
│   │   ├── logs3.txt 
│   │   ├── logs4.txt <-- NEW FILE 

Usage

$ zip -ur existing.zip ParentDir 
> updating: ParentDir/ChildDir/Logs (stored 0%)
>   adding: ParentDir/ChildDir/Logs/logs4.txt (stored 96%)
请你别敷衍 2024-10-20 20:40:31

我知道这是一个老问题,但我也想做同样的事情。更新 zip 存档中的文件。以上答案都没有真正帮助我。

这就是我所做的。创建临时目录abc。将 file.zip 复制到 abc 并将文件提取到该目录中。我编辑了我想要编辑的文件。
然后,在 abc 中运行以下命令

user@host ~/temp/abc $ zip -u file.zip
updating: content/js/ (stored 0%)
updating: content/js/moduleConfig.js (deflated 69%)

-u 开关将查找已更改/新文件并将添加到 zip 存档中。

I know this is old question, but I wanted to do the same. Update a file in zip archive. And none of the above answers really helped me.

Here is what I did. Created temp directory abc. Copied file.zip to abc and extracted the file in that directory. I edited the file I wanted to edit.
Then while being in abc, ran the following command

user@host ~/temp/abc $ zip -u file.zip
updating: content/js/ (stored 0%)
updating: content/js/moduleConfig.js (deflated 69%)

-u switch will look for changed/new files and will add to the zip archive.

本宫微胖 2024-10-20 20:40:31

您可以使用:

zip -u file.zip path/file_to_update

You can use:

zip -u file.zip path/file_to_update
鸩远一方 2024-10-20 20:40:31

还有 -f 选项可以刷新 zip 文件。它可用于更新自 zip 生成以来已更新的所有文件(假设它们位于 zip 文件内树结构中的同一位置)。

如果您的文件名为 /myfiles/myzip.zip 您所要做的就是

zip -f /myfiles/myzip.zip

There is also the -f option that will freshen the zip file. It can be used to update ALL files which have been updated since the zip was generated (assuming they are in the same place within the tree structure within the zip file).

If your file is named /myfiles/myzip.zip all you have to do is

zip -f /myfiles/myzip.zip
高冷爸爸 2024-10-20 20:40:31

从 ZIP 存档结构的角度来看 - 您可以更新 zip 文件而无需重新压缩它,您只需跳过需要替换的文件之前的所有文件,然后放置更新的文件,然后放置其余文件。最后,您需要放置更新的中央目录结构。
然而,我怀疑大多数常见的工具是否能让你做到这一点。

From the side of ZIP archive structure - you can update zip file without recompressing it, you will just need to skip all files which are prior of the file you need to replace, then put your updated file, and then the rest of the files. And finally you'll need to put the updated centeral directory structure.
However, I doubt that most common tools would allow you to make this.

Saygoodbye 2024-10-20 20:40:31

7zip (7za) 可用于很好地添加/更新文件/目录:

示例:
替换(无论文件日期)JAR 文件中的 MANIFEST.MF 文件。 /source/META-INF 目录包含要放入 jar (zip) 的 MANIFEST.MF 文件:

7za a /tmp/file.jar /source/META-INF/

仅更新 (如果源较旧,则不会替换目标)

7za u /tmp/file.jar /source/META-INF/

7zip (7za) can be used for adding/updating files/directories nicely:

Example:
Replacing (regardless of file date) the MANIFEST.MF file in a JAR file. The /source/META-INF directory contains the MANIFEST.MF file that you want to put into the jar (zip):

7za a /tmp/file.jar /source/META-INF/

Only update (does not replace the target if the source is older)

7za u /tmp/file.jar /source/META-INF/
删除会话 2024-10-20 20:40:31

是的,有可能。

在基于 Linux 的系统上只需安装 zip 即可在命令行中调用它。看一下手册页: http://linux.die.net/man/1/zip< /a>

但根据我个人的经验,如果可能的话并且压缩不是那么重要,这对于纯 tar 文件和 tar 效果更好。

yes its possible.

on linux based systems just install zip and you can call it in the command line. have a look at the manpage: http://linux.die.net/man/1/zip

but in my personal experience, if possible and compression is not so important, this works better with plain tar files and tar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文