Skip to content

Commit

Permalink
update README
Browse files Browse the repository at this point in the history
  • Loading branch information
jm33-m0 committed Nov 19, 2024
1 parent 80a21ab commit 374f004
Showing 1 changed file with 19 additions and 26 deletions.
45 changes: 19 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# archiver [![Go Reference](https://pkg.go.dev/badge/github.com/mholt/archiver/v4.svg)](https://pkg.go.dev/github.com/mholt/archiver/v4) [![Ubuntu-latest](https://github.com/mholt/archiver/actions/workflows/ubuntu-latest.yml/badge.svg)](https://github.com/mholt/archiver/actions/workflows/ubuntu-latest.yml) [![Macos-latest](https://github.com/mholt/archiver/actions/workflows/macos-latest.yml/badge.svg)](https://github.com/mholt/archiver/actions/workflows/macos-latest.yml) [![Windows-latest](https://github.com/mholt/archiver/actions/workflows/windows-latest.yml/badge.svg)](https://github.com/mholt/archiver/actions/workflows/windows-latest.yml)

Introducing **Archiver 4.0** - a cross-platform, multi-format archive utility and Go library. A powerful and flexible library meets an elegant CLI in this generic replacement for several platform-specific or format-specific archive utilities.
Introducing **Archiver 4.0 (alpha)** - a cross-platform, multi-format archive utility and Go library. A powerful and flexible library meets an elegant CLI in this generic replacement for several platform-specific or format-specific archive utilities.

**:warning: v4 is in ALPHA. The core library APIs work pretty well but the command has not been implemented yet, nor have most automated tests. If you need the `arc` command, stick with v3 for now.**

Expand All @@ -11,8 +11,8 @@ Introducing **Archiver 4.0** - a cross-platform, multi-format archive utility an
- By file name
- By header
- Traverse directories, archive files, and any other file uniformly as [`io/fs`](https://pkg.go.dev/io/fs) file systems:
- [`DirFS`](https://pkg.go.dev/github.com/mholt/archiver/v4#DirFS)
- [`FileFS`](https://pkg.go.dev/github.com/mholt/archiver/v4#FileFS)
- [`DirFS`](https://pkg.go.dev/github.com/mholt/archiver/v4#DirFS)
- [`ArchiveFS`](https://pkg.go.dev/github.com/mholt/archiver/v4#ArchiveFS)
- Compress and decompress files
- Create and extract archive files
Expand Down Expand Up @@ -91,11 +91,12 @@ if err != nil {
}
defer out.Close()

// we can use the CompressedArchive type to gzip a tarball
// we can use the Archive type to gzip a tarball
// (compression is not required; you could use Tar directly)
format := archiver.CompressedArchive{
format := archiver.Archive{
Compression: archiver.Gz{},
Archival: archiver.Tar{},
Extraction: archiver.Tar{},
}

// create the archive
Expand All @@ -111,26 +112,16 @@ The first parameter to `FilesFromDisk()` is an optional options struct, allowing

Extracting an archive, extracting _from_ an archive, and walking an archive are all the same function.

Simply use your format type (e.g. `Zip`) to call `Extract()`. You'll pass in a context (for cancellation), the input stream, the list of files you want out of the archive, and a callback function to handle each file.

If you want all the files, pass in a nil list of file paths.
Simply use your format type (e.g. `Zip`) to call `Extract()`. You'll pass in a context (for cancellation), the input stream, and a callback function to handle each file.

```go
// the type that will be used to read the input stream
format := archiver.Zip{}

// the list of files we want out of the archive; any
// directories will include all their contents unless
// we return fs.SkipDir from our handler
// (leave this nil to walk ALL files from the archive)
fileList := []string{"file1.txt", "subfolder"}
var format archiver.Zip

handler := func(ctx context.Context, f archiver.File) error {
err := format.Extract(ctx, input, func(ctx context.Context, f archiver.File) error {
// do something with the file
return nil
}

err := format.Extract(ctx, input, fileList, handler)
})
if err != nil {
return err
}
Expand All @@ -141,7 +132,7 @@ if err != nil {
Have an input stream with unknown contents? No problem, archiver can identify it for you. It will try matching based on filename and/or the header (which peeks at the stream):

```go
format, input, err := archiver.Identify("filename.tar.zst", input)
format, input, err := archiver.Identify(ctx, "filename.tar.zst", input)
if err != nil {
return err
}
Expand All @@ -154,8 +145,8 @@ if ex, ok := format.(archiver.Extractor); ok {
}

// or maybe it's compressed and you want to decompress it?
if decom, ok := format.(archiver.Decompressor); ok {
rc, err := decom.OpenReader(unknownFile)
if decomp, ok := format.(archiver.Decompressor); ok {
rc, err := decomp.OpenReader(unknownFile)
if err != nil {
return err
}
Expand All @@ -165,7 +156,7 @@ if decom, ok := format.(archiver.Decompressor); ok {
}
```

`Identify()` works by reading an arbitrary number of bytes from the beginning of the stream (just enough to check for file headers). It buffers them and returns a new reader that lets you re-read them anew.
`Identify()` works by reading an arbitrary number of bytes from the beginning of the stream (just enough to check for file headers). It buffers them and returns a new reader that lets you re-read them anew. If your input stream is `io.Seeker` however, no buffer is created (it uses `Seek()` instead).

### Automatically identifying formats and extracting archives

Expand Down Expand Up @@ -216,7 +207,7 @@ See the [example](./examples/unarchiver) for details.

This is my favorite feature.

Let's say you have a file. It could be a real directory on disk, an archive, a compressed archive, or any other regular file. You don't really care; you just want to use it uniformly no matter what it is.
Let's say you have a file. It could be a real directory on disk, an archive, a compressed archive, or any other regular file (or stream!). You don't really care; you just want to use it uniformly no matter what it is.

Use archiver to simply create a file system:

Expand All @@ -227,7 +218,7 @@ Use archiver to simply create a file system:
// - a compressed archive ("example.tar.gz")
// - a regular file ("example.txt")
// - a compressed regular file ("example.txt.gz")
fsys, err := archiver.FileSystem(filename)
fsys, err := archiver.FileSystem(ctx, filename, nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -257,7 +248,7 @@ if dir, ok := f.(fs.ReadDirFile); ok {
return err
}
for _, e := range entries {
fmt.Println(e.Name())
fmt.Println(e.Extension())
}
}
```
Expand All @@ -270,7 +261,7 @@ if err != nil {
return err
}
for _, e := range entries {
fmt.Println(e.Name())
fmt.Println(e.Extension())
}
```

Expand All @@ -292,6 +283,8 @@ if err != nil {
}
```

**Important .tar note:** Tar files do not efficiently implement file system semantics due to their roots in sequential-access design for tapes. File systems inherently assume random access, but tar files need to be read from the beginning to access something at the end. This is especially slow when the archive is compressed. Optimizations have been implemented to amortize `ReadDir()` calls so that `fs.WalkDir()` only has to scan the archive once, but they use more memory. Open calls require another scan to find the file. It may be more efficient to use `Tar.Extract()` directly if file system semantics are not important to you.

#### Use with `http.FileServer`

It can be used with http.FileServer to browse archives and directories in a browser. However, due to how http.FileServer works, don't directly use http.FileServer with compressed files; instead wrap it like following:
Expand Down

0 comments on commit 374f004

Please sign in to comment.