gobzip2

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: BSD-3-Clause Imports: 6 Imported by: 1

README

GoDoc Go Report Card Coverage Status

gobzip2

Pure Go implementation of bzip2 compression and decompression, based on the bzip2 1.0.8 reference C implementation.

Unlike the standard library's compress/bzip2 which only supports decompression, this package provides both compression and decompression.

Install

go get github.com/KarpelesLab/gobzip2

Usage

Compression
var buf bytes.Buffer
w := gobzip2.NewWriter(&buf)
w.Write([]byte("hello, world"))
w.Close()
Decompression
r := gobzip2.NewReader(bytes.NewReader(compressed))
data, err := io.ReadAll(r)
CLI
go install github.com/KarpelesLab/gobzip2/cmd/gobzip2@latest

# Compress
gobzip2 file.txt

# Decompress
gobzip2 -d file.txt.bz2

# Pipe
cat file | gobzip2 | gobzip2 -d

Documentation

Overview

Package gobzip2 implements reading and writing of bzip2 format compressed data.

This is a pure Go implementation based on the bzip2 1.0.8 reference C code. It provides both compression and decompression, unlike the standard library's compress/bzip2 package which only supports decompression.

Index

Constants

View Source
const (
	BestSpeed          = 1
	DefaultCompression = 9
	BestCompression    = 9
)

Compression levels.

View Source
const ParallelCPU = -1

ParallelCPU can be used as WriterOptions.Concurrency to use all available CPUs.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChecksumError

type ChecksumError struct {
	Expected, Got uint32
}

ChecksumError is returned when the CRC of the decompressed data does not match the expected value.

func (*ChecksumError) Error

func (e *ChecksumError) Error() string

type Reader

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

Reader decompresses bzip2-compressed data read from the underlying reader.

func NewReader

func NewReader(r io.Reader) *Reader

NewReader returns a new Reader that decompresses bzip2 data from r.

func (*Reader) Close

func (r *Reader) Close() error

Close releases resources. It does not close the underlying reader.

func (*Reader) Read

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

Read decompresses data into p.

func (*Reader) Reset

func (r *Reader) Reset(src io.Reader)

Reset discards internal state and switches to reading from src.

type StructuralError

type StructuralError string

StructuralError is returned when the bzip2 data is found to be syntactically invalid.

func (StructuralError) Error

func (e StructuralError) Error() string

type Writer

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

Writer compresses data written to it in bzip2 format.

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter returns a new Writer that compresses data at the default level (9) and writes the compressed output to w.

func NewWriterLevel

func NewWriterLevel(w io.Writer, level int) (*Writer, error)

NewWriterLevel returns a new Writer with the given block size level (1-9).

func NewWriterOptions

func NewWriterOptions(w io.Writer, opts *WriterOptions) (*Writer, error)

NewWriterOptions returns a new Writer with the given options.

func (*Writer) Close

func (w *Writer) Close() error

Close flushes any pending data and writes the bzip2 stream trailer.

func (*Writer) Reset

func (w *Writer) Reset(dst io.Writer)

Reset discards internal state and switches to writing to dst.

func (*Writer) Write

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

Write compresses p and writes it to the underlying writer.

type WriterOptions

type WriterOptions struct {
	// Level is the block size level (1-9). Default is 9 (900KB blocks).
	Level int

	// Concurrency is the number of goroutines used for compression.
	// 0 or 1 means single-threaded (no goroutine overhead).
	// Higher values enable parallel block compression.
	// Use ParallelCPU for runtime.NumCPU().
	Concurrency int

	// WorkFactor controls when the fallback sorting algorithm kicks in.
	// 0 means use the default (30). Range: 1-250.
	WorkFactor int
}

WriterOptions configures the compressor.

Directories

Path Synopsis
cmd
gobzip2 command
Command gobzip2 compresses and decompresses files in bzip2 format.
Command gobzip2 compresses and decompresses files in bzip2 format.

Jump to

Keyboard shortcuts

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