Skip to content

Instantly share code, notes, and snippets.

@DeadNumbers
Created August 8, 2018 12:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DeadNumbers/b7b5c1e927e5fea3acceb31d4c4c66a7 to your computer and use it in GitHub Desktop.
Save DeadNumbers/b7b5c1e927e5fea3acceb31d4c4c66a7 to your computer and use it in GitHub Desktop.
Golang example for compress file with LZ4
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"runtime"
"time"
"github.com/pierrec/lz4"
)
func init() {
runtime.GOMAXPROCS(runtime.NumCPU())
}
func main() {
start := time.Now()
err := compressLZ4(os.Args[1])
if err != nil {
log.Fatal(err)
}
fmt.Printf("LZ4 time is: %v\n", time.Since(start))
}
func compressLZ4(filename string) error {
data, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatal(err)
}
var buff bytes.Buffer
w := lz4.NewWriter(&buff)
w.Write(data)
w.Close()
if err != nil {
return err
}
return ioutil.WriteFile("/tmp/test.lz4", buff.Bytes(), 0644)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment