-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
59 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
*~ | ||
.idea | ||
*.DS* | ||
*.zip | ||
*.rar | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,65 @@ | ||
package xtractr | ||
|
||
import ( | ||
"archive/zip" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"archive/zip" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
/* How to extract a ZIP file. */ | ||
|
||
// ExtractZIP extracts a zip file.. to a destination. Simple enough. | ||
func ExtractZIP(xFile *XFile) (int64, []string, error) { | ||
zipReader, err := zip.OpenReader(xFile.FilePath) | ||
if err != nil { | ||
return 0, nil, fmt.Errorf("zip.OpenReader: %w", err) | ||
} | ||
defer zipReader.Close() | ||
zipReader, err := zip.OpenReader(xFile.FilePath) | ||
if err != nil { | ||
return 0, nil, fmt.Errorf("zip.OpenReader: %w", err) | ||
} | ||
defer zipReader.Close() | ||
|
||
files := []string{} | ||
size := int64(0) | ||
files := []string{} | ||
size := int64(0) | ||
|
||
for _, zipFile := range zipReader.Reader.File { | ||
fSize, err := xFile.unzip(zipFile) | ||
if err != nil { | ||
return size, files, fmt.Errorf("%s: %w", xFile.FilePath, err) | ||
} | ||
for _, zipFile := range zipReader.Reader.File { | ||
fSize, err := xFile.unzip(zipFile) | ||
if err != nil { | ||
return size, files, fmt.Errorf("%s: %w", xFile.FilePath, err) | ||
} | ||
|
||
files = append(files, filepath.Join(xFile.OutputDir, zipFile.Name)) //nolint: gosec | ||
size += fSize | ||
} | ||
files = append(files, filepath.Join(xFile.OutputDir, zipFile.Name)) //nolint:gosec | ||
size += fSize | ||
} | ||
|
||
return size, files, nil | ||
return size, files, nil | ||
} | ||
|
||
func (x *XFile) unzip(zipFile *zip.File) (int64, error) { //nolint:dupl | ||
wfile := x.clean(zipFile.Name) | ||
if !strings.HasPrefix(wfile, x.OutputDir) { | ||
// The file being written is trying to write outside of our base path. Malicious archive? | ||
return 0, fmt.Errorf("%s: %w: %s (from: %s)", zipFile.FileInfo().Name(), ErrInvalidPath, wfile, zipFile.Name) | ||
} | ||
wfile := x.clean(zipFile.Name) | ||
outputDir := filepath.Clean(x.OutputDir) | ||
if !strings.HasPrefix(wfile, outputDir) { | ||
// The file being written is trying to write outside of our base path. Malicious archive? | ||
return 0, fmt.Errorf("%s: %w: %s (from: %s)", zipFile.FileInfo().Name(), ErrInvalidPath, wfile, zipFile.Name) | ||
} | ||
|
||
if strings.HasSuffix(wfile, "/") || zipFile.FileInfo().IsDir() { | ||
if err := os.MkdirAll(wfile, x.DirMode); err != nil { | ||
return 0, fmt.Errorf("making zipFile dir: %w", err) | ||
} | ||
if strings.HasSuffix(wfile, "/") || zipFile.FileInfo().IsDir() { | ||
if err := os.MkdirAll(wfile, x.DirMode); err != nil { | ||
return 0, fmt.Errorf("making zipFile dir: %w", err) | ||
} | ||
|
||
return 0, nil | ||
} | ||
return 0, nil | ||
} | ||
|
||
zFile, err := zipFile.Open() | ||
if err != nil { | ||
return 0, fmt.Errorf("zipFile.Open: %w", err) | ||
} | ||
defer zFile.Close() | ||
zFile, err := zipFile.Open() | ||
if err != nil { | ||
return 0, fmt.Errorf("zipFile.Open: %w", err) | ||
} | ||
defer zFile.Close() | ||
|
||
s, err := writeFile(wfile, zFile, x.FileMode, x.DirMode) | ||
if err != nil { | ||
return s, fmt.Errorf("%s: %w: %s (from: %s)", zipFile.FileInfo().Name(), err, wfile, zipFile.Name) | ||
} | ||
s, err := writeFile(wfile, zFile, x.FileMode, x.DirMode) | ||
if err != nil { | ||
return s, fmt.Errorf("%s: %w: %s (from: %s)", zipFile.FileInfo().Name(), err, wfile, zipFile.Name) | ||
} | ||
|
||
return s, nil | ||
return s, nil | ||
} |