From 5042b2ddcbd2dfb3641d776762693d322a849de0 Mon Sep 17 00:00:00 2001 From: cc11001100 Date: Mon, 11 Dec 2023 23:59:38 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- errors.go | 4 ++-- examples/main.go | 5 ++++- go.mod | 2 ++ go.sum | 4 ++++ options.go | 10 ++++++---- unzip.go | 7 ++++--- zip_slip.go | 2 ++ 7 files changed, 24 insertions(+), 10 deletions(-) diff --git a/errors.go b/errors.go index ff41496..1623609 100644 --- a/errors.go +++ b/errors.go @@ -4,10 +4,10 @@ import "errors" var ( - // ErrWorkerNumInvalid 并发数必须大于0,否则没法搞了 + // ErrWorkerNumInvalid 解压zip文件时的并发数必须大于0,否则没法搞了 ErrWorkerNumInvalid = errors.New("options.WorkerNum it has to be greater than 0") - // ErrSourceZipFileEmpty 源zip文件不能为空 + // ErrSourceZipFileEmpty 源zip文件不能为空,否则会返回此错误 ErrSourceZipFileEmpty = errors.New("options.SourceZipFile can not empty") // ErrDestinationDirectoryEmpty 解压到的目录不能为空 diff --git a/examples/main.go b/examples/main.go index 75f9fc4..1438034 100644 --- a/examples/main.go +++ b/examples/main.go @@ -4,7 +4,10 @@ import "github.com/compression-algorithm-research-lab/go-unzip" func main() { - options := unzip.NewOptions().SetSourceZipFile("test_data/foo.zip").SetDestinationDirectory("test_data/foo") + options := unzip.NewOptions(). + SetSourceZipFile("test_data/foo.zip"). + SetDestinationDirectory("test_data/foo"). + SetWorkerNum(100) err := unzip.New(options).Unzip() if err != nil { panic(err) diff --git a/go.mod b/go.mod index 4a0ab8b..3af9fd0 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,8 @@ go 1.18 require ( github.com/davecgh/go-spew v1.1.1 // indirect + github.com/golang-infrastructure/go-pointer v0.0.5 // indirect + github.com/golang-infrastructure/go-reflect-utils v0.0.0-20221130143747-965ef2eb09c3 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/stretchr/objx v0.5.0 // indirect github.com/stretchr/testify v1.8.4 // indirect diff --git a/go.sum b/go.sum index 5bddba9..8e9d02d 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,10 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/golang-infrastructure/go-pointer v0.0.5 h1:wzZ/XnXKzD3DWECTnUpUh+xAlGSWqfn/pQyusPNsqrQ= +github.com/golang-infrastructure/go-pointer v0.0.5/go.mod h1:SBP0/8QH+mr8LGP/tDo28EQ2b45WqfzjXYbPjmK88g8= +github.com/golang-infrastructure/go-reflect-utils v0.0.0-20221130143747-965ef2eb09c3 h1:jJ7AdpNdLQudsx1hiXY9iwmauHARV4/UB52KnBh9Se0= +github.com/golang-infrastructure/go-reflect-utils v0.0.0-20221130143747-965ef2eb09c3/go.mod h1:zqXYxqOBa1mL2ilBK6PuH/Wb/Iego7en6XhiKWdZQHI= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/options.go b/options.go index 77fe2ad..94f63a1 100644 --- a/options.go +++ b/options.go @@ -1,5 +1,7 @@ package unzip +import "github.com/golang-infrastructure/go-pointer" + // DefaultUnzipWorkerNum 如果没有指定的话,默认情况下解压使用的并发数是多少 const DefaultUnzipWorkerNum = 1 @@ -12,13 +14,13 @@ type Options struct { // 解压到的目标文件夹,必须是一个目录,如果不存在的话会自动创建,如果已经存在的话尽量为空,否则可能会被重复覆盖写文件 DestinationDirectory string - // 解压的时候使用的并发数 - WorkerNum int + // 解压的时候使用的并发数,如果不指定的话默认为 DefaultUnzipWorkerNum + WorkerNum *int } func NewOptions() *Options { return &Options{ - WorkerNum: DefaultUnzipWorkerNum, + WorkerNum: pointer.ToPointer(DefaultUnzipWorkerNum), } } @@ -36,6 +38,6 @@ func (x *Options) SetDestinationDirectory(destinationDirectory string) *Options // SetWorkerNum 设置解压时使用到的并发数 func (x *Options) SetWorkerNum(workerNum int) *Options { - x.WorkerNum = workerNum + x.WorkerNum = pointer.ToPointer(workerNum) return x } diff --git a/unzip.go b/unzip.go index 4cb5731..0ee0087 100644 --- a/unzip.go +++ b/unzip.go @@ -3,6 +3,7 @@ package unzip import ( "archive/zip" "fmt" + "github.com/golang-infrastructure/go-pointer" "sync" ) @@ -37,7 +38,7 @@ func (x *Unzip) Traversal(handler FileHandler) (err error) { // 参数检查 if x.options.SourceZipFile == "" { return ErrSourceZipFileEmpty - } else if x.options.WorkerNum <= 0 { + } else if x.options.WorkerNum == nil || pointer.FromPointer(x.options.WorkerNum) <= 0 { return ErrWorkerNumInvalid } @@ -61,7 +62,7 @@ func (x *Unzip) Traversal(handler FileHandler) (err error) { // 并发处理压缩文件中的每个文件 var wg sync.WaitGroup - for i := 0; i < x.options.WorkerNum; i++ { + for i := 0; i < pointer.FromPointer(x.options.WorkerNum); i++ { wg.Add(1) go func() { defer wg.Done() @@ -101,7 +102,7 @@ func (x *Unzip) makeZipFileChannel(files []*zip.File) chan *File { func (x *Unzip) Unzip() error { // 参数检查 - if x.options.WorkerNum <= 0 { + if x.options.WorkerNum == nil || pointer.FromPointer(x.options.WorkerNum) <= 0 { return ErrWorkerNumInvalid } else if x.options.SourceZipFile == "" { return ErrSourceZipFileEmpty diff --git a/zip_slip.go b/zip_slip.go index b40169a..517a7ae 100644 --- a/zip_slip.go +++ b/zip_slip.go @@ -9,6 +9,8 @@ import ( ) // IsZipSlip 对单个文件进行zip slip检查 +// baseDirectory: 要解压到的目录 +// filename: 要解压的文件的名称 func IsZipSlip(baseDirectory, filename string) bool { if baseDirectory == "" { baseDirectory = "fake-directory"