expand

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2026 License: GPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package expand is for use with the archive/zip package.

Expand was a decompression method authored in 1989 by Phil Katz and used in his MS-DOS compression tool, PKZip v0.90.

It is referred to as ZIP Reduce/Expand, methods 2, 3, 4, 5, and is based on the LZ77 algorithm.

This package was first prompted using Gemini in August 2026 and manually cleaned to be idiomatic and readable.

In 2020, Jason Summers authored a collection of public domain libraries named oldunzip. In-depth information on Reduce was authored by Hans Wennborg in 2021, who programmed the public domain reduce.c.

Example

Example demonstrates the basic usage of the package.

package main

import (
	"archive/zip"
	"fmt"
	"hash/crc32"
	"io"
	"path/filepath"

	"github.com/Defacto2/archive/expand"
)

func main() {
	const testdata = "testdata"
	const testFile = "example-r.zip"

	// Register the Expand decompressors with the archive zip package.
	expand.Register()

	name := filepath.Join(testdata, testFile)
	rc, _ := zip.OpenReader(name)
	defer rc.Close()

	for n, f := range rc.File {
		fr, _ := f.Open()
		hasher := crc32.NewIEEE()
		size := int64(f.UncompressedSize64) //nolint:gosec
		_, _ = io.CopyN(hasher, fr, size)

		fmt.Printf("%d. Decompressed method %d: %s, %dB\n"+
			"Stored zip crc32 %x, and copied to buffer: %x\n",
			n+1, f.Method, f.Name, f.UncompressedSize64, f.CRC32, hasher.Sum32())

		fr.Close()
	}
}
Output:
1. Decompressed method 5: R.BIN, 2048B
Stored zip crc32 8314ddd9, and copied to buffer: 8314ddd9

Index

Examples

Constants

View Source
const (
	Expand2 uint16 = iota + 2 // Method 2: Reduced/Expand with compression factor 1
	Expand3                   // Method 3: Reduced/Expand with compression factor 2
	Expand4                   // Method 4: Reduced/Expand with compression factor 3
	Expand5                   // Method 5: Reduced/Expand with compression factor 4
)

Variables

View Source
var (
	// ErrCorruptData is returned when the compressed data stream is malformed.
	ErrCorruptData = errors.New("expand: corrupt compressed data")
	// ErrMethod is returned when an invalid compression method is specified.
	ErrMethod = errors.New("expand: invalid method for expand dcomp")
	// ErrOverflow is returned when an integer cannot be safely wrapped to a new type.
	ErrOverflow = errors.New("expand: integer overflow")
)

Functions

func Register

func Register()

Register the expand decompressor methods globally for archive/zip.

Types

This section is empty.

Jump to

Keyboard shortcuts

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