File tree Expand file tree Collapse file tree 7 files changed +24
-10
lines changed Expand file tree Collapse file tree 7 files changed +24
-10
lines changed Original file line number Diff line number Diff line change @@ -4,10 +4,10 @@ import "errors"
44
55var (
66
7- // ErrWorkerNumInvalid 并发数必须大于0 ,否则没法搞了
7+ // ErrWorkerNumInvalid 解压zip文件时的并发数必须大于0 ,否则没法搞了
88 ErrWorkerNumInvalid = errors .New ("options.WorkerNum it has to be greater than 0" )
99
10- // ErrSourceZipFileEmpty 源zip文件不能为空
10+ // ErrSourceZipFileEmpty 源zip文件不能为空,否则会返回此错误
1111 ErrSourceZipFileEmpty = errors .New ("options.SourceZipFile can not empty" )
1212
1313 // ErrDestinationDirectoryEmpty 解压到的目录不能为空
Original file line number Diff line number Diff line change @@ -4,7 +4,10 @@ import "github.com/compression-algorithm-research-lab/go-unzip"
44
55func main () {
66
7- options := unzip .NewOptions ().SetSourceZipFile ("test_data/foo.zip" ).SetDestinationDirectory ("test_data/foo" )
7+ options := unzip .NewOptions ().
8+ SetSourceZipFile ("test_data/foo.zip" ).
9+ SetDestinationDirectory ("test_data/foo" ).
10+ SetWorkerNum (100 )
811 err := unzip .New (options ).Unzip ()
912 if err != nil {
1013 panic (err )
Original file line number Diff line number Diff line change 44
55require (
66 github.com/davecgh/go-spew v1.1.1 // indirect
7+ github.com/golang-infrastructure/go-pointer v0.0.5 // indirect
8+ github.com/golang-infrastructure/go-reflect-utils v0.0.0-20221130143747-965ef2eb09c3 // indirect
79 github.com/pmezard/go-difflib v1.0.0 // indirect
810 github.com/stretchr/objx v0.5.0 // indirect
911 github.com/stretchr/testify v1.8.4 // indirect
Original file line number Diff line number Diff line change 11github.com/davecgh/go-spew v1.1.0 /go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38 =
22github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c =
33github.com/davecgh/go-spew v1.1.1 /go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38 =
4+ github.com/golang-infrastructure/go-pointer v0.0.5 h1:wzZ/XnXKzD3DWECTnUpUh+xAlGSWqfn/pQyusPNsqrQ =
5+ github.com/golang-infrastructure/go-pointer v0.0.5 /go.mod h1:SBP0/8QH+mr8LGP/tDo28EQ2b45WqfzjXYbPjmK88g8 =
6+ github.com/golang-infrastructure/go-reflect-utils v0.0.0-20221130143747-965ef2eb09c3 h1:jJ7AdpNdLQudsx1hiXY9iwmauHARV4/UB52KnBh9Se0 =
7+ github.com/golang-infrastructure/go-reflect-utils v0.0.0-20221130143747-965ef2eb09c3 /go.mod h1:zqXYxqOBa1mL2ilBK6PuH/Wb/Iego7en6XhiKWdZQHI =
48github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM =
59github.com/pmezard/go-difflib v1.0.0 /go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4 =
610github.com/stretchr/objx v0.1.0 /go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME =
Original file line number Diff line number Diff line change 11package unzip
22
3+ import "github.com/golang-infrastructure/go-pointer"
4+
35// DefaultUnzipWorkerNum 如果没有指定的话,默认情况下解压使用的并发数是多少
46const DefaultUnzipWorkerNum = 1
57
@@ -12,13 +14,13 @@ type Options struct {
1214 // 解压到的目标文件夹,必须是一个目录,如果不存在的话会自动创建,如果已经存在的话尽量为空,否则可能会被重复覆盖写文件
1315 DestinationDirectory string
1416
15- // 解压的时候使用的并发数
16- WorkerNum int
17+ // 解压的时候使用的并发数,如果不指定的话默认为 DefaultUnzipWorkerNum
18+ WorkerNum * int
1719}
1820
1921func NewOptions () * Options {
2022 return & Options {
21- WorkerNum : DefaultUnzipWorkerNum ,
23+ WorkerNum : pointer . ToPointer ( DefaultUnzipWorkerNum ) ,
2224 }
2325}
2426
@@ -36,6 +38,6 @@ func (x *Options) SetDestinationDirectory(destinationDirectory string) *Options
3638
3739// SetWorkerNum 设置解压时使用到的并发数
3840func (x * Options ) SetWorkerNum (workerNum int ) * Options {
39- x .WorkerNum = workerNum
41+ x .WorkerNum = pointer . ToPointer ( workerNum )
4042 return x
4143}
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package unzip
33import (
44 "archive/zip"
55 "fmt"
6+ "github.com/golang-infrastructure/go-pointer"
67 "sync"
78)
89
@@ -37,7 +38,7 @@ func (x *Unzip) Traversal(handler FileHandler) (err error) {
3738 // 参数检查
3839 if x .options .SourceZipFile == "" {
3940 return ErrSourceZipFileEmpty
40- } else if x .options .WorkerNum <= 0 {
41+ } else if x .options .WorkerNum == nil || pointer . FromPointer ( x . options . WorkerNum ) <= 0 {
4142 return ErrWorkerNumInvalid
4243 }
4344
@@ -61,7 +62,7 @@ func (x *Unzip) Traversal(handler FileHandler) (err error) {
6162
6263 // 并发处理压缩文件中的每个文件
6364 var wg sync.WaitGroup
64- for i := 0 ; i < x .options .WorkerNum ; i ++ {
65+ for i := 0 ; i < pointer . FromPointer ( x .options .WorkerNum ) ; i ++ {
6566 wg .Add (1 )
6667 go func () {
6768 defer wg .Done ()
@@ -101,7 +102,7 @@ func (x *Unzip) makeZipFileChannel(files []*zip.File) chan *File {
101102func (x * Unzip ) Unzip () error {
102103
103104 // 参数检查
104- if x .options .WorkerNum <= 0 {
105+ if x .options .WorkerNum == nil || pointer . FromPointer ( x . options . WorkerNum ) <= 0 {
105106 return ErrWorkerNumInvalid
106107 } else if x .options .SourceZipFile == "" {
107108 return ErrSourceZipFileEmpty
Original file line number Diff line number Diff line change 99)
1010
1111// IsZipSlip 对单个文件进行zip slip检查
12+ // baseDirectory: 要解压到的目录
13+ // filename: 要解压的文件的名称
1214func IsZipSlip (baseDirectory , filename string ) bool {
1315 if baseDirectory == "" {
1416 baseDirectory = "fake-directory"
You can’t perform that action at this time.
0 commit comments