compress.go 706 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package compress
  2. import (
  3. "bytes"
  4. "compress/zlib"
  5. "io"
  6. )
  7. var _ Compress = (*compress)(nil)
  8. type Compress interface {
  9. DoZlibCompress(src []byte) []byte
  10. DoZlibUnCompress(compressSrc []byte) []byte
  11. }
  12. type compress struct{}
  13. func New() Compress {
  14. return &compress{}
  15. }
  16. // DoZlibCompress 进行zlib压缩
  17. func (c *compress) DoZlibCompress(src []byte) []byte {
  18. var in bytes.Buffer
  19. w := zlib.NewWriter(&in)
  20. _, _ = w.Write(src)
  21. _ = w.Close()
  22. return in.Bytes()
  23. }
  24. // DoZlibUnCompress 进行zlib解压缩
  25. func (c *compress) DoZlibUnCompress(compressSrc []byte) []byte {
  26. b := bytes.NewReader(compressSrc)
  27. var out bytes.Buffer
  28. r, _ := zlib.NewReader(b)
  29. _, _ = io.Copy(&out, r)
  30. return out.Bytes()
  31. }