fix issue in gres.UnpackContent; fix issue in gtime.NewFromTimeStamp

This commit is contained in:
John
2020-02-04 17:09:18 +08:00
parent 4c3af63076
commit 4d2b244319
8 changed files with 81 additions and 35 deletions

View File

@ -91,9 +91,18 @@ func Unpack(path string) ([]*File, error) {
// UnpackContent unpacks the content to []*File.
func UnpackContent(content string) ([]*File, error) {
data, err := gcompress.UnGzip(hexStrToBytes(content))
if err != nil {
return nil, err
var data []byte
var err error
if isHexStr(content) {
data, err = gcompress.UnGzip(hexStrToBytes(content))
if err != nil {
return nil, err
}
} else {
data, err = gcompress.UnGzip(gconv.UnsafeStrToBytes(content))
if err != nil {
return nil, err
}
}
reader, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
if err != nil {
@ -106,6 +115,21 @@ func UnpackContent(content string) ([]*File, error) {
return array, nil
}
// isHexStr checks and returns whether given content <s> is hex string.
// It returns true if <s> is hex string, or false if not.
func isHexStr(s string) bool {
var r bool
for i := 0; i < len(s); i++ {
r = (s[i] >= '0' && s[i] <= '9') ||
(s[i] >= 'a' && s[i] <= 'f') ||
(s[i] >= 'A' && s[i] <= 'F')
if !r {
return false
}
}
return true
}
// bytesToHexString converts binary content to hex string content.
func bytesToHexStr(b []byte) string {
dst := make([]byte, hex.EncodedLen(len(b)))

View File

@ -7,6 +7,7 @@
package gres_test
import (
"github.com/gogf/gf/os/gtime"
"strings"
"testing"
@ -17,7 +18,7 @@ import (
"github.com/gogf/gf/test/gtest"
)
func Test_Pack(t *testing.T) {
func Test_PackToGoFile(t *testing.T) {
gtest.Case(t, func() {
srcPath := gdebug.CallerDirectory() + "/testdata/files"
goFilePath := gdebug.CallerDirectory() + "/testdata/testdata.go"
@ -27,6 +28,35 @@ func Test_Pack(t *testing.T) {
})
}
func Test_Pack(t *testing.T) {
gtest.Case(t, func() {
srcPath := gdebug.CallerDirectory() + "/testdata/files"
data, err := gres.Pack(srcPath)
gtest.Assert(err, nil)
r := gres.New()
err = r.Add(string(data))
gtest.Assert(err, nil)
gtest.Assert(r.Contains("files"), true)
})
}
func Test_PackToFile(t *testing.T) {
gtest.Case(t, func() {
srcPath := gdebug.CallerDirectory() + "/testdata/files"
dstPath := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr())
err := gres.PackToFile(srcPath, dstPath)
gtest.Assert(err, nil)
defer gfile.Remove(dstPath)
r := gres.New()
err = r.Load(dstPath)
gtest.Assert(err, nil)
gtest.Assert(r.Contains("files"), true)
})
}
func Test_PackMulti(t *testing.T) {
gtest.Case(t, func() {
srcPath := gdebug.CallerDirectory() + "/testdata/files"

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long