From bfdacd00b69dbf09961fa2f440711c1d394c7c4e Mon Sep 17 00:00:00 2001 From: qydysky Date: Tue, 28 Jul 2020 06:36:45 +0800 Subject: [PATCH] 9 --- Json.go | 19 +++++++++++++++++++ Lock.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ Zip.go | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- go.mod | 5 ++++- go.sum | 3 +++ 5 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 Json.go create mode 100644 Lock.go diff --git a/Json.go b/Json.go new file mode 100644 index 0000000..b07669f --- /dev/null +++ b/Json.go @@ -0,0 +1,19 @@ +package part + +import ( + "github.com/thedevsaddam/gojsonq" +) + +type json struct {} + +func Json() (*json) {return &json{}} + +func (*json) GetValFrom(file,key string)interface {}{ + var jq *gojsonq.JSONQ + if Checkfile().IsExist(file) { + jq = gojsonq.New().File(file) + }else{ + jq = gojsonq.New().FromString(file) + } + return jq.Find(key) +} \ No newline at end of file diff --git a/Lock.go b/Lock.go new file mode 100644 index 0000000..c33dcef --- /dev/null +++ b/Lock.go @@ -0,0 +1,49 @@ +package part + +import ( + "os" + "sync" +) + +type lock struct { + sync.Mutex +} + +var ( + lock_file *os.File + lock_md5Key string +) + +func Lock() *lock { + return &lock{} +} + +func (l *lock) Start(filePath string) int { + l.Lock() + defer l.Unlock() + + if l.State() {return 1} + if Checkfile().IsOpen(lock_md5Key) {return 2} + if !Checkfile().IsExist(filePath) {return 3} + + lock_md5Key = ".lock."+filePath + lock_file, _ = os.Create(lock_md5Key) + return 0 +} + +func (l *lock) Stop() int { + l.Lock() + defer l.Unlock() + + if !l.State() {return 1} + if !Checkfile().IsExist(lock_md5Key) {return 2} + + lock_file.Close() + os.Remove(lock_md5Key) + lock_md5Key = "" + return 0 +} + +func (*lock) State() bool { + return lock_md5Key != "" +} \ No newline at end of file diff --git a/Zip.go b/Zip.go index 288eaa3..a3db512 100644 --- a/Zip.go +++ b/Zip.go @@ -6,7 +6,9 @@ import ( "github.com/klauspost/compress/zip" "io" "path/filepath" - "strings" + "strings" + "bytes" + "time" ) type lzip struct {sync.Mutex} @@ -132,4 +134,50 @@ func (this *lzip) UnZip(zipFile string, destDir string) error { } return nil +} + +type rZip struct { + poit *zip.ReadCloser + buf map[string]*zip.File +} + +func RZip() *rZip {return &rZip{}} + +func (t *rZip) New(zipFile string) (error) { + t.buf = make(map[string]*zip.File) + + var err error + t.poit, err = zip.OpenReader(zipFile) + if err != nil {return err} + + for _, _f := range t.poit.File { + if _f.FileInfo().IsDir() {continue} + t.buf[_f.Name] = _f + } + + return nil +} + +func (t *rZip) Read(path string) (*bytes.Buffer,string,error) { + var timeLayoutStr = "2006-01-02 15:04:05" + var err error + + if f,ok := t.buf[path];ok { + if rc, err := f.Open();err == nil { + defer rc.Close(); + + buf := new(bytes.Buffer) + buf.ReadFrom(rc) + return buf,f.FileHeader.Modified.Format(timeLayoutStr),nil + } + return &bytes.Buffer{},time.Now().UTC().Format(timeLayoutStr),err + } + return &bytes.Buffer{},time.Now().UTC().Format(timeLayoutStr),nil +} + +func (t *rZip) Close() { + t.poit.Close() + for k := range t.buf { + delete(t.buf, k) + } } \ No newline at end of file diff --git a/go.mod b/go.mod index 4cb42a1..3cfaed9 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/qydysky/part go 1.14 -require github.com/klauspost/compress v1.10.10 +require ( + github.com/klauspost/compress v1.10.10 + github.com/thedevsaddam/gojsonq v2.3.0+incompatible +) diff --git a/go.sum b/go.sum index 9ff177b..b0ae02b 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,5 @@ github.com/klauspost/compress v1.10.10 h1:a/y8CglcM7gLGYmlbP/stPE5sR3hbhFRUjCBfd/0B3I= github.com/klauspost/compress v1.10.10/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= +github.com/thedevsaddam/gojsonq v1.9.1 h1:zQulEP43nwmq5EKrNWyIgJVbqDeMdC1qzXM/f5O15a0= +github.com/thedevsaddam/gojsonq v2.3.0+incompatible h1:i2lFTvGY4LvoZ2VUzedsFlRiyaWcJm3Uh6cQ9+HyQA8= +github.com/thedevsaddam/gojsonq v2.3.0+incompatible/go.mod h1:RBcQaITThgJAAYKH7FNp2onYodRz8URfsuEGpAch0NA= -- 2.39.2