This repository has been archived by the owner on Jun 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mike Brevoort
committed
Jul 28, 2015
1 parent
047ffd0
commit 8d0755f
Showing
4 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package util | ||
|
||
import ( | ||
"bytes" | ||
"compress/flate" | ||
"io" | ||
"strings" | ||
) | ||
|
||
func CompressString(in string) string { | ||
buf := new(bytes.Buffer) | ||
compressor, _ := flate.NewWriter(buf, 9) | ||
compressor.Write([]byte(in)) | ||
compressor.Close() | ||
return buf.String() | ||
} | ||
|
||
func DecompressString(in string) string { | ||
buf := new(bytes.Buffer) | ||
decompressor := flate.NewReader(strings.NewReader(in)) | ||
io.Copy(buf, decompressor) | ||
decompressor.Close() | ||
return buf.String() | ||
} | ||
|
||
func Compress(in []byte) []byte { | ||
buf := new(bytes.Buffer) | ||
compressor, _ := flate.NewWriter(buf, 9) | ||
compressor.Write(in) | ||
compressor.Close() | ||
return buf.Bytes() | ||
} | ||
|
||
func Decompress(in []byte) []byte { | ||
buf := new(bytes.Buffer) | ||
decompressor := flate.NewReader(bytes.NewReader(in)) | ||
io.Copy(buf, decompressor) | ||
decompressor.Close() | ||
return buf.Bytes() | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package util | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCompressString(t *testing.T) { | ||
expected := "This is the test string" | ||
compressed := CompressString(expected) | ||
decompressed := DecompressString(compressed) | ||
assert.Equal(t, expected, decompressed) | ||
assert.True(t, len(compressed) > len(decompressed)) | ||
} | ||
|
||
func TestCompress(t *testing.T) { | ||
expected := []byte("This is the test string") | ||
compressed := Compress(expected) | ||
decompressed := Decompress(compressed) | ||
assert.Equal(t, expected, decompressed) | ||
assert.True(t, len(compressed) > len(decompressed)) | ||
} |