From 31f19b0eee8c2c91c8f317cc17d46a452a5143e2 Mon Sep 17 00:00:00 2001 From: John Date: Fri, 6 Mar 2020 15:38:32 +0800 Subject: [PATCH] improve package gcompress --- .../gcompress/gcompress_z_unit_gzip_test.go | 39 +++++ .../gcompress/gcompress_z_unit_zip_test.go | 153 ++++++++++++++++++ ..._test.go => gcompress_z_unit_zlib_test.go} | 28 +--- encoding/gcompress/gcompress_zip_file.go | 35 +++- encoding/gcompress/testdata/zip/path1/1.txt | 1 + encoding/gcompress/testdata/zip/path2/2.txt | 1 + 6 files changed, 222 insertions(+), 35 deletions(-) create mode 100644 encoding/gcompress/gcompress_z_unit_gzip_test.go create mode 100644 encoding/gcompress/gcompress_z_unit_zip_test.go rename encoding/gcompress/{gcompress_test.go => gcompress_z_unit_zlib_test.go} (61%) create mode 100644 encoding/gcompress/testdata/zip/path1/1.txt create mode 100644 encoding/gcompress/testdata/zip/path2/2.txt diff --git a/encoding/gcompress/gcompress_z_unit_gzip_test.go b/encoding/gcompress/gcompress_z_unit_gzip_test.go new file mode 100644 index 000000000..712261a03 --- /dev/null +++ b/encoding/gcompress/gcompress_z_unit_gzip_test.go @@ -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) +} diff --git a/encoding/gcompress/gcompress_z_unit_zip_test.go b/encoding/gcompress/gcompress_z_unit_zip_test.go new file mode 100644 index 000000000..9fb0f3b1f --- /dev/null +++ b/encoding/gcompress/gcompress_z_unit_zip_test.go @@ -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")), + ) + }) +} diff --git a/encoding/gcompress/gcompress_test.go b/encoding/gcompress/gcompress_z_unit_zlib_test.go similarity index 61% rename from encoding/gcompress/gcompress_test.go rename to encoding/gcompress/gcompress_z_unit_zlib_test.go index 1b563f716..68ea18703 100644 --- a/encoding/gcompress/gcompress_test.go +++ b/encoding/gcompress/gcompress_z_unit_zlib_test.go @@ -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) } diff --git a/encoding/gcompress/gcompress_zip_file.go b/encoding/gcompress/gcompress_zip_file.go index 595138d2d..c36839fed 100644 --- a/encoding/gcompress/gcompress_zip_file.go +++ b/encoding/gcompress/gcompress_zip_file.go @@ -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 to 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 and writes the content to . +// The parameter specifies the exclusive file path that is not compressed to , +// commonly the destination zip file path. +// The unnecessary parameter 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 to using zip compressing algorithm. -// The parameter specifies the unzipped path of , +// The optional parameter specifies the unzipped path of , // which can be used to specify part of the archive file to unzip. // -// Note thate the parameter should be a directory. +// Note that the parameter 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 specifies the unzipped path of , // which can be used to specify part of the archive file to unzip. // -// Note thate the parameter should be a directory. +// Note that the parameter 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 and writes the content to . +// The parameter 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 { diff --git a/encoding/gcompress/testdata/zip/path1/1.txt b/encoding/gcompress/testdata/zip/path1/1.txt new file mode 100644 index 000000000..8529b38b3 --- /dev/null +++ b/encoding/gcompress/testdata/zip/path1/1.txt @@ -0,0 +1 @@ +This is a test file for zip compression purpose. \ No newline at end of file diff --git a/encoding/gcompress/testdata/zip/path2/2.txt b/encoding/gcompress/testdata/zip/path2/2.txt new file mode 100644 index 000000000..f51884841 --- /dev/null +++ b/encoding/gcompress/testdata/zip/path2/2.txt @@ -0,0 +1 @@ +This is an another test file for zip compression purpose. \ No newline at end of file