forked from mholt/archiver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterfaces.go
93 lines (81 loc) · 3.12 KB
/
interfaces.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
89
90
91
92
93
package archiver
import (
"context"
"io"
)
// Format represents either an archive or compression format.
type Format interface {
// Name returns the name of the format.
Name() string
// Match returns true if the given name/stream is recognized.
// One of the arguments is optional: filename might be empty
// if working with an unnamed stream, or stream might be
// empty if only working with a filename. The filename should
// consist only of the base name, not a path component, and is
// typically used for matching by file extension. However,
// matching by reading the stream is preferred. Match reads
// only as many bytes as needed to determine a match. To
// preserve the stream through matching, you should either
// buffer what is read by Match, or seek to the last position
// before Match was called.
Match(filename string, stream io.Reader) (MatchResult, error)
}
// Compression is a compression format with both compress and decompress methods.
type Compression interface {
Format
Compressor
Decompressor
}
// Archival is an archival format with both archive and extract methods.
type Archival interface {
Format
Archiver
Extractor
}
// Compressor can compress data by wrapping a writer.
type Compressor interface {
// OpenWriter wraps w with a new writer that compresses what is written.
// The writer must be closed when writing is finished.
OpenWriter(w io.Writer) (io.WriteCloser, error)
}
// Decompressor can decompress data by wrapping a reader.
type Decompressor interface {
// OpenReader wraps r with a new reader that decompresses what is read.
// The reader must be closed when reading is finished.
OpenReader(r io.Reader) (io.ReadCloser, error)
}
// Archiver can create a new archive.
type Archiver interface {
// Archive writes an archive file to output with the given files.
//
// Context cancellation must be honored.
Archive(ctx context.Context, output io.Writer, files []File) error
}
// ArchiverAsync is an Archiver that can also create archives
// asynchronously by pumping files into a channel as they are
// discovered.
type ArchiverAsync interface {
Archiver
// Use ArchiveAsync if you can't pre-assemble a list of all
// the files for the archive. Close the files channel after
// all the files have been sent.
ArchiveAsync(ctx context.Context, output io.Writer, files <-chan File) error
}
// Extractor can extract files from an archive.
type Extractor interface {
// Extract reads the files at pathsInArchive from sourceArchive.
// If pathsInArchive is nil, all files are extracted without discretion.
// If pathsInArchive is empty, no files are extracted.
// If a path refers to a directory, all files within it are extracted.
// Extracted files are passed to the handleFile callback for handling.
//
// Context cancellation must be honored.
Extract(ctx context.Context, sourceArchive io.Reader, pathsInArchive []string, handleFile FileHandler) error
}
// Inserter can insert files into an existing archive.
type Inserter interface {
// Insert inserts the files into archive.
//
// Context cancellation must be honored.
Insert(ctx context.Context, archive io.ReadWriteSeeker, files []File) error
}