The types are tar, tar.gz, tar.bz2, tar.xz, tar.zst, zip and 7z, detected from case-insensitive suffixes. --fast, default and --best map to each tool's own levels; "best" is the highest standard level, never a memory-hungry mode (zstd --ultra, xz -e). xz and zstd use all cores.
105 lines
2.8 KiB
Go
105 lines
2.8 KiB
Go
package create
|
|
|
|
import "strings"
|
|
|
|
// Type is an archive type that c can create.
|
|
type Type string
|
|
|
|
const (
|
|
Tar Type = "tar"
|
|
TarGz Type = "tar.gz"
|
|
TarBz2 Type = "tar.bz2"
|
|
TarXz Type = "tar.xz"
|
|
TarZst Type = "tar.zst"
|
|
Zip Type = "zip"
|
|
SevenZ Type = "7z"
|
|
)
|
|
|
|
// Types lists every type, in the order shown to users.
|
|
var Types = []Type{Tar, TarGz, TarBz2, TarXz, TarZst, Zip, SevenZ}
|
|
|
|
func (t Type) isTar() bool { return t == Tar || strings.HasPrefix(string(t), "tar.") }
|
|
|
|
// suffixes maps output file name suffixes to types, most specific first.
|
|
var suffixes = []struct {
|
|
suffix string
|
|
typ Type
|
|
}{
|
|
{".tar.gz", TarGz}, {".tgz", TarGz},
|
|
{".tar.bz2", TarBz2}, {".tbz2", TarBz2}, {".tbz", TarBz2},
|
|
{".tar.xz", TarXz}, {".txz", TarXz},
|
|
{".tar.zst", TarZst}, {".tzst", TarZst},
|
|
{".tar", Tar}, {".zip", Zip}, {".7z", SevenZ},
|
|
}
|
|
|
|
// TypeFromName returns the type implied by the suffix of the file name name
|
|
// (case-insensitive).
|
|
func TypeFromName(name string) (Type, bool) {
|
|
for _, s := range suffixes {
|
|
n := len(name) - len(s.suffix)
|
|
if n > 0 && strings.EqualFold(name[n:], s.suffix) {
|
|
return s.typ, true
|
|
}
|
|
}
|
|
return "", false
|
|
}
|
|
|
|
// ParseType accepts the canonical type names only.
|
|
func ParseType(s string) (Type, bool) {
|
|
for _, t := range Types {
|
|
if string(t) == s {
|
|
return t, true
|
|
}
|
|
}
|
|
return "", false
|
|
}
|
|
|
|
func typeList() string {
|
|
names := make([]string, len(Types))
|
|
for i, t := range Types {
|
|
names[i] = string(t)
|
|
}
|
|
return strings.Join(names, ", ")
|
|
}
|
|
|
|
// Profile trades speed against size.
|
|
type Profile int
|
|
|
|
const (
|
|
Default Profile = iota
|
|
Fast
|
|
Best
|
|
)
|
|
|
|
func (p Profile) String() string { return [...]string{"default", "fast", "best"}[p] }
|
|
|
|
// compression describes how each type compresses. levels is indexed by
|
|
// Profile. "best" is each tool's highest standard level; memory-hungry modes
|
|
// (zstd --ultra, xz -e) are deliberately not used.
|
|
type compression struct {
|
|
program []string // stream compressor for tar types; nil otherwise
|
|
levels [3]string
|
|
}
|
|
|
|
var compressions = map[Type]compression{
|
|
TarGz: {[]string{"gzip", "-c", "-n"}, [3]string{"-6", "-1", "-9"}},
|
|
TarBz2: {[]string{"bzip2", "-c"}, [3]string{"-9", "-1", "-9"}},
|
|
TarXz: {[]string{"xz", "-c", "-T0"}, [3]string{"-6", "-1", "-9"}},
|
|
TarZst: {[]string{"zstd", "-c", "-q", "-T0"}, [3]string{"-3", "-1", "-19"}},
|
|
Zip: {nil, [3]string{"-6", "-1", "-9"}},
|
|
SevenZ: {nil, [3]string{"-mx=5", "-mx=1", "-mx=9"}},
|
|
}
|
|
|
|
// level returns the backend's level option for t and p ("" for plain tar).
|
|
func level(t Type, p Profile) string { return compressions[t].levels[p] }
|
|
|
|
// compressorArgs returns the stream compressor for a compressed tar type, or
|
|
// nil for every other type.
|
|
func compressorArgs(t Type, p Profile) []string {
|
|
c, ok := compressions[t]
|
|
if !ok || c.program == nil {
|
|
return nil
|
|
}
|
|
return append(append([]string(nil), c.program...), c.levels[p])
|
|
}
|