Reputation: 380
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.
Upvotes: 0
Views: 1014
Reputation: 4756
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.
Upvotes: 4