zip_files v1

A simple Go library for creating and extracting ZIP archives in memory. Create ZIP files by adding bytes, or extract files from existing ZIP archives into a map of bytes.

Repository

Installation

go get -u github.com/metadiv-io/zip_files

Usage

Creating ZIP Archives

zipper := zip_files.New()

// Add files to the archive
zipper.AddFile("hello.txt", []byte("Hello, World!"))
zipper.AddFile("data.json", []byte(`{"message": "Hello from JSON"}`))

// Get the number of files
count := zipper.CountFiles() // returns 2

// Get list of file names
files := zipper.Files() // returns []string{"hello.txt", "data.json"}

// Create the ZIP archive
zipBytes, err := zipper.Zip()
if err != nil {
    log.Fatal(err)
}

// Now zipBytes contains the ZIP archive data
// You can write it to a file or send it over network

Extracting ZIP Archives

// Assuming you have ZIP archive data in zipBytes
files, err := zip_files.Unzip(zipBytes)
if err != nil {
    log.Fatal(err)
}

// files is now a map[string][]byte containing the extracted files
// Access files by their names
helloContent := files["hello.txt"] // []byte("Hello, World!")
jsonContent := files["data.json"]  // []byte(`{"message": "Hello from JSON"}`)

API Reference

New() *Zipper

Creates a new Zipper instance for creating ZIP archives.

AddFile(filename string, file []byte)

Adds a file to the ZIP archive with the specified filename and content.

CountFiles() int

Returns the number of files currently added to the archive.

Files() []string

Returns a slice of filenames currently added to the archive.

Zip() ([]byte, error)

Creates and returns the ZIP archive as a byte slice.

Last updated