-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
11 changed files
with
184 additions
and
10 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
*~ | ||
*.zip | ||
*.rar | ||
*.r00 | ||
*.r01 | ||
*.7z | ||
*.tar | ||
*.gz | ||
*.tgz | ||
*.bz2 | ||
*.tbz2 | ||
/cmd/xt/xt | ||
/xt |
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package xtractr | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
|
||
"github.com/saracen/go7z" | ||
) | ||
|
||
// Extract7z extracts a 7zip archive. This wraps https://github.com/saracen/go7z. | ||
func Extract7z(x *XFile) (int64, []string, error) { | ||
sz, err := go7z.OpenReader(x.FilePath) | ||
if err != nil { | ||
return 0, nil, fmt.Errorf("os.Open: %w", err) | ||
} | ||
defer sz.Close() | ||
|
||
return x.un7zip(sz) | ||
} | ||
|
||
func (x *XFile) un7zip(szreader *go7z.ReadCloser) (int64, []string, error) { | ||
files := []string{} | ||
size := int64(0) | ||
|
||
for { | ||
header, err := szreader.Next() | ||
|
||
switch { | ||
case errors.Is(err, io.EOF): | ||
return size, files, nil | ||
case err != nil: | ||
return size, files, fmt.Errorf("szreader.Next: %w", err) | ||
case header == nil: | ||
return size, files, fmt.Errorf("%w: %s", ErrInvalidHead, x.FilePath) | ||
} | ||
|
||
wfile := x.clean(header.Name) | ||
if !strings.HasPrefix(wfile, x.OutputDir) { | ||
// The file being written is trying to write outside of our base path. Malicious archive? | ||
return size, files, fmt.Errorf("%s: %w: %s (from: %s)", x.FilePath, ErrInvalidPath, wfile, header.Name) | ||
} | ||
|
||
// https://github.com/saracen/go7z/blob/9c09b6bd7fda869ef48ff6f693744a65f477816b/README.md#usage | ||
if header.IsEmptyStream && !header.IsEmptyFile { | ||
if err = os.MkdirAll(wfile, x.DirMode); err != nil { | ||
return size, files, fmt.Errorf("os.MkdirAll: %w", err) | ||
} | ||
|
||
continue | ||
} | ||
|
||
s, err := writeFile(wfile, szreader, x.FileMode, x.DirMode) | ||
if err != nil { | ||
return size, files, err | ||
} | ||
|
||
files = append(files, wfile) | ||
size += s | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# xt | ||
|
||
This is an example app you may compile and use to extract files or whole directories. | ||
|
||
```shell | ||
go get -u golift.io/xtractr/cmd/xt | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Package main is a binary used for demonstration purposes. It works, but lacks | ||
// the features you can program into your own application. This is just a quick | ||
// sample provided to show one way to interface this library. | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"log" | ||
"os" | ||
"strings" | ||
"time" | ||
|
||
"golift.io/xtractr" | ||
) | ||
|
||
func main() { | ||
pwd, _ := os.Getwd() | ||
output := flag.String("output", pwd, "Output directory, default is current directory") | ||
|
||
flag.Parse() | ||
log.SetFlags(0) | ||
|
||
inputFiles := flag.Args() | ||
if len(inputFiles) < 1 { | ||
log.Printf("If you pass a directory, this app will extract every archive in it.") | ||
log.Fatalf("Usage: %s [-output <path>] <path> [paths...]", os.Args[0]) | ||
} | ||
|
||
processInput(inputFiles, *output) | ||
} | ||
|
||
func processInput(paths []string, output string) { | ||
log.Printf("==> Output Path: %s", output) | ||
|
||
archives := getArchives(paths) | ||
for i, f := range archives { | ||
log.Printf("==> Extracting Archive (%d/%d): %s", i, len(archives), f) | ||
|
||
start := time.Now() | ||
|
||
size, files, _, err := xtractr.ExtractFile(&xtractr.XFile{ | ||
FilePath: f, // Path to archive being extracted. | ||
OutputDir: output, // Folder to extract archive into. | ||
FileMode: 0644, // nolint:gomnd // Write files with this mode. | ||
DirMode: 0755, // nolint:gomnd // Write folders with this mode. | ||
Password: "", // (RAR) Archive password. Blank for none. | ||
}) | ||
if err != nil { | ||
log.Printf("[ERROR] Archive: %s: %v", f, err) | ||
continue | ||
} | ||
|
||
elapsed := time.Since(start).Round(time.Millisecond) | ||
log.Printf("==> Extracted Archive %s in %v: bytes: %d, files: %d", f, elapsed, size, len(files)) | ||
log.Printf("==> Files:\n - %s", strings.Join(files, "\n - ")) | ||
} | ||
} | ||
|
||
func getArchives(paths []string) []string { | ||
archives := []string{} | ||
|
||
for _, f := range paths { | ||
switch fileInfo, err := os.Stat(f); { | ||
case err != nil: | ||
log.Fatalf("[ERROR] Reading Path: %s: %s", f, err) | ||
case fileInfo.IsDir(): | ||
archives = append(archives, xtractr.FindCompressedFiles(f)...) | ||
default: | ||
archives = append(archives, f) | ||
} | ||
} | ||
|
||
return archives | ||
} |
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,2 +1,8 @@ | ||
github.com/nwaples/rardecode v1.1.0 h1:vSxaY8vQhOcVr4mm5e8XllHWTiM4JF507A0Katqw7MQ= | ||
github.com/nwaples/rardecode v1.1.0/go.mod h1:5DzqNKiOdpKKBH87u8VlvAnPZMXcGRhxWkRpHbbfGS0= | ||
github.com/saracen/go7z v0.0.0-20191010121135-9c09b6bd7fda h1:h+YpzUB/bGVJcLqW+d5GghcCmE/A25KbzjXvWJQi/+o= | ||
github.com/saracen/go7z v0.0.0-20191010121135-9c09b6bd7fda/go.mod h1:MSotTrCv1PwoR8QgU1JurEx+lNNbtr25I+m0zbLyAGw= | ||
github.com/saracen/solidblock v0.0.0-20190426153529-45df20abab6f h1:1cJITU3JUI8qNS5T0BlXwANsVdyoJQHQ4hvOxbunPCw= | ||
github.com/saracen/solidblock v0.0.0-20190426153529-45df20abab6f/go.mod h1:LyBTue+RWeyIfN3ZJ4wVxvDuvlGJtDgCLgCb6HCPgps= | ||
github.com/ulikunitz/xz v0.5.10 h1:t92gobL9l3HE202wg3rlk19F6X+JOxl9BBrCCMYEYd8= | ||
github.com/ulikunitz/xz v0.5.10/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= |
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