Skip to content

Commit f9883ad

Browse files
Adding support for gomodules + refactor and issue fixes
Signed-off-by: Dariusz Prząda <[email protected]>
1 parent 6b0fdfb commit f9883ad

File tree

8 files changed

+126
-121
lines changed

8 files changed

+126
-121
lines changed

.gitignore

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1 @@
1-
# Binaries for programs and plugins
2-
*.exe
3-
*.dll
4-
*.so
5-
*.dylib
6-
7-
# Test binary, build with `go test -c`
8-
*.test
9-
10-
# Output of the go coverage tool, specifically when used with LiteIDE
11-
*.out
12-
13-
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
14-
.glide/
15-
16-
.idea/*
17-
data/*
1+
.idea

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,21 @@ go get -u github.com/artdarek/go-unzip
1414
package main
1515

1616
import (
17-
"github.com/artdarek/go-unzip"
1817
"fmt"
18+
19+
"github.com/artdarek/go-unzip/pkg/unzip"
1920
)
2021

2122
func main() {
22-
uz := unzip.New("file.zip", "directory/")
23-
err := uz.Extract()
23+
uz := unzip.New()
24+
25+
files, err := uz.Extract("./data/file.zip", "./data/directory")
2426
if err != nil {
2527
fmt.Println(err)
2628
}
29+
30+
fmt.Printf("extracted files count: %d", len(files))
31+
fmt.Printf("files list: %v", files)
2732
}
2833
```
2934

cmd/example/main.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/artdarek/go-unzip/pkg/unzip"
7+
)
8+
9+
func main() {
10+
uz := unzip.New()
11+
12+
files, err := uz.Extract("./data/file.zip", "./data/directory")
13+
if err != nil {
14+
fmt.Println(err)
15+
}
16+
17+
fmt.Printf("extracted files count: %d", len(files))
18+
fmt.Printf("files list: %v", files)
19+
}

data/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

examples/main.go

Lines changed: 0 additions & 14 deletions
This file was deleted.

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/artdarek/go-unzip
2+
3+
go 1.16

pkg/unzip/unzip.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package unzip
2+
3+
import (
4+
"archive/zip"
5+
"fmt"
6+
"io"
7+
"os"
8+
"path/filepath"
9+
"strings"
10+
)
11+
12+
type Unzip struct {
13+
}
14+
15+
func New() *Unzip {
16+
return &Unzip{}
17+
}
18+
19+
func (uz Unzip) Extract(source, destination string) ([]string, error) {
20+
r, err := zip.OpenReader(source)
21+
if err != nil {
22+
return nil, err
23+
}
24+
25+
defer func() {
26+
if err := r.Close(); err != nil {
27+
panic(err)
28+
}
29+
}()
30+
31+
err = os.MkdirAll(destination, 0755)
32+
if err != nil {
33+
return nil, err
34+
}
35+
36+
var extractedFiles []string
37+
for _, f := range r.File {
38+
err := uz.extractAndWriteFile(destination, f)
39+
if err != nil {
40+
return nil, err
41+
}
42+
43+
extractedFiles = append(extractedFiles, f.Name)
44+
}
45+
46+
return extractedFiles, nil
47+
}
48+
49+
func (Unzip) extractAndWriteFile(destination string, f *zip.File) error {
50+
rc, err := f.Open()
51+
if err != nil {
52+
return err
53+
}
54+
defer func() {
55+
if err := rc.Close(); err != nil {
56+
panic(err)
57+
}
58+
}()
59+
60+
path := filepath.Join(destination, f.Name)
61+
if !strings.HasPrefix(path, filepath.Clean(destination)+string(os.PathSeparator)) {
62+
return fmt.Errorf("%s: illegal file path", path)
63+
}
64+
65+
if f.FileInfo().IsDir() {
66+
err = os.MkdirAll(path, f.Mode())
67+
if err != nil {
68+
return err
69+
}
70+
} else {
71+
err = os.MkdirAll(filepath.Dir(path), f.Mode())
72+
if err != nil {
73+
return err
74+
}
75+
76+
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
77+
if err != nil {
78+
return err
79+
}
80+
defer func() {
81+
if err := f.Close(); err != nil {
82+
panic(err)
83+
}
84+
}()
85+
86+
_, err = io.Copy(f, rc)
87+
if err != nil {
88+
return err
89+
}
90+
}
91+
92+
return nil
93+
}

unzip.go

Lines changed: 0 additions & 87 deletions
This file was deleted.

0 commit comments

Comments
 (0)