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
+6.1k Golang : Detect face in uploaded photo like GPlus
+10k Golang : cannot assign type int to value (type uint8) in range error
+7.2k Golang : Dealing with struct's private part
+7.9k Android Studio : Rating bar example
+5.3k PHP : Fix Call to undefined function curl_init() error
+32.3k Golang : Regular Expression for alphanumeric and underscore
+10.1k Golang : Meaning of omitempty in struct's field tag
+11k Use systeminfo to find out installed Windows Hotfix(s) or updates
+5.8k Fix ERROR 2003 (HY000): Can't connect to MySQL server on 'IP address' (111)
+8.4k Golang : How to join strings?
+4.8k Linux/Unix/MacOSX : Find out which application is listening to port 80 or use which IP version
+34.1k Golang : Smarter Error Handling with strings.Contains()