gitee.com/sodacan/packr@v1.11.1/packr.go (about) 1 package packr 2 3 import ( 4 "bytes" 5 "compress/gzip" 6 "encoding/json" 7 "sync" 8 ) 9 10 var gil = &sync.Mutex{} 11 var data = map[string]map[string][]byte{} 12 13 // PackBytes packs bytes for a file into a box. 14 func PackBytes(box string, name string, bb []byte) { 15 gil.Lock() 16 defer gil.Unlock() 17 if _, ok := data[box]; !ok { 18 data[box] = map[string][]byte{} 19 } 20 data[box][name] = bb 21 } 22 23 // PackBytesGzip packets the gzipped compressed bytes into a box. 24 func PackBytesGzip(box string, name string, bb []byte) error { 25 var buf bytes.Buffer 26 w := gzip.NewWriter(&buf) 27 _, err := w.Write(bb) 28 if err != nil { 29 return err 30 } 31 err = w.Close() 32 if err != nil { 33 return err 34 } 35 PackBytes(box, name, buf.Bytes()) 36 return nil 37 } 38 39 // PackJSONBytes packs JSON encoded bytes for a file into a box. 40 func PackJSONBytes(box string, name string, jbb string) error { 41 var bb []byte 42 err := json.Unmarshal([]byte(jbb), &bb) 43 if err != nil { 44 return err 45 } 46 PackBytes(box, name, bb) 47 return nil 48 }