zip

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: MIT Imports: 6 Imported by: 1

README

zip

Static Badge Build Go Reference codecov Release Go Report Card License

This package provides a simple abstraction to read and process files from a ZIP archive.

It exposes a Reader interface that allows consumers to interact with compressed files without dealing directly with the underlying archive/zip implementation. The package is designed to be straightforward, testable, and easy to integrate into your codebase.

Install

$> go get github.com/gofast-pkg/zip@latest

Key Features

  • Unified interface (Reader)
  • Retrieve the number of files in the archive
  • Read file contents by index
  • Access file metadata
  • Write file contents directly to an io.Writer
  • In-memory processing

Files are accessed by index, ensuring deterministic behavior aligned with the ZIP structure.

Usage

Check the example and the go documentation for more details.

Contributing

 ❕  Use issues for everything

Read more informations with the CONTRIBUTING_GUIDE

For all changes, please update the CHANGELOG.txt file by replacing the existant content.

Thank you  🙏  👍 

Made with contrib.rocks.

Licence

MIT

Documentation

Overview

Package zip process a compressed file / body.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrNilReader         = errors.New("reader is nil")
	ErrNilWriter         = errors.New("writer is nil")
	ErrToReadReader      = errors.New("fails to read the reader")
	ErrToCreateZipReader = errors.New("fails to create a zip.Reader")
	ErrInvalidIndex      = errors.New("file index reference is invalid")
	ErrToReadFile        = errors.New("fails to read file in the zip.Reader")
	ErrToWriteFile       = errors.New("fails to write file")
	ErrToOpenFile        = errors.New("fails to open file in the zip.Reader")
)

Errors supported by the zip.Reader operations.

Functions

This section is empty.

Types

type Reader

type Reader interface {
	// NumFile returns the number of files in the compressed file / body.
	NumFile() int
	// Read the content of a file and return the body.
	Read(index int) ([]byte, error)
	// InfoFile returns the information of a file.
	InfoFile(index int) (fs.FileInfo, error)
	// Write the content in the io.Writer parameter.
	Write(w io.Writer, index int) error
}

Reader is the interface that wraps the basic methods to process compressed file(s).

func NewReader

func NewReader(input io.ReadCloser) (Reader, error)

NewReader returns a Reader interface for the zip. It receives a io.ReadCloser interface from http request or file.

Example
package main

import (
	"errors"
	"fmt"
	"os"

	"github.com/gofast-pkg/zip"
	"github.com/gofast-pkg/zip/testdata"
)

func main() {
	file, err := os.Open(testdata.TestZipFile)
	if err != nil {
		panic(err)
	}
	defer func() {
		if err = errors.Join(err, file.Close()); err != nil {
			panic(err)
		}
	}()

	r, err := zip.NewReader(file)
	if err != nil {
		panic(err)
	}

	fmt.Println(r.NumFile())

	// Iterate through the files in the archive,
	for i := 0; i < r.NumFile(); i++ {
		var content []byte
		if content, err = r.Read(i); err != nil {
			panic(err)
		}

		fmt.Println(string(content[:5]))
		fmt.Println(string(content[len(content)-5:]))
	}
}
Output:
1
annee
;eur;

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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