This repository has been archived by the owner on Oct 3, 2022. It is now read-only.
/
archive.go
74 lines (64 loc) · 1.48 KB
/
archive.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 imager
import (
"bytes"
"compress/gzip"
"io"
"github.com/ulikunitz/xz"
)
const (
mimeZip = "application/zip"
mime7Zip = "application/x-7z-compressed"
mimeTarGZ = "application/gzip"
mimeTarXZ = "application/x-xz"
)
// Detect if file is a TAR archive compressed with GZIP
func detectTarGZ(buf []byte) (mime string, ext string) {
if !bytes.HasPrefix(buf, []byte("\x1F\x8B\x08")) {
return
}
r, err := gzip.NewReader(bytes.NewReader(buf))
switch {
case err != nil:
case isTar(r):
mime = mimeTarGZ
ext = "tar.gz"
}
return
}
// Read the start of the file and determine, if it is a TAR archive
func isTar(r io.Reader) bool {
head := make([]byte, 262)
read, err := r.Read(head)
if err != nil || read != 262 {
return false
}
return bytes.HasPrefix(head[257:], []byte("ustar"))
}
// Detect if file is a TAR archive compressed with XZ
func detectTarXZ(buf []byte) (mime string, ext string) {
if !bytes.HasPrefix(buf, []byte{0xFD, '7', 'z', 'X', 'Z', 0x00}) {
return "", ""
}
r, err := xz.NewReader(bytes.NewReader(buf))
switch {
case err != nil:
case isTar(r):
mime = mimeTarXZ
ext = "tar.xz"
}
return
}
// Detect if file is a 7zip archive
func detect7z(buf []byte) (string, string) {
if bytes.HasPrefix(buf, []byte{'7', 'z', 0xBC, 0xAF, 0x27, 0x1C}) {
return mime7Zip, "7z"
}
return "", ""
}
// Detect zip archives
func detectZip(data []byte) (string, string) {
if bytes.HasPrefix(data, []byte("\x50\x4B\x03\x04")) {
return mimeZip, "zip"
}
return "", ""
}