Skip to content

Commit

Permalink
feat: 优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
CC11001100 committed Dec 11, 2023
1 parent 4e08104 commit 5042b2d
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 10 deletions.
4 changes: 2 additions & 2 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 解压到的目录不能为空
Expand Down
5 changes: 4 additions & 1 deletion examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
10 changes: 6 additions & 4 deletions options.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package unzip

import "github.com/golang-infrastructure/go-pointer"

// DefaultUnzipWorkerNum 如果没有指定的话,默认情况下解压使用的并发数是多少
const DefaultUnzipWorkerNum = 1

Expand All @@ -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),
}
}

Expand All @@ -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
}
7 changes: 4 additions & 3 deletions unzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package unzip
import (
"archive/zip"
"fmt"
"github.com/golang-infrastructure/go-pointer"
"sync"
)

Expand Down Expand Up @@ -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
}

Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions zip_slip.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
)

// IsZipSlip 对单个文件进行zip slip检查
// baseDirectory: 要解压到的目录
// filename: 要解压的文件的名称
func IsZipSlip(baseDirectory, filename string) bool {
if baseDirectory == "" {
baseDirectory = "fake-directory"
Expand Down

0 comments on commit 5042b2d

Please sign in to comment.