如何更新 zip 存档中的一个文件
是否可以在不解压的情况下替换 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
尝试以下操作:
示例:
Try the following:
An example:
我发现 Linux
zip
文件对于替换 zip 中的单个文件来说很麻烦。 Java 开发工具包中的jar
实用程序可能更容易。考虑更新 JAR 文件(只是一个 zip 文件)中的WEB/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. Thejar
utility from the Java Development Kit may be easier. Consider the common task of updatingWEB/web.xml
in a JAR file (which is just a zip file):Here,
path/to/dir
is the path to a directory containing theWEB-INF
directory (which in turn containsweb.xml
).来自 zip(1):
因此,只需像平常一样使用
zip
命令即可创建一个仅包含该文件的新 .zip 文件,但您指定的 .zip 文件名将是现有存档。From zip(1):
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.使用更新标志:
-u
示例:
此命令将压缩并添加
myFolder
(及其内容)到existing.zip.
高级用法:
更新标志实际上会将传入文件与现有文件进行比较,并添加新文件或更新现有文件。
因此,如果您想添加/更新 zip 文件中的特定子目录,只需根据需要更新源代码,然后使用
-u
标志重新压缩整个源代码。只有更改过的文件才会被压缩。如果您无权访问源文件,可以解压缩 zip 文件,然后更新所需的文件,然后使用
-u
标志重新压缩。同样,只有更改的文件才会被压缩。示例:
原始源结构
更新的源结构
用法
Use the update flag:
-u
Example:
This command will compress and add
myFolder
(and it's contents) to theexisting.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
Updated Source Structure
Usage
我知道这是一个老问题,但我也想做同样的事情。更新 zip 存档中的文件。以上答案都没有真正帮助我。
这就是我所做的。创建临时目录
abc
。将file.zip
复制到abc
并将文件提取到该目录中。我编辑了我想要编辑的文件。然后,在
abc
中运行以下命令-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
. Copiedfile.zip
toabc
and extracted the file in that directory. I edited the file I wanted to edit.Then while being in
abc
, ran the following command-u
switch will look for changed/new files and will add to the zip archive.您可以使用:
You can use:
还有 -f 选项可以刷新 zip 文件。它可用于更新自 zip 生成以来已更新的所有文件(假设它们位于 zip 文件内树结构中的同一位置)。
如果您的文件名为 /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 存档结构的角度来看 - 您可以更新 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.
7zip
(7za) 可用于很好地添加/更新文件/目录:示例:
替换(无论文件日期)JAR 文件中的 MANIFEST.MF 文件。
/source/META-INF
目录包含要放入jar
(zip) 的MANIFEST.MF
文件:仅更新 (如果源较旧,则不会替换目标)
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 theMANIFEST.MF
file that you want to put into thejar
(zip):Only update (does not replace the target if the source is older)
是的,有可能。
在基于 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.