Golang archive/zip.CreateHeader() function usage example

package archive/zip

CreateHeader adds a file to the zip file using the provided FileHeader for the file metadata. It returns a Writer to which the file contents should be written. The file's contents must be written to the io.Writer before the next call to Create, CreateHeader, or Close.

zip.CreateHeader() function usage example

 zipWriter := zip.NewWriter(buffer)
 fileInfo := file.Stat()
 header := zip.FileInfoHeader(fileInfo)
 writer : = zipWriter.CreateHeader(header)

NOTE : By the default the zip package will use algorithm 0 - which is Store, without any compression. Remember to add :

 header.Method = zip.Deflate

if you want to create smaller size zip files

Advertisement