Created
August 8, 2018 12:26
-
-
Save DeadNumbers/b7b5c1e927e5fea3acceb31d4c4c66a7 to your computer and use it in GitHub Desktop.
Golang example for compress file with LZ4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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