Golang : Create zip/ePub file without compression(use Store algorithm)
Problem :
You need to create zip file without compression. i.e just use the Store algorithm. For example, you are trying to create ePub file, which only uses Store algorithm.
Solution :
By default, Golang's archive/zip
package uses the Store(no compression) algorithm. However, it is best not to assume that the default will always be the same after upgrade. To ensure that the zip/ePub file that are created with Store algorithm. Use the zip.CreateHeader()
function with the following line :
...
header, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
header.Method = zip.Store // <<<----- here
writer, err := zipit.CreateHeader(header)
if err != nil {
return err
}
or if you want to add more data to the header.
writer, err := zipwriter.CreateHeader(&zip.FileHeader{
Name: filename,
Method: zip.Store,
})
See full example on how to create zip file at : https://www.socketloop.com/tutorials/zip-compress-file-in-go
References :
https://www.socketloop.com/tutorials/zip-compress-file-in-go
See also : Golang : How to tell if a file is compressed either gzip or zip ?
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+8.3k Golang : Changing a RGBA image number of channels with OpenCV
+26k Golang : Record voice(audio) from microphone to .WAV file
+10k Golang : Proper way to test CIDR membership of an IP 4 or 6 address example
+20.5k Golang : Match strings by wildcard patterns with filepath.Match() function
+6.1k Javascript : How to get JSON data from another website with JQuery or Ajax ?
+21.6k Golang : Calculate time different
+8.9k Golang : Convert octal value to string to deal with leading zero problem
+5k Golang : Calculate US Dollar Index (DXY)
+14.9k Golang : Get file permission
+5.6k Golang : Derive cryptographic key from passwords with Argon2
+13.4k Golang : Save(pipe) HTTP response into a file
+18.9k Golang : How to get struct tag and use field name to retrieve data?