bash 如何更新 zip 存档中的一个文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4799553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to update one file in a zip archive
提问by user577732
Is it possible to replace a file in a zip file without unzipping deleting the old file adding the new file and rezipping it back?
是否可以在不解压缩的情况下替换 zip 文件中的文件,删除旧文件,添加新文件并将其重新压缩?
Reason is I have a zip file which is really big there is one xml inside the zip file that I have to update sometimes. Unzipping the zip and rezipping it takes a long time. So I'd like to just be able to replace the one xml inside the zip through a script. I already have that checks for updates on the xml I have.
原因是我有一个非常大的 zip 文件,在 zip 文件中有一个 xml,有时我必须更新。解压缩并重新压缩需要很长时间。所以我只想能够通过脚本替换 zip 中的一个 xml。我已经检查了我拥有的 xml 的更新。
So is it possible to just replace the one xml without unzipping and rezipping ?
那么是否可以只替换一个 xml 而无需解压缩和重新压缩?
Sorry i would use the zip command to do things like that but 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 definately 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
抱歉,我会使用 zip 命令来做这样的事情,但问题是脚本实际上是针对 android 手机的,而 zip 不是我可以使用的命令,很遗憾,我把它遗漏了。如果可以的话,我肯定会使用 zip,但我只有为 droid 解压缩,然后busybox 中有 tar,但 tar 不能满足我的需要
采纳答案by PleaseStand
From zip(1):
从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.
当给定现有 zip 存档的名称时,zip 将替换 zip 存档中同名的条目或为新名称添加条目。
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.
因此,只需zip
像往常一样使用该命令创建一个仅包含该文件的新 .zip 文件,但您指定的 .zip 文件名将是现有存档。
回答by cayhorstmann
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):
我发现 Linuxzip
文件替换 zip 中的单个文件很麻烦。jar
Java Development Kit 中的实用程序可能更简单。考虑WEB/web.xml
在 JAR 文件(它只是一个 zip 文件)中更新的常见任务:
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
).
这里,path/to/dir
是包含WEB-INF
目录(又包含web.xml
)的目录的路径。
回答by user738048
Try the following:
请尝试以下操作:
zip [zipfile] [file to update]
An example:
一个例子:
$ zip test.zip test/test.txt
updating: test/test.txt (stored 0%)
回答by traditional
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.
我知道这是个老问题,但我也想做同样的事情。更新 zip 存档中的文件。以上答案都没有真正帮助我。
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
这是我所做的。创建临时目录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
switch will look for changed/new files and will add to the zip archive.
-u
switch 将查找更改的/新文件并将添加到 zip 存档中。
回答by Ethan Strider
Use the update flag: -u
使用更新标志: -u
Example:
例子:
zip -ur existing.zip myFolder
zip -ur existing.zip myFolder
This command will compress and add myFolder
(and it's contents) to the existing.zip
.
此命令将压缩并添加myFolder
(及其内容)到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.
因此,如果您想添加/更新 zip 文件中的特定子目录,只需根据需要更新源,然后使用-u
标志重新压缩整个源。只有更改过的文件才会被压缩。
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.
如果您无权访问源文件,则可以解压缩 zip 文件,然后更新所需的文件,然后使用该-u
标志重新压缩。同样,只有更改的文件才会被压缩。
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%)
回答by Renato Luiz Ramos
You can use: zip -u file.zip path/file_to_update
您可以使用:zip -u file.zip path/file_to_update
回答by gvrocha
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).
还有 -f 选项可以刷新 zip 文件。它可用于更新自生成 zip 以来已更新的所有文件(假设它们位于 zip 文件内树结构中的相同位置)。
If your file is named /myfiles/myzip.zip all you have to do is
如果您的文件名为 /myfiles/myzip.zip,那么您所要做的就是
zip -f /myfiles/myzip.zip
回答by mzsolt
7zip
(7za) can be used for adding/updating files/directories nicely:
7zip
(7za) 可用于很好地添加/更新文件/目录:
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):
示例:替换(不管文件日期)MANIFEST.MF
JAR 文件中的文件。该/source/META-INF
目录包含MANIFEST.MF
要放入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/
回答by Nickolay Olshevsky
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.
从 ZIP 存档结构的角度来看 - 您可以更新 zip 文件而无需重新压缩它,您只需要跳过需要替换的文件之前的所有文件,然后放置更新的文件,然后放置其余文件。最后,您需要放置更新后的中心目录结构。但是,我怀疑大多数常用工具是否能让您做到这一点。
回答by The Surrican
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
在基于 linux 的系统上,只需安装 zip,您就可以在命令行中调用它。看看手册页: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.
但根据我的个人经验,如果可能并且压缩不是那么重要,这对普通 tar 文件和 tar 文件效果更好。