archive

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package archive provides types and functions for working with ZSTD compressed archives.

Index

Constants

View Source
const (
	// DefaultCompressionLevel is the default compression level for encoding.
	DefaultCompressionLevel = zstd.BestSpeed
)
View Source
const HeaderSize = 24 // 4 + 4 + 8 + 8 bytes

HeaderSize is the fixed binary size of an archive header.

Variables

View Source
var Magic = [4]byte{0x5a, 0x53, 0x54, 0x44} // "ZSTD"

Magic bytes identifying a ZSTD archive header.

Functions

func Encode

func Encode(dst io.WriteSeeker, data []byte, opts ...WriterOption) error

Encode compresses data and writes it as an archive to dst.

func ReadAll

func ReadAll(r io.ReadSeeker) ([]byte, error)

ReadAll reads the entire decompressed content from an archive.

Types

type Header struct {
	Magic            [4]byte
	HeaderLength     uint32
	Length           uint64 // Uncompressed size
	CompressedLength uint64 // Compressed size
}

Header represents the header of a compressed archive file.

func NewHeader

func NewHeader(uncompressedSize, compressedSize uint64) *Header

NewHeader creates a new archive header with the given sizes.

func (*Header) DecodeFrom

func (h *Header) DecodeFrom(data []byte)

DecodeFrom reads the header from the given buffer. Does not validate - use UnmarshalBinary for validation.

func (*Header) EncodeTo

func (h *Header) EncodeTo(buf []byte)

EncodeTo writes the header to the given buffer. The buffer must be at least HeaderSize bytes.

func (*Header) MarshalBinary

func (h *Header) MarshalBinary() ([]byte, error)

MarshalBinary encodes the header to binary format. Uses direct encoding to avoid allocations.

func (*Header) Size

func (h *Header) Size() int

Size returns the binary size of the header.

func (*Header) UnmarshalBinary

func (h *Header) UnmarshalBinary(data []byte) error

UnmarshalBinary decodes the header from binary format. Uses direct decoding to avoid allocations.

func (*Header) Validate

func (h *Header) Validate() error

Validate checks the header for validity.

type Reader

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

Reader wraps an io.ReadSeeker to provide decompression of archive data.

func NewReader

func NewReader(r io.ReadSeeker) (*Reader, error)

NewReader creates a new archive reader from the given source. It reads and validates the header, then returns a reader for the decompressed content.

func (*Reader) Close

func (r *Reader) Close() error

Close closes the reader.

func (*Reader) CompressedLength

func (r *Reader) CompressedLength() int

CompressedLength returns the compressed data length.

func (*Reader) Header

func (r *Reader) Header() *Header

Header returns the archive header.

func (*Reader) Length

func (r *Reader) Length() int

Length returns the uncompressed data length.

func (*Reader) Read

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

Read reads decompressed data into p.

type Writer

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

Writer wraps an io.WriteSeeker to provide compression of archive data.

func NewWriter

func NewWriter(dst io.WriteSeeker, uncompressedSize uint64, opts ...WriterOption) (*Writer, error)

NewWriter creates a new archive writer that writes to dst. The uncompressedSize is the expected size of the uncompressed data.

func (*Writer) Close

func (w *Writer) Close() error

Close finalizes the archive by updating the header with the compressed size.

func (*Writer) Write

func (w *Writer) Write(p []byte) (n int, err error)

Write writes compressed data.

type WriterOption

type WriterOption func(*Writer)

WriterOption configures a Writer.

func WithCompressionLevel

func WithCompressionLevel(level int) WriterOption

WithCompressionLevel sets the compression level for the writer.

Jump to

Keyboard shortcuts

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