Golang : Gzip file example
This tutorial is to fulfill a request from a student learning Golang in Senegal.
His question is how to gzip a file in Golang. Most of the online articles he found only show how to compress some bytes of text but not how to compress a file. This tutorial will show how to read an uncompress file content into a buffer, gzip the buffer and write out the buffer content into a new file.
Here we go :
package main
import (
"bufio"
"bytes"
"compress/gzip"
"flag"
"fmt"
"io/ioutil"
"os"
)
func main() {
flag.Parse() // get the arguments from command line
filename := flag.Arg(0)
if filename == "" {
fmt.Println("Usage : go-gzip sourcefile")
os.Exit(1)
}
rawfile, err := os.Open(filename)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer rawfile.Close()
// calculate the buffer size for rawfile
info, _ := rawfile.Stat()
var size int64 = info.Size()
rawbytes := make([]byte, size)
// read rawfile content into buffer
buffer := bufio.NewReader(rawfile)
_, err = buffer.Read(rawbytes)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
var buf bytes.Buffer
writer := gzip.NewWriter(&buf)
writer.Write(rawbytes)
writer.Close()
err = ioutil.WriteFile(filename+".gz", buf.Bytes(), info.Mode())
// use 0666 to replace info.Mode() if you prefer
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("%s compressed to %s\n", filename, filename + ".gz")
}
Sample tests result :
./go-gzip
Usage : go-gzip sourcefile
./go-gzip uncompressed.txt
uncompressed.txt compressed to uncompressed.txt.gz
References :
http://golang.org/pkg/compress/gzip/#NewWriter
https://www.socketloop.com/tutorials/golang-read-binary-file-into-memory
See also : Golang : Gunzip 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
+17.3k How to enable MariaDB/MySQL logs ?
+16.5k Golang : Fix cannot convert buffer (type *bytes.Buffer) to type string error
+21.4k Golang : Upload big file (larger than 100MB) to AWS S3 with multipart upload
+7.2k Golang : Gorrila set route name and get the current route name
+7.9k Android Studio : Rating bar example
+44.4k Golang : Use wildcard patterns with filepath.Glob() example
+9.8k Golang : Get escape characters \u form from unicode characters
+8k Golang : Auto-generate reply email with text/template package
+13.2k Golang : Get user input until a command or receive a word to stop
+16.7k Golang : Get input from keyboard
+12.4k Golang : Transform comma separated string to slice example
+48.2k Golang : Upload file from web browser to server