Skip to content

Commit

Permalink
Adding support for gomodules + refactor and issue fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Dariusz Prząda <[email protected]>
  • Loading branch information
dariusz-przada committed Mar 23, 2021
1 parent 6b0fdfb commit f9883ad
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 121 deletions.
18 changes: 1 addition & 17 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,17 +1 @@
# Binaries for programs and plugins
*.exe
*.dll
*.so
*.dylib

# Test binary, build with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/

.idea/*
data/*
.idea
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@ go get -u github.com/artdarek/go-unzip
package main

import (
"github.com/artdarek/go-unzip"
"fmt"

"github.com/artdarek/go-unzip/pkg/unzip"
)

func main() {
uz := unzip.New("file.zip", "directory/")
err := uz.Extract()
uz := unzip.New()

files, err := uz.Extract("./data/file.zip", "./data/directory")
if err != nil {
fmt.Println(err)
}

fmt.Printf("extracted files count: %d", len(files))
fmt.Printf("files list: %v", files)
}
```

Expand Down
19 changes: 19 additions & 0 deletions cmd/example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"fmt"

"github.com/artdarek/go-unzip/pkg/unzip"
)

func main() {
uz := unzip.New()

files, err := uz.Extract("./data/file.zip", "./data/directory")
if err != nil {
fmt.Println(err)
}

fmt.Printf("extracted files count: %d", len(files))
fmt.Printf("files list: %v", files)
}
2 changes: 2 additions & 0 deletions data/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
14 changes: 0 additions & 14 deletions examples/main.go

This file was deleted.

3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/artdarek/go-unzip

go 1.16
93 changes: 93 additions & 0 deletions pkg/unzip/unzip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package unzip

import (
"archive/zip"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)

type Unzip struct {
}

func New() *Unzip {
return &Unzip{}
}

func (uz Unzip) Extract(source, destination string) ([]string, error) {
r, err := zip.OpenReader(source)
if err != nil {
return nil, err
}

defer func() {
if err := r.Close(); err != nil {
panic(err)
}
}()

err = os.MkdirAll(destination, 0755)
if err != nil {
return nil, err
}

var extractedFiles []string
for _, f := range r.File {
err := uz.extractAndWriteFile(destination, f)
if err != nil {
return nil, err
}

extractedFiles = append(extractedFiles, f.Name)
}

return extractedFiles, nil
}

func (Unzip) extractAndWriteFile(destination string, f *zip.File) error {
rc, err := f.Open()
if err != nil {
return err
}
defer func() {
if err := rc.Close(); err != nil {
panic(err)
}
}()

path := filepath.Join(destination, f.Name)
if !strings.HasPrefix(path, filepath.Clean(destination)+string(os.PathSeparator)) {
return fmt.Errorf("%s: illegal file path", path)
}

if f.FileInfo().IsDir() {
err = os.MkdirAll(path, f.Mode())
if err != nil {
return err
}
} else {
err = os.MkdirAll(filepath.Dir(path), f.Mode())
if err != nil {
return err
}

f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
defer func() {
if err := f.Close(); err != nil {
panic(err)
}
}()

_, err = io.Copy(f, rc)
if err != nil {
return err
}
}

return nil
}
87 changes: 0 additions & 87 deletions unzip.go

This file was deleted.

0 comments on commit f9883ad

Please sign in to comment.