]> 127.0.0.1 Git - part/.git/commitdiff
9
authorqydysky <qydysky@foxmail.com>
Mon, 27 Jul 2020 22:36:45 +0000 (06:36 +0800)
committerqydysky <qydysky@foxmail.com>
Mon, 27 Jul 2020 22:36:45 +0000 (06:36 +0800)
Json.go [new file with mode: 0644]
Lock.go [new file with mode: 0644]
Zip.go
go.mod
go.sum

diff --git a/Json.go b/Json.go
new file mode 100644 (file)
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 (file)
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 288eaa37caa1481fe59267bbea225d7e4f45a566..a3db51263a0844e24d28fe7245f58b761ed6d60d 100644 (file)
--- 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 4cb42a10743836596591762794de2de1c8b5ba98..3cfaed926f00dc3d5a637edc1ee0fece408b5822 100644 (file)
--- 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 9ff177b2ef41ddb7582d2e1330d426fe00b97263..b0ae02bf7bfb66743d44f1ca721c46576afa85a8 100644 (file)
--- 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=