-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.go
51 lines (48 loc) · 1.2 KB
/
common.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
package apkutils
import (
"archive/tar"
"bytes"
"compress/gzip"
"fmt"
"path/filepath"
)
// ReadGzipHeader reads the header of a gzip file if found.
// There are 3 signature bytes that occur in a specific order.
func ReadGzipHeader(buf []byte) bool {
if len(buf) < 3 {
return false
}
if buf[0] != GzipID1 || buf[1] != GzipID2 || buf[2] != GzipDeflate {
return false
}
return true
}
// TarGzip create a tar.gz file data from some source bytes.
// Optionally, you can specify not to write an EnfOfTar header.
// This function can be used to create signature.tar.gz files for signed APKINDEX files,
// and APKINDEX.unsigned.tar.gz of an APKINDEX file.
func TarGzip(filename string, b []byte, writeEOFTar bool) (int, []byte, error) {
nRead := len(b)
var buf bytes.Buffer
// io.Copy(&buf, bytes.NewReader(b))
gz := gzip.NewWriter(&buf)
defer gz.Close()
tw := tar.NewWriter(gz)
// Closing tar writer writes the EOF tail.
if writeEOFTar {
defer tw.Close()
}
tw.WriteHeader(&tar.Header{
Name: filepath.Base(filename),
Size: int64(nRead),
Mode: 0600,
})
n, err := tw.Write(b)
fmt.Printf("Wrote %d bytes\n", n)
if err != nil {
return 0, nil, err
}
gz.Close()
ret := buf.Bytes()
return n, ret, nil
}