-
Notifications
You must be signed in to change notification settings - Fork 14
/
archiver_options.go
74 lines (65 loc) · 1.89 KB
/
archiver_options.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
package fastzip
import (
"errors"
)
var (
ErrMinConcurrency = errors.New("concurrency must be at least 1")
)
// ArchiverOption is an option used when creating an archiver.
type ArchiverOption func(*archiverOptions) error
type archiverOptions struct {
method uint16
concurrency int
bufferSize int
stageDir string
offset int64
}
// WithArchiverMethod sets the zip method to be used for compressible files.
func WithArchiverMethod(method uint16) ArchiverOption {
return func(o *archiverOptions) error {
o.method = method
return nil
}
}
// WithArchiverConcurrency will set the maximum number of files to be
// compressed concurrently. The default is set to runtime.NumCPU().
func WithArchiverConcurrency(n int) ArchiverOption {
return func(o *archiverOptions) error {
if n <= 0 {
return ErrMinConcurrency
}
o.concurrency = n
return nil
}
}
// WithArchiverBufferSize sets the buffer size for each file to be compressed
// concurrently. If a compressed file's data exceeds the buffer size, a
// temporary file is written (to the stage directory) to hold the additional
// data. The default is 2 mebibytes, so if concurrency is 16, 32 mebibytes of
// memory will be allocated.
func WithArchiverBufferSize(n int) ArchiverOption {
return func(o *archiverOptions) error {
if n < 0 {
n = 0
}
o.bufferSize = n
return nil
}
}
// WithStageDirectory sets the directory to be used to stage compressed files
// before they're written to the archive. The default is the directory to be
// archived.
func WithStageDirectory(dir string) ArchiverOption {
return func(o *archiverOptions) error {
o.stageDir = dir
return nil
}
}
// WithArchiverOffset sets the offset of the beginning of the zip data. This
// should be used when zip data is appended to an existing file.
func WithArchiverOffset(n int64) ArchiverOption {
return func(o *archiverOptions) error {
o.offset = n
return nil
}
}