libarchive

package module
v0.0.0-...-2d6f5e5 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2026 License: MIT Imports: 9 Imported by: 0

README

libarchive

Golang bindings for the libarchive library.

Simple Extraction Example

package main

import (
	"bytes"
	"fmt"
	"io"
	"os"

	ar "github.com/CiscoSecurityServices/go-libarchive"
)

func printContents(filename string) {
	fmt.Println("file ", filename)
	file, err := os.Open(filename)
	if err != nil {
		fmt.Printf("Error while opening file:\n %s\n", err)
		return
	}
	reader, err := ar.NewReader(file)
	if err != nil {
		fmt.Printf("Error on NewReader\n %s\n", err)
	}
	defer reader.Close()
	for {
		entry, err := reader.Next()
		if err == io.EOF {
			break
		}
		if errors.Is(err, ErrArchiveWarn) {
			// do something with the warning
			fmt.Println(err)
			continue
		}
		if err != nil {
			fmt.Printf("Error on reader.Next():\n%s\n", err)
			return
		}
		fmt.Printf("Name %s\n", entry.PathName())
		var buf bytes.Buffer
		size, err := buf.ReadFrom(reader)


		if err != nil {
			fmt.Printf("Error on reading entry from archive:\n%s\n", err)
		}
		if size > 0 {
			fmt.Println("Contents:\n***************", buf.String(), "*********************")
		}
	}
}

func main() {
	for _, filename := range os.Args[1:] {
		printContents(filename)
	}
}

Docker

docker build -t lib .
docker run --rm -it -v $PWD:/app lib:latest go test ./...

Test

When building a test fixture, use the folder in fixtures/test for golden data.

Acknowledgments

based on mstoykov's go libarchive which is based on based on robxu9's go-libarchive

Documentation

Index

Constants

View Source
const (
	ARCHIVE_EOF    = C.ARCHIVE_EOF
	ARCHIVE_OK     = C.ARCHIVE_OK
	ARCHIVE_RETRY  = C.ARCHIVE_RETRY
	ARCHIVE_WARN   = C.ARCHIVE_WARN
	ARCHIVE_FAILED = C.ARCHIVE_FAILED
	ARCHIVE_FATAL  = C.ARCHIVE_FATAL
)

Variables

View Source
var (
	FileTypeRegFile      = C.AE_IFREG
	FileTypeSymLink      = C.AE_IFLNK
	FileTypeSocket       = C.AE_IFSOCK
	FileTypeCharDev      = C.AE_IFCHR
	FileTypeBlkDev       = C.AE_IFBLK
	FileTypeDir          = C.AE_IFDIR
	FileTypeFIFO         = C.AE_IFIFO
	LibArchiveVersion    = C.ARCHIVE_VERSION_ONLY_STRING
	LibArchiveVersionInt = C.ARCHIVE_VERSION_NUMBER
)
View Source
var (
	ErrArchiveEOF             = io.EOF
	ErrArchiveFatal           = wrapError{/* contains filtered or unexported fields */}
	ErrArchiveFailed          = wrapError{/* contains filtered or unexported fields */}
	ErrArchiveRetry           = wrapError{/* contains filtered or unexported fields */}
	ErrArchiveWarn            = wrapError{/* contains filtered or unexported fields */}
	ErrArchiveFatalClosing    = ErrArchiveFatal.wrap("critical error, archive closing")
	ErrUnexpectedEOF          = fmt.Errorf("unexpected end of file")
	ErrInvalidHeaderSignature = fmt.Errorf("invalid header signature")
)

Functions

This section is empty.

Types

type ArchiveEntry

type ArchiveEntry interface {
	// FileInfo describing archive_entry
	Stat() os.FileInfo
	// The name of the entry
	PathName() string
	Symlink() string
	Hardlink() string
	IsHardlink() bool
}

ArchiveEntry represents an libarchive archive_entry

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader represents libarchive archive

func NewReader

func NewReader(reader io.Reader) (*Reader, error)

NewReader returns new Archive by calling archive_read_open

func NewReaderWithBufferSize

func NewReaderWithBufferSize(reader io.Reader, bufferSize int) (r *Reader, err error)

NewReaderWithBufferSize returns new Archive by calling archive_read_open with specified buffer size

func (*Reader) Close

func (r *Reader) Close() error

Close closes the underlying libarchive archive and frees it, using Close since its more common in go to always call Close (rather than having two methods)

func (*Reader) IsRaw

func (r *Reader) IsRaw() bool

Must be called after Next

func (*Reader) Next

func (r *Reader) Next() (ArchiveEntry, error)

Next calls archive_read_next_header and returns an interpretation of the ArchiveEntry which is a wrapper around libarchive's archive_entry, or Err.

ErrArchiveEOF is returned when there is no more to be read from the archive

func (*Reader) Read

func (r *Reader) Read(b []byte) (n int, err error)

Read calls archive_read_data which reads the current archive_entry. It acts as io.Reader.Read in any other aspect

func (*Reader) ReadClose

func (r *Reader) ReadClose() error

Close closes the underlying libarchive archive calling archive read_close

func (*Reader) ReadFree

func (r *Reader) ReadFree() error

Free frees the resources the underlying libarchive archive is using calling archive_read_free Note this calls

Directories

Path Synopsis
example
simple-extract command
simple-print command

Jump to

Keyboard shortcuts

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