Skip to content

Instantly share code, notes, and snippets.

@eliquious
Created January 4, 2016 05:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliquious/980d556a03078444a97c to your computer and use it in GitHub Desktop.
Save eliquious/980d556a03078444a97c to your computer and use it in GitHub Desktop.
Golang LZ4 compression and decompression

Building

go build -o lz4 main.go

Compressing

cat file.txt | ./lz4 compress > file.txt.lz4

Decompressing

cat file.txt.lz4 | ./lz4 decompress > file.txt.bak
package main
import (
"bufio"
"io"
"os"
"github.com/pierrec/lz4"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
var (
app = kingpin.New("lz4", "A command line tool for compressing files")
compressCmd = app.Command("compress", "Compress stdin")
decompressCmd = app.Command("decompress", "Decompress stdin")
)
func main() {
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
case compressCmd.FullCommand():
compress()
case decompressCmd.FullCommand():
decompress()
}
}
func compress() {
bufWriter := bufio.NewWriter(os.Stdout)
writer := lz4.NewWriter(bufWriter)
writer.Header = lz4.Header{
BlockDependency: false,
BlockChecksum: true,
BlockMaxSize: 4 << 14,
NoChecksum: false,
HighCompression: true,
}
io.Copy(writer, bufio.NewReaderSize(os.Stdin, 4*1024*1024))
bufWriter.Flush()
writer.Close()
}
func decompress() {
io.Copy(os.Stdout, lz4.NewReader(os.Stdin))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment