improve gzip feature for gcompress; add gzip compression for package gres

This commit is contained in:
John
2020-01-15 00:15:56 +08:00
parent 22af5be71f
commit edf2366296
13 changed files with 177 additions and 121 deletions

View File

@ -2,12 +2,23 @@ package main
import (
"fmt"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/os/gres"
)
func main() {
result, _ := g.View().ParseContent("姓名: ${.name}", g.Map{
"name": "<script>alert('john');</script>",
})
fmt.Println(result)
//buffer := bytes.NewBuffer(nil)
//buffer.WriteString("\x00")
//hex.Decode()
//if v, e := strconv.ParseInt(s[2:], 16, 64); e == nil {
// return v
//}
//s := "\x00"
//fmt.Println([]byte(s))
//return
err := gres.PackToGoFile(
"/Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf-cli/public",
"/Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/.example/other/config.go",
"main",
)
fmt.Println(err)
}

View File

@ -6,78 +6,3 @@
// Package gcompress provides kinds of compression algorithms for binary/bytes data.
package gcompress
import (
"bytes"
"compress/gzip"
"compress/zlib"
"io"
)
// Zlib compresses <data> with zlib algorithm.
func Zlib(data []byte) ([]byte, error) {
if data == nil || len(data) < 13 {
return data, nil
}
var in bytes.Buffer
var err error
w := zlib.NewWriter(&in)
if _, err = w.Write(data); err != nil {
return nil, err
}
if err = w.Close(); err != nil {
return in.Bytes(), err
}
return in.Bytes(), nil
}
// UnZlib decompresses <data> with zlib algorithm.
func UnZlib(data []byte) ([]byte, error) {
if data == nil || len(data) < 13 {
return data, nil
}
b := bytes.NewReader(data)
var out bytes.Buffer
var err error
r, err := zlib.NewReader(b)
if err != nil {
return nil, err
}
if _, err = io.Copy(&out, r); err != nil {
return nil, err
}
return out.Bytes(), nil
}
// Gzip compresses <data> with gzip algorithm.
func Gzip(data []byte) ([]byte, error) {
var buf bytes.Buffer
var err error
zip := gzip.NewWriter(&buf)
_, err = zip.Write(data)
if err != nil {
return nil, err
}
if err = zip.Close(); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// UnGzip decompresses <data> with gzip algorithm.
func UnGzip(data []byte) ([]byte, error) {
var buf bytes.Buffer
content := bytes.NewReader(data)
zipData, err := gzip.NewReader(content)
if err != nil {
return nil, err
}
if _, err = io.Copy(&buf, zipData); err != nil {
return nil, err
}
if err = zipData.Close(); err != nil {
return buf.Bytes(), err
}
return buf.Bytes(), nil
}

View File

@ -0,0 +1,55 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gcompress
import (
"bytes"
"compress/gzip"
"io"
)
// Gzip compresses <data> with gzip algorithm.
// The optional parameter <level> specifies the compression level from
// 1 to 9 which means from none to the best compression.
//
// Note that it returns error if given <level> is invalid.
func Gzip(data []byte, level ...int) ([]byte, error) {
var writer *gzip.Writer
var buf bytes.Buffer
var err error
if len(level) > 0 {
writer, err = gzip.NewWriterLevel(&buf, level[0])
if err != nil {
return nil, err
}
} else {
writer = gzip.NewWriter(&buf)
}
if _, err = writer.Write(data); err != nil {
return nil, err
}
if err = writer.Close(); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// UnGzip decompresses <data> with gzip algorithm.
func UnGzip(data []byte) ([]byte, error) {
var buf bytes.Buffer
reader, err := gzip.NewReader(bytes.NewReader(data))
if err != nil {
return nil, err
}
if _, err = io.Copy(&buf, reader); err != nil {
return nil, err
}
if err = reader.Close(); err != nil {
return buf.Bytes(), err
}
return buf.Bytes(), nil
}

View File

@ -24,7 +24,8 @@ import (
// ZipPath compresses <paths> to <dest> using zip compressing algorithm.
// The unnecessary parameter <prefix> indicates the path prefix for zip file.
//
// Note that parameter <paths> supports multiple paths join with ','.
// Note that the parameter <paths> can be either a directory or a file, which
// supports multiple paths join with ','.
func ZipPath(paths, dest string, prefix ...string) error {
writer, err := os.Create(dest)
if err != nil {
@ -37,7 +38,8 @@ func ZipPath(paths, dest string, prefix ...string) error {
// ZipPathWriter compresses <paths> to <writer> using zip compressing algorithm.
// The unnecessary parameter <prefix> indicates the path prefix for zip file.
//
// Note that parameter <paths> supports multiple paths join with ','.
// Note that the parameter <paths> can be either a directory or a file, which
// supports multiple paths join with ','.
func ZipPathWriter(paths string, writer io.Writer, prefix ...string) error {
zipWriter := zip.NewWriter(writer)
defer zipWriter.Close()
@ -51,13 +53,19 @@ func ZipPathWriter(paths string, writer io.Writer, prefix ...string) error {
}
func doZipPathWriter(path string, zipWriter *zip.Writer, prefix ...string) error {
var err error
var files []string
realPath, err := gfile.Search(path)
if err != nil {
return err
}
files, err := gfile.ScanDir(path, "*", true)
if err != nil {
return err
if gfile.IsDir(path) {
files, err = gfile.ScanDir(path, "*", true)
if err != nil {
return err
}
} else {
files = []string{path}
}
headerPrefix := ""
if len(prefix) > 0 && prefix[0] != "" {

View File

@ -0,0 +1,50 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// Package gcompress provides kinds of compression algorithms for binary/bytes data.
package gcompress
import (
"bytes"
"compress/zlib"
"io"
)
// Zlib compresses <data> with zlib algorithm.
func Zlib(data []byte) ([]byte, error) {
if data == nil || len(data) < 13 {
return data, nil
}
var in bytes.Buffer
var err error
w := zlib.NewWriter(&in)
if _, err = w.Write(data); err != nil {
return nil, err
}
if err = w.Close(); err != nil {
return in.Bytes(), err
}
return in.Bytes(), nil
}
// UnZlib decompresses <data> with zlib algorithm.
func UnZlib(data []byte) ([]byte, error) {
if data == nil || len(data) < 13 {
return data, nil
}
b := bytes.NewReader(data)
var out bytes.Buffer
var err error
r, err := zlib.NewReader(b)
if err != nil {
return nil, err
}
if _, err = io.Copy(&out, r); err != nil {
return nil, err
}
return out.Bytes(), nil
}

View File

@ -1,23 +0,0 @@
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// Package utibytes provides some bytes functions for internal usage.
package utilbytes
import (
"bytes"
"fmt"
)
func Export(b []byte) string {
buffer := bytes.NewBuffer(nil)
buffer.WriteString(`[]byte("`)
for _, v := range b {
fmt.Fprintf(buffer, `\x%02x`, v)
}
buffer.WriteString(`")`)
return buffer.String()
}

View File

@ -171,7 +171,7 @@ func (p *Parser) GetOpt(name string, def ...string) string {
}
// GetOptVar returns the option value named <name> as *gvar.Var.
func (p *Parser) GetOptVar(name string, def ...string) *gvar.Var {
func (p *Parser) GetOptVar(name string, def ...interface{}) *gvar.Var {
return gvar.New(p.GetOpt(name, def...))
}

View File

@ -20,7 +20,7 @@ var (
// Add unpacks and adds the <content> into the default resource object.
// The unnecessary parameter <prefix> indicates the prefix
// for each file storing into current resource object.
func Add(content []byte, prefix ...string) error {
func Add(content string, prefix ...string) error {
return defaultResource.Add(content, prefix...)
}

View File

@ -9,10 +9,11 @@ package gres
import (
"archive/zip"
"bytes"
"encoding/hex"
"fmt"
"github.com/gogf/gf/util/gconv"
"github.com/gogf/gf/encoding/gcompress"
"github.com/gogf/gf/internal/utilbytes"
"github.com/gogf/gf/os/gfile"
)
@ -22,7 +23,7 @@ const (
import "github.com/gogf/gf/os/gres"
func init() {
if err := gres.Add(%s); err != nil {
if err := gres.Add("%s"); err != nil {
panic(err)
}
}
@ -44,7 +45,8 @@ func Pack(srcPaths string, keyPrefix ...string) ([]byte, error) {
if err != nil {
return nil, err
}
return buffer.Bytes(), nil
// Gzip the data bytes to reduce the size.
return gcompress.Gzip(buffer.Bytes(), 9)
}
// PackToFile packs the path specified by <srcPaths> to target file <dstPath>.
@ -73,7 +75,8 @@ func PackToGoFile(srcPath, goFilePath, pkgName string, keyPrefix ...string) erro
return err
}
return gfile.PutContents(
goFilePath, fmt.Sprintf(gPACKAGE_TEMPLATE, pkgName, utilbytes.Export(data)),
goFilePath,
fmt.Sprintf(gPACKAGE_TEMPLATE, pkgName, bytesToHexStr(data)),
)
}
@ -83,12 +86,16 @@ func Unpack(path string) ([]*File, error) {
if err != nil {
return nil, err
}
return UnpackContent(gfile.GetBytes(realPath))
return UnpackContent(gfile.GetContents(realPath))
}
// UnpackContent unpacks the content to []*File.
func UnpackContent(content []byte) ([]*File, error) {
reader, err := zip.NewReader(bytes.NewReader(content), int64(len(content)))
func UnpackContent(content string) ([]*File, error) {
data, err := gcompress.UnGzip(hexStrToBytes(content))
if err != nil {
return nil, err
}
reader, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
if err != nil {
return nil, err
}
@ -98,3 +105,18 @@ func UnpackContent(content []byte) ([]*File, error) {
}
return array, nil
}
// bytesToHexString converts binary content to hex string content.
func bytesToHexStr(b []byte) string {
dst := make([]byte, hex.EncodedLen(len(b)))
hex.Encode(dst, b)
return gconv.UnsafeBytesToStr(dst)
}
// hexStrToBytes converts hex string content to []byte.
func hexStrToBytes(s string) []byte {
src := gconv.UnsafeStrToBytes(s)
dst := make([]byte, hex.DecodedLen(len(src)))
hex.Decode(dst, src)
return dst
}

View File

@ -38,7 +38,7 @@ func New() *Resource {
// Add unpacks and adds the <content> into current resource object.
// The unnecessary parameter <prefix> indicates the prefix
// for each file storing into current resource object.
func (r *Resource) Add(content []byte, prefix ...string) error {
func (r *Resource) Add(content string, prefix ...string) error {
files, err := UnpackContent(content)
if err != nil {
return err
@ -62,7 +62,7 @@ func (r *Resource) Load(path string, prefix ...string) error {
if err != nil {
return err
}
return r.Add(gfile.GetBytes(realPath), prefix...)
return r.Add(gfile.GetContents(realPath), prefix...)
}
// Get returns the file with given path.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -47,6 +47,14 @@ func Test_NewFromStrFormat(t *testing.T) {
t.Error("test fail")
}
})
gtest.Case(t, func() {
t1 := gtime.NewFromStrFormat("2019/2/1", "Y/n/j")
gtest.Assert(t1.Format("Y-m-d"), "2019-02-01")
t2 := gtime.NewFromStrFormat("2019/10/12", "Y/n/j")
gtest.Assert(t2.Format("Y-m-d"), "2019-10-12")
})
}
func Test_NewFromStrLayout(t *testing.T) {