Skip to content

Commit

Permalink
Update build script and tweak readme
Browse files Browse the repository at this point in the history
  • Loading branch information
mholt committed Nov 7, 2018
1 parent bec9a8b commit edeadbb
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 16 deletions.
81 changes: 71 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Or download binaries from the [releases](https://github.com/mholt/archiver/relea

```bash
# Syntax: arc archive [archive name] [input files...]

$ arc archive test.tar.gz file1.txt images/file2.jpg folder/subfolder
```

Expand All @@ -73,6 +74,7 @@ $ arc archive test.tar.gz file1.txt images/file2.jpg folder/subfolder

```bash
# Syntax: arc unarchive [archive name] [destination]

$ arc unarchive test.tar.gz
```

Expand All @@ -84,55 +86,114 @@ The archive name must end with a supported file extension—this is how it k

```bash
# Syntax: arc ls [archive name]

$ arc ls caddy_dist.tar.gz
drwxr-xr-x matt staff 0 2018-09-19 15:47:18 -0600 MDT dist/
-rw-r--r-- matt staff 6148 2017-08-07 18:34:22 -0600 MDT dist/.DS_Store
-rw-r--r-- matt staff 22481 2018-09-19 15:47:18 -0600 MDT dist/CHANGES.txt
-rw-r--r-- matt staff 17189 2018-09-19 15:47:18 -0600 MDT dist/EULA.txt
-rw-r--r-- matt staff 25261 2016-03-07 16:32:00 -0700 MST dist/LICENSES.txt
-rw-r--r-- matt staff 1017 2018-09-19 15:47:18 -0600 MDT dist/README.txt
-rw-r--r-- matt staff 288 2016-03-21 11:52:38 -0600 MDT dist/gitcookie.sh.enc
drwxr-xr-x matt staff 0 2018-09-19 15:47:18 -0600 MDT dist/
-rw-r--r-- matt staff 6148 2017-08-07 18:34:22 -0600 MDT dist/.DS_Store
-rw-r--r-- matt staff 22481 2018-09-19 15:47:18 -0600 MDT dist/CHANGES.txt
-rw-r--r-- matt staff 17189 2018-09-19 15:47:18 -0600 MDT dist/EULA.txt
-rw-r--r-- matt staff 25261 2016-03-07 16:32:00 -0700 MST dist/LICENSES.txt
-rw-r--r-- matt staff 1017 2018-09-19 15:47:18 -0600 MDT dist/README.txt
-rw-r--r-- matt staff 288 2016-03-21 11:52:38 -0600 MDT dist/gitcookie.sh.enc
...
```

### Extract a specific file or folder from an archive

```bash
# Syntax: arc extract [archive name] [path in archive] [destination on disk]

$ arc extract test.tar.gz foo/hello.txt extracted/hello.txt
```

### Compress a single file

```bash
# Syntax: arc compress [input file] [output file]

$ arc compress test.txt compressed_test.txt.gz
$ arc compress test.txt gz
```

For convenience, if the output file is simply a compression format (without leading dot), the output file name will be the same as the input name but with the format extension appended, and the input file will be deleted if successful.
For convenience, the output file (second argument) may simply be a compression format (without leading dot), in which case the output filename will be the same as the input filename but with the format extension appended, and the input file will be deleted if successful.

### Decompress a single file

```bash
# Syntax: arc decompress [input file] [output file]

$ arc decompress test.txt.gz original_test.txt
$ arc decompress test.txt.gz
```

For convenience, if the output file is not specified, it will have the same name as the input, but with the compression extension stripped from the end, and the input file will be deleted if successful.
For convenience, the output file (second argument) may be omitted. In that case, the output filename will have the same name as the input filename, but with the compression extension stripped from the end; and the input file will be deleted if successful.

### Flags

Flags are specified before the subcommand. Use `arc help` or `arc -h` to get usage help and a description of flags with their default values.

## Library Use

The archiver package allows you to easily create and open archives, walk their contents, extract specific files, compress and decompress files, and even stream archives in and out using pure io.Reader and io.Writer interfaces, without ever needing to touch the disk.

```go
import "github.com/mholt/archiver"
```

The archiver package allows you to easily create and open archives, walk their contents, extract specific files, compress and decompress files, and even stream archives in and out using pure io.Reader and io.Writer interfaces, without ever needing to touch the disk. See [package godoc documentation](https://godoc.org/github.com/mholt/archiver) to learn how to do this -- it's really slick!
[See the package's GoDoc](https://godoc.org/github.com/mholt/archiver) for full API documentation.

For example, creating an archive:

```go
z := archiver.Zip{
CompressionLevel: flate.DefaultCompression,
MkdirAll: true,
SelectiveCompression: true,
ContinueOnError: false,
OverwriteExisting: false,
ImplicitTopLevelFolder: false,
}

err := z.Archive([]string{"testdata", "other/file.txt"}, "/Users/matt/Desktop/test.zip")
```

Inspecting an archive:

```go
err = z.Walk("/Users/matt/Desktop/test.zip", func(f archiver.File) error {
zfh, ok := f.Header.(zip.FileHeader)
if ok {
fmt.Println("Filename:", zfh.Name)
}
return nil
})
```

Streaming files into an archive that is being written to the HTTP response:

```go
err = z.Create(responseWriter)
if err != nil {
return err
}
defer z.Close()

for _, f := range files {
// ... open file and get the name for it within the archive ...
err = z.Write(File{
FileInfo: archiver.FileInfo{
FileInfo: f.FileInfo(),
CustomName: nameInArchive,
},
ReadCloser: f,
})
if err != nil {
return err
}
}
```

There's a lot more that can be done, too. [See the GoDoc](https://godoc.org/github.com/mholt/archiver) for full API documentation.

**Security note: This package does NOT attempt to mitigate zip-slip attacks.** It is [extremely difficult](https://github.com/rubyzip/rubyzip/pull/376) [to do properly](https://github.com/mholt/archiver/pull/65#issuecomment-395988244) and [seemingly impossible to mitigate effectively across platforms](https://github.com/golang/go/issues/20126). [Attempted fixes have broken processing of legitimate files in production](https://github.com/mholt/archiver/pull/70#issuecomment-423267320), rendering the program unusable. Our recommendation instead is to inspect the contents of an untrusted archive before extracting it (this package provides `Walkers`) and decide if you want to proceed with extraction.

Expand Down
8 changes: 2 additions & 6 deletions build.bash
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@ set -ex

export CGO_ENABLED=0

cd cmd/archiver
cd cmd/arc
GOOS=linux GOARCH=386 go build -o ../../builds/archiver_linux_386
GOOS=linux GOARCH=amd64 go build -o ../../builds/archiver_linux_amd64
GOOS=linux GOARCH=arm go build -o ../../builds/archiver_linux_arm7
GOOS=linux GOARCH=arm64 go build -o ../../builds/archiver_linux_arm64
GOOS=darwin GOARCH=amd64 go build -o ../../builds/archiver_mac_amd64
GOOS=windows GOARCH=386 go build -o ../../builds/archiver_windows_386.exe
GOOS=windows GOARCH=amd64 go build -o ../../builds/archiver_windows_amd64.exe
GOOS=freebsd GOARCH=386 go build -o ../../builds/archiver_freebsd_386
GOOS=freebsd GOARCH=amd64 go build -o ../../builds/archiver_freebsd_amd64
GOOS=freebsd GOARCH=arm go build -o ../../builds/archiver_freebsd_arm7
GOOS=openbsd GOARCH=386 go build -o ../../builds/archiver_openbsd_386
GOOS=openbsd GOARCH=amd64 go build -o ../../builds/archiver_openbsd_amd64
cd ../..
cd ../..

0 comments on commit edeadbb

Please sign in to comment.