不解压缩归档文件即可编辑文件

4

如何在不解压缩归档文件的情况下编辑文件?原因是我正在编写自动化任务,我可以解压缩、编辑文件并重新压缩它,但如果我能在运行时完成它,那将节省解压缩/压缩的时间。


重复的问题。你不能这样做,请参见下面链接:http://stackoverflow.com/questions/6334037/how-to-change-a-file-inside-an-archive-ear-file-without-extracting-entire-fil - Boris Pavlovic
1个回答

4

zip命令手册提供了一个-u选项来更新zip压缩包。您可以按如下方式使用:

zip -u bigzip.zip file/to/update1 file/to/update2 ...

虽然不是瞬间完成,但处理速度会更快。如果我创建一个大小为200MB的zip文件作为样例:

mkdir source
for (( f = 0; f < 200; f++ )); do
    head -c 1000000 /dev/random > source/${f}
done
zip -0r bigzip.zip source

解压、编辑一个文件,再重新压缩需要大约9秒钟在我的机器上完成。
unzip bigzip.zip
head -c 1000000 /dev/random > source/3
zip -0r bigzip.zip source

但只需要约3秒钟就可以调用zip -u

mkdir source
head -c1000000 /dev/random > source/3
zip -u bigzip.zip source/3

这个脚本 zip -u file.zip path-to-file-1 path-to-file-2 ... 在我使用 Python3 时无法工作。我需要遵循什么技巧吗? - Afshin Oroojlooy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接