pkger/pkger.go

83 lines
1.3 KiB
Go
Raw Normal View History

2019-07-31 00:21:26 +03:00
package pkger
2019-08-02 07:22:17 +03:00
import (
"bytes"
"compress/gzip"
2019-08-03 01:14:48 +03:00
"encoding/hex"
2019-08-09 22:30:12 +03:00
"encoding/json"
2019-08-03 01:14:48 +03:00
"fmt"
2019-08-02 07:22:17 +03:00
"io"
"log"
)
func Unpack(ind string) error {
2019-08-03 01:14:48 +03:00
b, err := hex.DecodeString(ind)
2019-08-02 07:22:17 +03:00
if err != nil {
2019-08-03 01:14:48 +03:00
log.Fatal("hex.DecodeString", err)
2019-08-02 07:22:17 +03:00
return err
}
gz, err := gzip.NewReader(bytes.NewReader(b))
if err != nil {
2019-08-03 01:14:48 +03:00
log.Fatal("gzip.NewReader", err)
2019-08-02 07:22:17 +03:00
return err
}
defer gz.Close()
2019-08-09 22:30:12 +03:00
var jay jason
if err := json.NewDecoder(gz).Decode(&jay); err != nil {
return err
}
2019-08-02 07:22:17 +03:00
2019-08-09 22:30:12 +03:00
filesCache = jay.Files
infosCache = jay.Infos
pathsCache = jay.Paths
currentInfo = jay.CurrentInfo
2019-08-02 07:22:17 +03:00
return nil
}
func Pack(out io.Writer, paths []Path) error {
2019-08-03 01:14:48 +03:00
bb := &bytes.Buffer{}
gz := gzip.NewWriter(bb)
defer gz.Close()
2019-08-02 07:22:17 +03:00
for _, p := range paths {
f, err := Open(p.String())
if err != nil {
return err
}
2019-08-05 00:13:27 +03:00
fi, err := f.Stat()
if err != nil {
return err
}
if fi.IsDir() {
2019-08-09 05:35:01 +03:00
filesCache.Store(p, f)
2019-08-05 00:13:27 +03:00
f.Close()
continue
}
2019-08-09 05:35:01 +03:00
dubeg("Pack", "%s", p)
filesCache.Store(p, f)
2019-08-02 07:22:17 +03:00
f.Close()
2019-08-05 00:13:27 +03:00
2019-08-02 07:22:17 +03:00
}
2019-08-03 01:14:48 +03:00
2019-08-09 22:30:12 +03:00
jay := jason{
Files: filesCache,
Infos: infosCache,
Paths: pathsCache,
CurrentInfo: currentInfo,
}
if err := json.NewEncoder(gz).Encode(jay); err != nil {
return err
}
2019-08-03 01:14:48 +03:00
if err := gz.Close(); err != nil {
return err
}
s := hex.EncodeToString(bb.Bytes())
2019-08-05 00:13:27 +03:00
_, err := fmt.Fprint(out, s)
return err
2019-08-02 07:22:17 +03:00
}