tarfs

package module
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 6, 2021 License: Unlicense Imports: 9 Imported by: 16

README

go-tarfs

Go Reference GitHub release (latest SemVer) GitHub Workflow Status License Unlicense

Read a tar file contents using go1.16 io/fs abstraction

Usage

⚠️ go-tarfs needs go>=1.16

Install:

go get github.com/nlepage/go-tarfs

Use:

package main

import (
    "os"

    tarfs "github.com/nlepage/go-tarfs"
)

func main() {
    tf, err := os.Open("path/to/archive.tar")
	if err != nil {
		panic(err)
	}
	defer tf.Close()

	tfs, err := tarfs.New(tf)
	if err != nil {
		panic(err)
	}

	f, err := tfs.Open("path/to/some/file")
	if err != nil {
		panic(err)
	}
	// defer f.Close() isn't necessary, it is a noop
    
	// use f...
}

More information at pkg.go.dev/github.com/nlepage/go-tarfs

Caveats

For now, no effort is done to support symbolic links.

Author

👤 Nicolas Lepage

🤝 Contributing

Contributions, issues and feature requests are welcome!

Feel free to check issues page.

Show your support

Give a ⭐️ if this project helped you!

📝 License

This project is unlicensed, it is free and unencumbered software released into the public domain.

Documentation

Overview

Example (HttpFileServer)

ExampleHTTPFileServer demonstrates how to serve the contents of a tar file using HTTP

tf, err := os.Open("test.tar")
if err != nil {
	panic(err)
}
defer tf.Close()

tfs, err := tarfs.New(tf)
if err != nil {
	panic(err)
}

srv := httptest.NewServer(http.FileServer(http.FS(tfs)))
defer srv.Close()

res, err := srv.Client().Get(srv.URL + "/dir1/dir11/file111")
if err != nil {
	panic(err)
}

if _, err := io.Copy(os.Stdout, res.Body); err != nil {
	panic(err)
}
res.Body.Close()
Output:

file111
Example (OpenAndStat)

ExampleOpenAndStat demonstrates how to open a file from a tar and read file info

tf, err := os.Open("test.tar")
if err != nil {
	panic(err)
}
defer tf.Close()

tfs, err := tarfs.New(tf)
if err != nil {
	panic(err)
}

f, err := tfs.Open("dir1/dir11/file111")
if err != nil {
	panic(err)
}

fi, err := f.Stat()
if err != nil {
	panic(err)
}

fmt.Println(fi.Name())
fmt.Println(fi.IsDir())
fmt.Println(fi.Size())
Output:

file111
false
7

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrNotDir = errors.New("not a directory")
	ErrDir    = errors.New("is a directory")
)

Generic errors

Functions

func New

func New(r io.Reader) (fs.FS, error)

New creates a new tar fs.FS from r

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL