github.com/wanlay/gorm-dm8@v1.0.5/dmr/f.go (about) 1 /* 2 * Copyright (c) 2000-2018, 达梦数据库有限公司. 3 * All rights reserved. 4 */ 5 package dmr 6 7 import ( 8 "bytes" 9 "compress/zlib" 10 "github.com/golang/snappy" 11 ) 12 13 func Compress(srcBuffer *Dm_build_0, offset int, length int, compressID int) ([]byte, error) { 14 if compressID == Dm_build_683 { 15 return snappy.Encode(nil, srcBuffer.Dm_build_290(offset, length)), nil 16 } 17 return GzlibCompress(srcBuffer, offset, length) 18 } 19 20 func UnCompress(srcBytes []byte, compressID int) ([]byte, error) { 21 if compressID == Dm_build_683 { 22 return snappy.Decode(nil, srcBytes) 23 } 24 return GzlibUncompress(srcBytes) 25 } 26 27 func GzlibCompress(srcBuffer *Dm_build_0, offset int, length int) ([]byte, error) { 28 var ret bytes.Buffer 29 var w = zlib.NewWriter(&ret) 30 w.Write(srcBuffer.Dm_build_290(offset, length)) 31 w.Close() 32 return ret.Bytes(), nil 33 } 34 35 func GzlibUncompress(srcBytes []byte) ([]byte, error) { 36 var bytesBuf = new(bytes.Buffer) 37 r, err := zlib.NewReader(bytes.NewReader(srcBytes)) 38 if err != nil { 39 return nil, err 40 } 41 defer r.Close() 42 _, err = bytesBuf.ReadFrom(r) 43 if err != nil { 44 return nil, err 45 } 46 return bytesBuf.Bytes(), nil 47 }