Documentation
¶
Overview ¶
Package sevenzip reads and WRITES the 7z archive format.
Reading goes through github.com/bodgit/sevenzip, which is pure Go and maintained: a second decoder would duplicate LZMA, LZMA2, bzip2, deflate, brotli, lz4, PPMd and the BCJ filters, all of which already work there.
Writing is here because nothing pure-Go writes 7z. The compression half was already available -- github.com/ulikunitz/xz/lzma encodes LZMA2 -- so what this package adds is the container: the signature header, the packed streams, and the structured header that describes them.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrClosed = errors.New("sevenzip: writer is closed")
ErrClosed is returned by a Writer that has already been closed.
var ErrEntryOpen = errors.New("sevenzip: the previous entry is still open")
ErrEntryOpen is returned when a second entry is started before the first is finished. Entries are written one after another because their bytes are written one after another: there is nowhere to put a second stream while the first is still arriving.
var ErrUnknownMethod = errors.New("sevenzip: unknown method")
ErrUnknownMethod is returned for a Method this writer does not have.
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option func(*config)
Option settles how a Writer stores what it is given.
func WithDictionary ¶
WithDictionary asks for an LZMA2 dictionary of at least this many bytes.
At least, because the format names sizes from a fixed sequence rather than carrying a number: the smallest one big enough is used, and it is that size the stream is encoded with. Asking for something the sequence cannot name and being given the next one up is the format's arithmetic, not a rounding this package chose.
func WithMethod ¶
WithMethod chooses how entries are stored. The default is Store.
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer builds a 7z archive.
It needs an io.WriteSeeker, and that is the format's doing rather than a convenience: the archive begins with a header that carries the POSITION, size and CRC of the header at the end, none of which is known until the end. The alternative is holding every byte in memory to count them first, which for the files people put in archives is not an alternative. So the first thirty-two bytes are reserved, the data is streamed straight through, and the beginning is filled in last.
What this writes ¶
Stored entries, one folder each, and an uncompressed header. That is a complete and valid archive -- every reader takes it -- and it is deliberately the smallest thing that can be verified against the reference implementation before compression is added on top. A folder per entry also means an entry can be read without decoding any other, which is what a solid archive gives up.
func NewWriter ¶
func NewWriter(w io.WriteSeeker, opts ...Option) (*Writer, error)
NewWriter reserves the signature header and returns a Writer ready for its first entry.
func (*Writer) AddDir ¶
AddDir records a directory. No stream is written for it, and its mode is kept in the attribute word.
func (*Writer) AddFile ¶
AddFile records a file of size bytes and copies them in.
AddDir and AddFile together are the shape a deferred write layer seals through, so that the thing rewriting an archive at the end does not have to know which format it is rewriting.
The size is accepted and NOT used, and that is the format's doing rather than an oversight: 7z records an entry's unpacked length in a header written at the END, so this writer counts the bytes as they go by and already knows. tar cannot do that -- its length goes in a header BEFORE its data -- so the contract carries the size for tar's sake, and a format that does not need it ignores it.
It is deliberately not checked against what arrives, either. The counted length is the truth this archive will be read by, so trusting the count over the claim is what keeps the archive consistent with itself; a caller whose claim was wrong gets an archive that is right.
func (*Writer) Close ¶
Close writes the header and the signature header, in that order, and the archive is complete when it returns.