zt

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2022 License: MIT Imports: 8 Imported by: 0

README

zt: the transparent io.Reader for compressed data

Package zt provides types and functions that allow to transparently handle an incoming stream of bytes, whether it's compressed – by decompressing it on the fly – or uncompressed, in which case the bytes are simply forwarded as-is.

One example of use is for CLI programs that wants to support reading compressed data from standard input. By using zt your program can transparently read compressed and uncompressed data.

Currently supported compression algorithms are:

Example program: a transparent decompressor.

package main

import (
	"io"
	"os"

	"github.com/arl/zt"
)

func main() {
	r, err := zt.NewReader(os.Stdin)
	if err != nil { /* handle error */ }

	_, err = io.Copy(os.Stdout, r)
	if err != nil { /* handle error */ }
}
go run main.go < /some/file.gz  # decompress gzip-compressed file to stdout
go run main.go < /some/file.zst # decompress zstandard-compressed file to stdout
go run main.go < /some/file.bz2 # decompress bzip2-compressed file to stdout
go run main.go < /some/file.zz  # decompress zlib-compressed file to stdout
go run main.go < /some/file     # print non-compressed file to stdout

Documentation

Overview

Package zt provides types and functions that allow to transparently handle an incoming stream of bytes, whether it's compressed – by decompressing it on the fly – or uncompressed, in which case the bytes are simply forwarded as-is.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewReader

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

NewReader returns an io.ReadCloser that reads from r, either decoding the compressed stream (in case the algorithm used to compress it is both supported and detected), or just forwarding it.

In order for NewReader to succeeds, at least 4 bytes must be read from r, in order to detect the presence of a compression algorithm and its type.

Note: this utility provided as a best effort, it's certainly possible to trick it into thinking a stream of bytes contains zstd or gzip compressed data, while in fact it's not.

Types

This section is empty.

Jump to

Keyboard shortcuts

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