xopen

package module
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2026 License: MIT Imports: 17 Imported by: 25 modules, 29 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

GoDoc

xopen

import "github.com/shenwei356/xopen"

xopen makes it easy to get buffered (possibly gzip-, xz-, zstd-, bzip2-, or lz4- compressed) readers and writers. and close all of the associated files. Ropen opens a file for reading. Wopen opens a file for writing.

This packages is forked from https://github.com/brentp/xopen , but I have modified too much :(

Requires Go 1.25.5 or later. Run go test -race ./... to check the package.

Usage

Here's how to get a buffered reader:

// gzipped
rdr, err := xopen.Ropen("some.gz")
// xz compressed
rdr, err := xopen.Ropen("some.xz")
// zstd compressed
rdr, err := xopen.Ropen("some.zst")
// normal
rdr, err := xopen.Ropen("some.txt")
// stdin (possibly gzip-, xz-, or zstd-compressed)
rdr, err := xopen.Ropen("-")
// https://
rdr, err := xopen.Ropen("http://example.com/some-file.txt")
// Cmd
rdr, err := xopen.Ropen("| ls -lh somefile.gz")
// User directory:
rdr, err := xopen.Ropen("~/shenwei356/somefile")

checkError(err)
defer func() { checkError(rdr.Close()) }()

Buf and Ropen return ErrNoContent only for an empty stream that ends normally. Read and decompression errors are returned by the constructor or a subsequent read; callers must check both. Buf leaves its input open if construction fails, while Ropen closes inputs it opened.

Stopping early does not validate unread compressed data. Read to EOF to check the whole stream, then close the reader to release its resources.

Writer

wtr, err := xopen.Wopen("some.gz")
checkError(err)
defer func() { checkError(wtr.Close()) }()

checkError(wtr.Flush())

Close releases resources even after a read or write error; repeated calls return its original result. Readers and writers must not be used concurrently or after Close. As before, closing a stream opened with "-" also closes stdin or stdout.

Set Level before starting concurrent writes. Compression levels, buffer sizes, and default compression settings are unchanged. Invalid gzip, zstd, or bzip2 levels are rejected before the destination is opened or truncated.

Flush flushes gzip and zstd compressor buffers. For parallel LZ4 it completes the current frame and starts another on the next write, so data and write errors are available when Flush returns. Xz and bzip2 require Close to finish their compressed output. Flush does not call File.Sync.

Command inputs (|command args) accept multiple arguments, whitespace, quotes, and escapes without starting a shell. Nonzero exits are returned as errors; closing early stops and reaps the direct child process.

Documentation

Overview

Package xopen makes it easy to get buffered readers and writers. Ropen opens a (possibly gzipped) file/process/http site for buffered reading. Wopen opens a (possibly gzipped) file for buffered writing. Both will use gzip when appropriate and will user buffered IO.

Index

Constants

This section is empty.

Variables

View Source
var ErrDirNotSupported = errors.New("xopen: input is a directory")

ErrDirNotSupported means the path is a directory.

View Source
var ErrNoContent = errors.New("xopen: no content")

ErrNoContent means nothing in the stream/file.

Level is the default compression level of gzip. The default (-1) selects the format default for zstd, bzip2 and LZ4. Set Level before concurrent use; changing it during writes is unsupported.

Functions

func CheckBytes

func CheckBytes(b *bufio.Reader, buf []byte) (bool, error)

CheckBytes peeks at a buffered stream and checks if the first read bytes match.

func Exists

func Exists(path string) bool

Exists checks if a local file exits

func ExpandUser

func ExpandUser(path string) (string, error)

ExpandUser expands ~/path and ~otheruser/path appropriately

func IsBzip2 added in v0.3.0

func IsBzip2(b *bufio.Reader) (bool, error)

IsBzip2 returns true buffered Reader has the bzip2 magic.

func IsGzip

func IsGzip(b *bufio.Reader) (bool, error)

IsGzip returns true buffered Reader has the gzip magic.

func IsLZ4 added in v0.4.0

func IsLZ4(b *bufio.Reader) (bool, error)

IsLZ4 reports modern or legacy LZ4 magic.

func IsStdin

func IsStdin() bool

IsStdin checks if we are getting data from stdin.

func IsXz added in v0.2.0

func IsXz(b *bufio.Reader) (bool, error)

IsXz returns true buffered Reader has the xz magic.

func IsZst added in v0.2.0

func IsZst(b *bufio.Reader) (bool, error)

IsZst returns true buffered Reader has the zstd magic.

func XReader

func XReader(f string) (io.Reader, error)

XReader returns a reader from a url string or a file.

Types

type Reader

type Reader struct {
	*bufio.Reader
	// contains filtered or unexported fields
}

Reader is returned by Ropen

func Buf

func Buf(r io.Reader) (_ *Reader, err error)

Buf returns a buffered reader, detecting supported compression and removing a BOM. Only a clean end of an empty stream returns ErrNoContent; read errors are preserved. On failure, the caller retains ownership of r.

func Ropen

func Ropen(f string) (*Reader, error)

Ropen opens a buffered reader.

func (*Reader) Close

func (r *Reader) Close() error

Close releases the input and decoder, preserving both cleanup errors. Repeated calls return the original result without releasing resources again.

type Writer

type Writer struct {
	*bufio.Writer
	// contains filtered or unexported fields
}

Writer is returned by Wopen

func Wopen

func Wopen(f string) (*Writer, error)

Wopen opens a buffered writer. If f == "-", then stdout will be used. If f endswith ".gz", then the output will be gzipped. If f endswith ".xz", then the output will be xz-compressed. If f endswith ".zst", then the output will be zstd-compressed. If f endswith ".bz2", then the output will be bzip2-compressed. If f endswith ".lz4", then the output will be LZ4-compressed.

func WopenFile

func WopenFile(f string, flag int, perm os.FileMode) (_ *Writer, err error)

WopenFile opens a buffered writer. If f == "-", then stdout will be used. If f endswith ".gz", then the output will be gzipped. If f endswith ".xz", then the output will be xz-compressed. If f endswith ".lz4", then the output will be LZ4-compressed.

func (*Writer) Close

func (w *Writer) Close() error

Close finishes compression and closes the file even when flushing fails. Repeated calls return the original result.

func (*Writer) Flush

func (w *Writer) Flush() (err error)

Flush writes buffered data without syncing the file. LZ4 finishes a frame; xz and bzip2 require Close to finish their compressor buffers.

Jump to

Keyboard shortcuts

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