Golang : Zip file
Go's standard library provides support for several archive file formats. Zip format is the most common standard of compressing file. It is easy to create/compress zip file in Go.
The package you need to import is
archive/zip
and below is a simple Go program to compress file.
NOTE : The default compression level is 0(store), you have to change the compression algorithm to deflate(number 8). Otherwise, you will find that the created zip file size will be slightly larger!
go-zip.go
package main
import (
"archive/zip"
"flag"
"fmt"
"io"
"os"
)
// use RegisterCompressor, if you plan to use your own compression algorithm
// other that the standard algorithm 8(deflate)
// the init block is commentted out as it is not used in this example
//func init() {
// see https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT
// section 4.4.5 for the available compression methods...which are not implemented by Golang....
// only algorithm 0 and number 8(deflate) available
// var comp zip.Compressor
// zip.RegisterCompressor(999, comp)
//}
func main() {
flag.Parse()
filename := flag.Arg(0)
if filename == "" {
fmt.Println("Usage go-zip sourcefile")
os.Exit(1)
}
fmt.Printf("Zipping file. \n")
err := zipfile(filename)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Zipped to ", filename+".zip")
}
func zipfile(filename string) error {
newfile, err := os.Create(filename + ".zip")
if err != nil {
return err
}
defer newfile.Close()
zipit := zip.NewWriter(newfile)
defer zipit.Close()
zipfile, err := os.Open(filename)
if err != nil {
return err
}
defer zipfile.Close()
// get the file information
info, err := zipfile.Stat()
if err != nil {
return err
}
header, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
// default is Store 0(no compression!)
// forking important !!
// change to deflate
// see http://golang.org/pkg/archive/zip/#pkg-constants
header.Method = zip.Deflate
writer, err := zipit.CreateHeader(header)
if err != nil {
return err
}
_, err = io.Copy(writer, zipfile)
return err
}
test out the go-zip.go
by >go run go-zip.go [filename]
Reference:
See also : Golang : Unzip file
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
+5.9k Golang : How to get capacity of a slice or array?
+16.7k Golang : How to tell if a file is compressed either gzip or zip ?
+13.7k Golang : Check if a file exist or not
+21.9k Golang : How to read JPG(JPEG), GIF and PNG files ?
+13.7k Elastic Search : Mapping date format and sort by date
+8.6k Golang : Go as a script or running go with shebang/hashbang style
+5.9k Golang : Detect face in uploaded photo like GPlus
+33.2k Golang : How to check if slice or array is empty?
+5.1k Golang : fmt.Println prints out empty data from struct
+19.3k Golang : Determine if directory is empty with os.File.Readdir() function
+12.8k Golang : Convert(cast) int to int64
+5.7k Java : Human readable password generator