mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
improve package gcompress
This commit is contained in:
39
encoding/gcompress/gcompress_z_unit_gzip_test.go
Normal file
39
encoding/gcompress/gcompress_z_unit_gzip_test.go
Normal file
@ -0,0 +1,39 @@
|
||||
// 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_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/encoding/gcompress"
|
||||
"github.com/gogf/gf/test/gtest"
|
||||
)
|
||||
|
||||
func Test_Gzip_UnGzip(t *testing.T) {
|
||||
src := "Hello World!!"
|
||||
|
||||
gzip := []byte{
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0xff,
|
||||
0xf2, 0x48, 0xcd, 0xc9, 0xc9,
|
||||
0x57, 0x08, 0xcf, 0x2f, 0xca,
|
||||
0x49, 0x51, 0x54, 0x04, 0x04,
|
||||
0x00, 0x00, 0xff, 0xff, 0x9d,
|
||||
0x24, 0xa8, 0xd1, 0x0d, 0x00,
|
||||
0x00, 0x00,
|
||||
}
|
||||
|
||||
arr := []byte(src)
|
||||
data, _ := gcompress.Gzip(arr)
|
||||
gtest.Assert(data, gzip)
|
||||
|
||||
data, _ = gcompress.UnGzip(gzip)
|
||||
gtest.Assert(data, arr)
|
||||
|
||||
data, _ = gcompress.UnGzip(gzip[1:])
|
||||
gtest.Assert(data, nil)
|
||||
}
|
||||
153
encoding/gcompress/gcompress_z_unit_zip_test.go
Normal file
153
encoding/gcompress/gcompress_z_unit_zip_test.go
Normal file
@ -0,0 +1,153 @@
|
||||
// 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_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/gogf/gf/debug/gdebug"
|
||||
"github.com/gogf/gf/encoding/gcompress"
|
||||
"github.com/gogf/gf/os/gfile"
|
||||
"github.com/gogf/gf/os/gtime"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/test/gtest"
|
||||
)
|
||||
|
||||
func Test_ZipPath(t *testing.T) {
|
||||
// file
|
||||
gtest.Case(t, func() {
|
||||
srcPath := gfile.Join(gdebug.CallerDirectory(), "testdata", "zip", "path1", "1.txt")
|
||||
dstPath := gfile.Join(gdebug.CallerDirectory(), "testdata", "zip", "zip.zip")
|
||||
|
||||
gtest.Assert(gfile.Exists(dstPath), false)
|
||||
err := gcompress.ZipPath(srcPath, dstPath)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(gfile.Exists(dstPath), true)
|
||||
defer gfile.Remove(dstPath)
|
||||
|
||||
tempDirPath := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr())
|
||||
err = gfile.Mkdir(tempDirPath)
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
err = gcompress.UnZipFile(dstPath, tempDirPath)
|
||||
gtest.Assert(err, nil)
|
||||
defer gfile.Remove(tempDirPath)
|
||||
|
||||
gtest.Assert(
|
||||
gfile.GetContents(gfile.Join(tempDirPath, "1.txt")),
|
||||
gfile.GetContents(gfile.Join(srcPath, "path1", "1.txt")),
|
||||
)
|
||||
})
|
||||
// directory
|
||||
gtest.Case(t, func() {
|
||||
srcPath := gfile.Join(gdebug.CallerDirectory(), "testdata", "zip")
|
||||
dstPath := gfile.Join(gdebug.CallerDirectory(), "testdata", "zip", "zip.zip")
|
||||
|
||||
pwd := gfile.Pwd()
|
||||
err := gfile.Chdir(srcPath)
|
||||
defer gfile.Chdir(pwd)
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
gtest.Assert(gfile.Exists(dstPath), false)
|
||||
err = gcompress.ZipPath(srcPath, dstPath)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(gfile.Exists(dstPath), true)
|
||||
defer gfile.Remove(dstPath)
|
||||
|
||||
tempDirPath := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr())
|
||||
err = gfile.Mkdir(tempDirPath)
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
err = gcompress.UnZipFile(dstPath, tempDirPath)
|
||||
gtest.Assert(err, nil)
|
||||
defer gfile.Remove(tempDirPath)
|
||||
|
||||
gtest.Assert(
|
||||
gfile.GetContents(gfile.Join(tempDirPath, "zip", "path1", "1.txt")),
|
||||
gfile.GetContents(gfile.Join(srcPath, "path1", "1.txt")),
|
||||
)
|
||||
gtest.Assert(
|
||||
gfile.GetContents(gfile.Join(tempDirPath, "zip", "path2", "2.txt")),
|
||||
gfile.GetContents(gfile.Join(srcPath, "path2", "2.txt")),
|
||||
)
|
||||
})
|
||||
// multiple paths joined using char ','
|
||||
gtest.Case(t, func() {
|
||||
srcPath := gfile.Join(gdebug.CallerDirectory(), "testdata", "zip")
|
||||
srcPath1 := gfile.Join(gdebug.CallerDirectory(), "testdata", "zip", "path1")
|
||||
srcPath2 := gfile.Join(gdebug.CallerDirectory(), "testdata", "zip", "path2")
|
||||
dstPath := gfile.Join(gdebug.CallerDirectory(), "testdata", "zip", "zip.zip")
|
||||
|
||||
pwd := gfile.Pwd()
|
||||
err := gfile.Chdir(srcPath)
|
||||
defer gfile.Chdir(pwd)
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
gtest.Assert(gfile.Exists(dstPath), false)
|
||||
err = gcompress.ZipPath(srcPath1+", "+srcPath2, dstPath)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(gfile.Exists(dstPath), true)
|
||||
defer gfile.Remove(dstPath)
|
||||
|
||||
tempDirPath := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr())
|
||||
err = gfile.Mkdir(tempDirPath)
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
zipContent := gfile.GetBytes(dstPath)
|
||||
gtest.AssertGT(len(zipContent), 0)
|
||||
err = gcompress.UnZipContent(zipContent, tempDirPath)
|
||||
gtest.Assert(err, nil)
|
||||
defer gfile.Remove(tempDirPath)
|
||||
|
||||
gtest.Assert(
|
||||
gfile.GetContents(gfile.Join(tempDirPath, "path1", "1.txt")),
|
||||
gfile.GetContents(gfile.Join(srcPath, "path1", "1.txt")),
|
||||
)
|
||||
gtest.Assert(
|
||||
gfile.GetContents(gfile.Join(tempDirPath, "path2", "2.txt")),
|
||||
gfile.GetContents(gfile.Join(srcPath, "path2", "2.txt")),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_ZipPathWriter(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
srcPath := gfile.Join(gdebug.CallerDirectory(), "testdata", "zip")
|
||||
srcPath1 := gfile.Join(gdebug.CallerDirectory(), "testdata", "zip", "path1")
|
||||
srcPath2 := gfile.Join(gdebug.CallerDirectory(), "testdata", "zip", "path2")
|
||||
|
||||
pwd := gfile.Pwd()
|
||||
err := gfile.Chdir(srcPath)
|
||||
defer gfile.Chdir(pwd)
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
writer := bytes.NewBuffer(nil)
|
||||
gtest.Assert(writer.Len(), 0)
|
||||
err = gcompress.ZipPathWriter(srcPath1+", "+srcPath2, writer)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.AssertGT(writer.Len(), 0)
|
||||
|
||||
tempDirPath := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr())
|
||||
err = gfile.Mkdir(tempDirPath)
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
zipContent := writer.Bytes()
|
||||
gtest.AssertGT(len(zipContent), 0)
|
||||
err = gcompress.UnZipContent(zipContent, tempDirPath)
|
||||
gtest.Assert(err, nil)
|
||||
defer gfile.Remove(tempDirPath)
|
||||
|
||||
gtest.Assert(
|
||||
gfile.GetContents(gfile.Join(tempDirPath, "path1", "1.txt")),
|
||||
gfile.GetContents(gfile.Join(srcPath, "path1", "1.txt")),
|
||||
)
|
||||
gtest.Assert(
|
||||
gfile.GetContents(gfile.Join(tempDirPath, "path2", "2.txt")),
|
||||
gfile.GetContents(gfile.Join(srcPath, "path2", "2.txt")),
|
||||
)
|
||||
})
|
||||
}
|
||||
@ -13,7 +13,7 @@ import (
|
||||
"github.com/gogf/gf/test/gtest"
|
||||
)
|
||||
|
||||
func TestZlib(t *testing.T) {
|
||||
func Test_Zlib_UnZlib(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
src := "hello, world\n"
|
||||
dst := []byte{120, 156, 202, 72, 205, 201, 201, 215, 81, 40, 207, 47, 202, 73, 225, 2, 4, 0, 0, 255, 255, 33, 231, 4, 147}
|
||||
@ -31,30 +31,4 @@ func TestZlib(t *testing.T) {
|
||||
data, _ = gcompress.UnZlib(dst[1:])
|
||||
gtest.Assert(data, nil)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func TestGzip(t *testing.T) {
|
||||
src := "Hello World!!"
|
||||
|
||||
gzip := []byte{
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0xff,
|
||||
0xf2, 0x48, 0xcd, 0xc9, 0xc9,
|
||||
0x57, 0x08, 0xcf, 0x2f, 0xca,
|
||||
0x49, 0x51, 0x54, 0x04, 0x04,
|
||||
0x00, 0x00, 0xff, 0xff, 0x9d,
|
||||
0x24, 0xa8, 0xd1, 0x0d, 0x00,
|
||||
0x00, 0x00,
|
||||
}
|
||||
|
||||
arr := []byte(src)
|
||||
data, _ := gcompress.Gzip(arr)
|
||||
gtest.Assert(data, gzip)
|
||||
|
||||
data, _ = gcompress.UnGzip(gzip)
|
||||
gtest.Assert(data, arr)
|
||||
|
||||
data, _ = gcompress.UnGzip(gzip[1:])
|
||||
gtest.Assert(data, nil)
|
||||
}
|
||||
@ -9,6 +9,7 @@ package gcompress
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"github.com/gogf/gf/internal/intlog"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@ -32,7 +33,15 @@ func ZipPath(paths, dest string, prefix ...string) error {
|
||||
return err
|
||||
}
|
||||
defer writer.Close()
|
||||
return ZipPathWriter(paths, writer, prefix...)
|
||||
zipWriter := zip.NewWriter(writer)
|
||||
defer zipWriter.Close()
|
||||
for _, path := range strings.Split(paths, ",") {
|
||||
path = strings.TrimSpace(path)
|
||||
if err := doZipPathWriter(path, gfile.RealPath(dest), zipWriter, prefix...); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ZipPathWriter compresses <paths> to <writer> using zip compressing algorithm.
|
||||
@ -45,17 +54,21 @@ func ZipPathWriter(paths string, writer io.Writer, prefix ...string) error {
|
||||
defer zipWriter.Close()
|
||||
for _, path := range strings.Split(paths, ",") {
|
||||
path = strings.TrimSpace(path)
|
||||
if err := doZipPathWriter(path, zipWriter, prefix...); err != nil {
|
||||
if err := doZipPathWriter(path, "", zipWriter, prefix...); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func doZipPathWriter(path string, zipWriter *zip.Writer, prefix ...string) error {
|
||||
// doZipPathWriter compresses the file of given <path> and writes the content to <zipWriter>.
|
||||
// The parameter <exclude> specifies the exclusive file path that is not compressed to <zipWriter>,
|
||||
// commonly the destination zip file path.
|
||||
// The unnecessary parameter <prefix> indicates the path prefix for zip file.
|
||||
func doZipPathWriter(path string, exclude string, zipWriter *zip.Writer, prefix ...string) error {
|
||||
var err error
|
||||
var files []string
|
||||
realPath, err := gfile.Search(path)
|
||||
path, err = gfile.Search(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -80,7 +93,11 @@ func doZipPathWriter(path string, zipWriter *zip.Writer, prefix ...string) error
|
||||
}
|
||||
headerPrefix = strings.Replace(headerPrefix, "//", "/", -1)
|
||||
for _, file := range files {
|
||||
err := zipFile(file, headerPrefix+gfile.Dir(file[len(realPath):]), zipWriter)
|
||||
if exclude == file {
|
||||
intlog.Printf(`exclude file path: %s`, file)
|
||||
continue
|
||||
}
|
||||
err := zipFile(file, headerPrefix+gfile.Dir(file[len(path):]), zipWriter)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -101,10 +118,10 @@ func doZipPathWriter(path string, zipWriter *zip.Writer, prefix ...string) error
|
||||
}
|
||||
|
||||
// UnZipFile decompresses <archive> to <dest> using zip compressing algorithm.
|
||||
// The parameter <path> specifies the unzipped path of <archive>,
|
||||
// The optional parameter <path> specifies the unzipped path of <archive>,
|
||||
// which can be used to specify part of the archive file to unzip.
|
||||
//
|
||||
// Note thate the parameter <dest> should be a directory.
|
||||
// Note that the parameter <dest> should be a directory.
|
||||
func UnZipFile(archive, dest string, path ...string) error {
|
||||
readerCloser, err := zip.OpenReader(archive)
|
||||
if err != nil {
|
||||
@ -118,7 +135,7 @@ func UnZipFile(archive, dest string, path ...string) error {
|
||||
// The parameter <path> specifies the unzipped path of <archive>,
|
||||
// which can be used to specify part of the archive file to unzip.
|
||||
//
|
||||
// Note thate the parameter <dest> should be a directory.
|
||||
// Note that the parameter <dest> should be a directory.
|
||||
func UnZipContent(data []byte, dest string, path ...string) error {
|
||||
reader, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
|
||||
if err != nil {
|
||||
@ -178,6 +195,8 @@ func unZipFileWithReader(reader *zip.Reader, dest string, path ...string) error
|
||||
return nil
|
||||
}
|
||||
|
||||
// zipFile compresses the file of given <path> and writes the content to <zw>.
|
||||
// The parameter <prefix> indicates the path prefix for zip file.
|
||||
func zipFile(path string, prefix string, zw *zip.Writer) error {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
|
||||
1
encoding/gcompress/testdata/zip/path1/1.txt
vendored
Normal file
1
encoding/gcompress/testdata/zip/path1/1.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
This is a test file for zip compression purpose.
|
||||
1
encoding/gcompress/testdata/zip/path2/2.txt
vendored
Normal file
1
encoding/gcompress/testdata/zip/path2/2.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
This is an another test file for zip compression purpose.
|
||||
Reference in New Issue
Block a user