更新Go中的Zip文件

huangapple go评论90阅读模式
英文:

Updating Zip file in Go

问题

我正在尝试在Go应用程序中实现zip的更新(-u标志),但是我找不到一种有效的方法来实现这一点,而不必重新编写整个zip文件。

当然,我可以在Go应用程序中调用zip -u,但这似乎不太合适。

我希望的是,我不需要重新编写整个zip文件来添加新文件或更新现有文件。同时,生成的存档文件需要进行压缩,所以我不能使用普通的tar格式。如果可以在Go中高效地实现这种行为,我也可以考虑使用其他压缩格式。

英文:

I'm trying to implement zip's update (-u flag) inside a Go application, but I can't find an efficient way of doing so without rewriting the whole zip file again.

Of course I could call zip -u from within the Go application but that doesn't seem appropriate.

What I expect is that I wouldn't need to rewrite the whole zip file to add new, or update existing files. It is also important that the resulting archive is compressed so I can't use normal tar. I'm open to using other compression formats if this behavior could be implemented efficiently in Go.

答案1

得分: 4

标准库的zip包不支持此操作。


如果对存档格式(如zip命令行工具)有足够高级的了解,可以通过替换存档的适当部分并更新目录(位于zip文件末尾),来更新zip存档中的文件,前提是更新后的文件(经过压缩)比原始文件(经过压缩)要小。

如果更新后的文件不比原始文件小,可以将新数据添加到存档的末尾,然后在额外数据之后重新编写目录;这也是添加新文件的方法。

如上所述,这不是标准库包原生支持的操作,因此您需要找到一个替代库,包装一个C zip库,或者执行zip工具。

有关文件格式的更多详细信息,请参阅Zip文件格式的维基百科条目

英文:

The standard library's zip package does not support this operation.


With sufficiently advanced knowledge of the archive format (like the zip command-line tool) it is possible to update files in a zip archive if the updated file (after compression) is smaller than the original file (after compression) by replacing the appropriate portion of the archive and updating the directory, which is at the end of the zipfile.

If it is not smaller, the new data can be added at the end of the archive and the directory would then be rewritten after the extra data; this is also how new files can be added.

As mentioned above, this is not an operation that is supported natively by the standard library package, so you would need to find an alternate library, wrap a C zip library, or exec out to the zip utility.

See the wikipedia entry for the Zip file format for more details about the file format.

huangapple
  • 本文由 发表于 2021年6月26日 04:21:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/68136688.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定