zipstream

package module
v0.0.0-...-0d81af8 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2026 License: MIT Imports: 3 Imported by: 0

README

zipstream

Small helper package for streaming filtered zip archives.

It is able to rebuild zip files while keeping only selected entries. It copies entry data 1:1, so files are not decompressed or recompressed. This is useful when you want to dynamically serve a modified zip archive, without first writing the complete output to disk.

Example

// Assuming you have some sort of source zip file, e.g. a file object
source, _ := os.Open("source.zip")
info, _ := file.Stat()
size := info.Size()

resultStream, resultSize, err := zipstream.StreamFiltered(
	source, size,
	// Determine what to keep, e.g. only txt files in this case
	func(file *zip.File) bool {
		return strings.HasSuffix(file.Name, ".txt")
	},
)

// do whatever ...

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ArchiveSizeFiltered

func ArchiveSizeFiltered(files []*zip.File, keep Predicate) (int64, bool)

ArchiveSizeFiltered returns the exact byte length of the archive generated by WriteFiltered, if it can be represented without zip64 metadata.

func RewriteRawHeader

func RewriteRawHeader(source *zip.FileHeader) *zip.FileHeader

RewriteRawHeader prepares a zip.FileHeader for CreateRaw.

func StreamFiltered

func StreamFiltered(
	source ReaderAtCloser,
	size int64,
	keep Predicate,
	optionList ...Option,
) (io.ReadCloser, int64, error)

StreamFiltered streams a new zip archive containing only entries for which keep(file) returns true.

The source is owned by this function and will be closed when streaming ends, so the caller should not close it.

func WriteFiltered

func WriteFiltered(
	dst io.Writer,
	files []*zip.File,
	keep Predicate,
	bufferSize int,
) error

WriteFiltered writes a filtered zip archive to dst.

Types

type Option

type Option func(*options)

Option is a function that modifies the options for the zipstream.

func WithBufferSize

func WithBufferSize(size int) Option

WithBufferSize returns an Option that sets the buffer size for the zipstream.

type Predicate

type Predicate func(file *zip.File) bool

Predicate is a function that returns true if a zip.File should be kept in the output archive.

type ReaderAtCloser

type ReaderAtCloser interface {
	io.ReaderAt
	io.Closer
}

ReaderAtCloser is an interface that combines io.ReaderAt and io.Closer.

It is required because zip.NewReader requires an io.ReaderAt, but we also need to close the source when done.

Jump to

Keyboard shortcuts

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