-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm.go
More file actions
67 lines (61 loc) · 1.8 KB
/
Copy pathalgorithm.go
File metadata and controls
67 lines (61 loc) · 1.8 KB
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
package xy3
import (
"strings"
"github.com/nguyengg/xy3/archive"
"github.com/nguyengg/xy3/codec"
)
// DefaultAlgorithmName is the name of the default compression algorithm.
const DefaultAlgorithmName = "zstd"
// NewCompressorFromName returns a compressor with the given algorithm name.
func NewCompressorFromName(algorithmName string) archive.Archiver {
switch algorithmName {
case "gzip", "gz":
return &archive.Tar{Codec: &codec.GzipCodec{}}
case "zip":
return &archive.Zip{}
case "zstd":
return &archive.Tar{Codec: &codec.ZstdCodec{}}
case "xz":
return &archive.Tar{Codec: &codec.XzCodec{}}
default:
return nil
}
}
// NewDecompressorFromName returns a decompressor for extracting from an archive with the given name.
//
// TODO use http.DetectContentType() instead of relying on file extension.
func NewDecompressorFromName(name string) archive.Archiver {
switch {
case strings.HasSuffix(name, ".tar"):
return &archive.Tar{}
case strings.HasSuffix(name, ".tar.gz"):
return &archive.Tar{Codec: &codec.GzipCodec{}}
case strings.HasSuffix(name, ".tar.xz"):
return &archive.Tar{Codec: &codec.XzCodec{}}
case strings.HasSuffix(name, ".tar.zst"):
return &archive.Tar{Codec: &codec.ZstdCodec{}}
case strings.HasSuffix(name, ".7z"):
return &archive.SevenZip{}
case strings.HasSuffix(name, ".rar"):
return &archive.Rar{}
case strings.HasSuffix(name, ".zip"):
return &archive.Zip{}
default:
return nil
}
}
// NewDecoderFromExt returns a decoder for decompressing from files with the given file name extension.
//
// TODO use http.DetectContentType() instead of relying on file extension.
func NewDecoderFromExt(ext string) codec.Codec {
switch ext {
case ".gz":
return &codec.GzipCodec{}
case ".xz":
return &codec.XzCodec{}
case ".zst":
return &codec.ZstdCodec{}
default:
return nil
}
}