Created
July 4, 2015 09:04
-
-
Save 1lann/77a2c39fe56e30face0a to your computer and use it in GitHub Desktop.
LZ compression in Go
This file contains hidden or 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 compress | |
import ( | |
"bytes" | |
"compress/lzw" | |
"io/ioutil" | |
) | |
func compress(input []byte) ([]byte, error) { | |
output := &bytes.Buffer{} | |
compressor := lzw.NewWriter(output, lzw.LSB, 8) | |
defer compressor.Close() | |
_, err := compressor.Write(input) | |
if err != nil { | |
return []byte{}, err | |
} | |
compressor.Close() | |
return output.Bytes(), nil | |
} | |
func decompress(input []byte) ([]byte, error) { | |
inputBuf := bytes.NewReader(input) | |
decompressor := lzw.NewReader(inputBuf, lzw.LSB, 8) | |
defer decompressor.Close() | |
output, err := ioutil.ReadAll(decompressor) | |
if err != nil { | |
return output, err | |
} | |
return output, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment