compress end

This commit is contained in:
snoop 2017-03-29 14:02:08 +09:00
parent 3b1f0a4ffc
commit b183cec51f
2 changed files with 112 additions and 2 deletions

View File

@ -1 +1,47 @@
package compression
import (
"bytes"
"compress/gzip"
"io/ioutil"
)
func CompressDataGzip(data []byte) []byte {
var b bytes.Buffer
w := gzip.NewWriter(&b)
w.Write(data)
w.Close()
return b.Bytes()
}
func CompressDataGzipLevel(data []byte, level int) ([]byte, error) {
var b bytes.Buffer
w := gzip.NewWriter(&b)
w, err := gzip.NewWriterLevel(&b, level)
if err != nil {
return nil, err
}
w.Write(data)
w.Close()
return b.Bytes(), nil
}
func UnCompressDataGzip(data []byte) ([]byte, error) {
ucb := bytes.NewReader(data[0:])
z, err := gzip.NewReader(ucb)
if err != nil {
return nil, err
}
p, err := ioutil.ReadAll(z)
if err != nil {
return nil, err
}
z.Close()
return p, nil
}

View File

@ -1,10 +1,74 @@
package compression
import (
"fmt"
"testing"
"time"
)
func TestCompress(t *testing.T) {
fmt.Println("aaaaaa")
jsonStr := "{ \"glossary\": { \"title\": \"example glossary\", \"GlossDiv\": { \"title\": \"S\", \"GlossList\": { \"GlossEntry\": { \"ID\": \"SGML\", \"SortAs\": \"SGML\", \"GlossTerm\": \"Standard Generalized Markup Language\", \"Acronym\": \"SGML\", \"Abbrev\": \"ISO 8879:1986\", \"GlossDef\": { \"para\": \"A meta-markup language, used to create markup languages such as DocBook.\", \"GlossSeeAlso\": [\"GML\", \"XML\"]},\"GlossSee\": \"markup\"}}}}}"
t.Logf("original length : %d \r\n", len(jsonStr))
start := time.Now()
bytes := CompressDataGzip([]byte(jsonStr))
elapsed := time.Since(start)
t.Logf("compress length : %d \r\n", len(bytes))
t.Log(elapsed)
}
func TestCompressLevel(t *testing.T) {
jsonStr := "{ \"glossary\": { \"title\": \"example glossary\", \"GlossDiv\": { \"title\": \"S\", \"GlossList\": { \"GlossEntry\": { \"ID\": \"SGML\", \"SortAs\": \"SGML\", \"GlossTerm\": \"Standard Generalized Markup Language\", \"Acronym\": \"SGML\", \"Abbrev\": \"ISO 8879:1986\", \"GlossDef\": { \"para\": \"A meta-markup language, used to create markup languages such as DocBook.\", \"GlossSeeAlso\": [\"GML\", \"XML\"]},\"GlossSee\": \"markup\"}}}}}"
level := 9
t.Logf("original length : %d \r\n", len(jsonStr))
start := time.Now()
bytes, _ := CompressDataGzipLevel([]byte(jsonStr), level)
elapsed := time.Since(start)
t.Logf("compress length : %d, level %d \r\n", len(bytes), level)
t.Log(elapsed)
}
func TestUnCompress(t *testing.T) {
jsonStr := "{ \"glossary\": { \"title\": \"example glossary\", \"GlossDiv\": { \"title\": \"S\", \"GlossList\": { \"GlossEntry\": { \"ID\": \"SGML\", \"SortAs\": \"SGML\", \"GlossTerm\": \"Standard Generalized Markup Language\", \"Acronym\": \"SGML\", \"Abbrev\": \"ISO 8879:1986\", \"GlossDef\": { \"para\": \"A meta-markup language, used to create markup languages such as DocBook.\", \"GlossSeeAlso\": [\"GML\", \"XML\"]},\"GlossSee\": \"markup\"}}}}}"
bytes := CompressDataGzip([]byte(jsonStr))
t.Logf("compress length : %d\r\n", len(bytes))
start := time.Now()
unComp, err := UnCompressDataGzip(bytes)
if err != nil {
t.Log(err)
return
}
elapsed := time.Since(start)
t.Logf("uncompress length : %d\r\n", len(unComp))
t.Log(elapsed)
}
func TestUnCompressLevel(t *testing.T) {
jsonStr := "{ \"glossary\": { \"title\": \"example glossary\", \"GlossDiv\": { \"title\": \"S\", \"GlossList\": { \"GlossEntry\": { \"ID\": \"SGML\", \"SortAs\": \"SGML\", \"GlossTerm\": \"Standard Generalized Markup Language\", \"Acronym\": \"SGML\", \"Abbrev\": \"ISO 8879:1986\", \"GlossDef\": { \"para\": \"A meta-markup language, used to create markup languages such as DocBook.\", \"GlossSeeAlso\": [\"GML\", \"XML\"]},\"GlossSee\": \"markup\"}}}}}"
level := 9
bytes, _ := CompressDataGzipLevel([]byte(jsonStr), level)
t.Logf("compress length : %d, level %d\r\n", len(bytes), level)
start := time.Now()
unComp, err := UnCompressDataGzip(bytes)
if err != nil {
t.Log(err)
return
}
elapsed := time.Since(start)
t.Logf("uncompress length : %d\r\n", len(unComp))
t.Log(elapsed)
}