go7z

package module
v0.0.0-...-9c09b6b Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2019 License: MIT Imports: 15 Imported by: 13

README

go7z

A native Go 7z archive reader.

Features:

  • Development in early stages.
  • Very little tests.
  • Medium probability of crashes.
  • Medium probability of using all memory.
  • Decompresses:

Usage

Extracting an archive:

package main

import (
	"io"
	"os"

	"github.com/saracen/go7z"
)

func main() {
	sz, err := go7z.OpenReader("hello.7z")
	if err != nil {
		panic(err)
	}
	defer sz.Close()

	for {
		hdr, err := sz.Next()
		if err == io.EOF {
			break // End of archive
		}
		if err != nil {
			panic(err)
		}

		// If empty stream (no contents) and isn't specifically an empty file...
		// then it's a directory.
		if hdr.IsEmptyStream && !hdr.IsEmptyFile {
			if err := os.MkdirAll(hdr.Name, os.ModePerm); err != nil {
				panic(err)
			}
			continue
		}

		// Create file
		f, err := os.Create(hdr.Name)
		if err != nil {
			panic(err)
		}
		defer f.Close()

		if _, err := io.Copy(f, sz); err != nil {
			panic(err)
		}
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotSupported is returned when an unrecognized archive format is
	// encountered.
	ErrNotSupported = errors.New("not supported")

	// ErrDecompressorNotFound is returned when a requested decompressor has not
	// been registered.
	ErrDecompressorNotFound = errors.New("decompressor not found")
)

Functions

func RegisterDecompressor

func RegisterDecompressor(method uint32, dcomp Decompressor)

RegisterDecompressor registers a decompressor.

Types

type Decompressor

type Decompressor func(r []io.Reader, options []byte, unpackSize uint64, ro *ReaderOptions) (io.Reader, error)

Decompressor is a handler function called when a registered decompressor is initialized.

type ReadCloser

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

ReadCloser provides an io.ReadCloser for the archive when opened with OpenReader.

func OpenReader

func OpenReader(name string) (*ReadCloser, error)

OpenReader will open the 7z file specified by name and return a ReadCloser.

func (*ReadCloser) Close

func (rc *ReadCloser) Close() error

Close closes the 7z file, rendering it unusable for I/O.

type Reader

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

Reader is a 7z archive reader.

func NewReader

func NewReader(r io.ReaderAt, size int64) (*Reader, error)

NewReader returns a new Reader reading from r, which is assumed to have the given size in bytes.

func (*Reader) Next

func (sz *Reader) Next() (*headers.FileInfo, error)

Next advances to the next entry in the 7z archive.

io.EOF is returned at the end of the input.

func (*Reader) Read

func (sz *Reader) Read(p []byte) (int, error)

Read reads from the current file in the 7z archive. It returns (0, io.EOF) when it reaches the end of that file, until Next is called to advance to the next file.

type ReaderOptions

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

ReaderOptions are optional options to configure a 7z archive reader.

func (*ReaderOptions) Password

func (o *ReaderOptions) Password() string

Password returns the set password. This will call the password callback supplied to SetPasswordCallback() if no password is set.

func (*ReaderOptions) SetPassword

func (o *ReaderOptions) SetPassword(password string)

SetPassword sets the password used for extraction.

func (*ReaderOptions) SetPasswordCallback

func (o *ReaderOptions) SetPasswordCallback(cb func() string)

SetPasswordCallback sets the callback thats used if a password is required, but wasn't supplied with SetPassword()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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