-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompressed_io.go
88 lines (77 loc) · 2.26 KB
/
compressed_io.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (c) 2025 Neomantra Corp
// Reader/Writer Compression helpers
//
// Adapted from Neomantra's Gist, but simplified to only support zstd.:
//
// https://gist.github.com/neomantra/691a6028cdf2ac3fc6ec97d00e8ea802
//
package dbn
import (
"io"
"os"
"strings"
"github.com/klauspost/compress/zstd"
)
///////////////////////////////////////////////////////////////////////////////
// Returns an io.Writer for the given filename, or os.Stdout if filename is "-". Also returns a closing function to defer and any error.
// If the filename ends in ".zst" or ".zstd", or if useZstd is true, the writer will zstd-compress the output.
func MakeCompressedWriter(filename string, useZstd bool) (io.Writer, func(), error) {
var writer io.Writer
var closer io.Closer
fileCloser := func() {
if closer != nil {
closer.Close()
}
}
if filename != "-" {
if file, err := os.Create(filename); err == nil {
writer, closer = file, file
} else {
return nil, nil, err
}
} else {
writer, closer = os.Stdout, nil
}
if useZstd || strings.HasSuffix(filename, ".zst") || strings.HasSuffix(filename, ".zstd") {
zstdWriter, err := zstd.NewWriter(writer)
if err != nil {
fileCloser()
return nil, nil, err
}
zstdCloser := func() {
zstdWriter.Close()
fileCloser()
}
return zstdWriter, zstdCloser, nil
} else {
return writer, fileCloser, nil
}
}
///////////////////////////////////////////////////////////////////////////////
// Returns a io.Reader for the given filename, or os.Stdout if filename is "-". Also returns a closing function to defer.
// If the filename ends in ".zst" or ".zstd", or if useZstd is true, the reader will zstd-decompress the input.
func MakeCompressedReader(filename string, useZstd bool) (io.Reader, io.Closer, error) {
var reader io.Reader
var closer io.Closer
if filename != "-" {
if file, err := os.Open(filename); err == nil {
reader, closer = file, file
} else {
return nil, nil, err
}
} else {
reader, closer = os.Stdin, nil
}
var err error
if useZstd || strings.HasSuffix(filename, ".zst") || strings.HasSuffix(filename, ".zstd") {
reader, err = zstd.NewReader(reader)
}
if err != nil {
// clean up file
if closer != nil {
closer.Close()
}
return nil, nil, err
}
return reader, closer, nil
}