zipper

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2025 License: MIT Imports: 10 Imported by: 0

README

Zipper

Overview

zipper provides simple, flexible helpers for creating and extracting ZIP archives. It focuses on readability, correctness, and safe filesystem handling.


✨ Features

  • 📁 Zip entire directories
  • 🗺️ Zip files using custom mapping
  • 📝 Zip arbitrary in-memory data
  • 🧠 Create ZIP archives entirely in memory
  • 💾 Write ZIPs directly to disk
  • 🔐 Secure unzip with max-file-size validation
  • 🧱 Uses Go’s os.OpenRoot for path-safe operations

Usage Examples

package main

import (
	"context"

	"github.com/gromey/octopus/zipper"
)

func main() {
	ctx := context.Background()

	// Zip a directory to a file
	writer := zipper.ZipSrcDir("/path/to/src", "archive.zip")

	if err := zipper.ZipToFile(ctx, writer, "/output/archive.zip"); err != nil {
		panic(err)
	}

	// Zip specific files with their own paths
	writer = zipper.ZipSrcMap(map[string]string{
		"/path/image.jpg":  "images",
		"/path/report.pdf": "docs",
	})

	if err := zipper.ZipToFile(ctx, writer, "out.zip"); err != nil {
		panic(err)
	}

	// Produces:
	// out.zip
	// ├── images/
	// │   └── image.jpg
	// └── docs/
	//     └── report.pdf

	// Zip in-memory data
	writer = zipper.ZipSrcData("hello.txt", []byte("Hello"))

	buf, err := zipper.ZipInMemory(ctx, writer)
	if err != nil {
		panic(err)
	}
	_ = buf.Bytes()

	// Unzip with security checks
	var limit int64 = 10 << 20 // 10 MB limit

	if err = zipper.Unzip(ctx, "archive.zip", "/dst", limit); err != nil {
		panic(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Unzip

func Unzip(ctx context.Context, src string, dest string, maxFileSize int64) error

Unzip extracts the contents of the ZIP file at src into the directory dest. The function enforces a maximum file size for entries, returning an error if any file exceeds maxFileSize. Directories and files are recreated with their original permissions. Any extraction or filesystem error is returned.

func ZipInMemory

func ZipInMemory(ctx context.Context, writer func(context.Context, *zip.Writer) error) (*bytes.Buffer, error)

ZipInMemory creates a ZIP archive entirely in memory and returns the resulting bytes.Buffer. The provided writer function is invoked to populate the archive.

func ZipSrcData

func ZipSrcData(filename string, data []byte) func(context.Context, *zip.Writer) error

ZipSrcData returns a writer function that writes the provided data as a single file in the ZIP archive using the given filename. A file header is created with the current modification time, and the resulting file entry is written to the provided zip.Writer.

func ZipSrcDir

func ZipSrcDir(src string, dest string) func(context.Context, *zip.Writer) error

ZipSrcDir returns a writer function that walks the directory tree rooted at src and adds all files and subdirectories into the provided zip.Writer. The relative paths of files are preserved, and the resulting entries are written to the ZIP archive. The root directory itself and the destination ZIP file (if inside the tree) are skipped.

func ZipSrcMap

func ZipSrcMap(src map[string]string) func(context.Context, *zip.Writer) error

ZipSrcMap returns a writer function that adds the files specified in the src map into the provided zip.Writer. Keys represent absolute paths to files, while values represent the directory structure within the ZIP archive. The function ensures directories are created inside the archive before writing their corresponding files.

func ZipToFile

func ZipToFile(ctx context.Context, writer func(context.Context, *zip.Writer) error, dest string) error

ZipToFile creates the destination ZIP file on disk and invokes the provided writer function to populate it.

Types

This section is empty.

Jump to

Keyboard shortcuts

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