This repository has been archived by the owner on Dec 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
partialzip.go
190 lines (154 loc) · 5.03 KB
/
partialzip.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package partialzip
import (
"bufio"
"bytes"
"compress/flate"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
"github.com/pkg/errors"
)
// PartialZip defines a custom partialzip object
type PartialZip struct {
URL string
Size int64
Files []*File
}
// New returns a newly created partialzip object.
func New(url string) (*PartialZip, error) {
pz := &PartialZip{URL: url}
err := pz.init()
if err != nil {
return nil, errors.Wrap(err, "failed to read http response body")
}
return pz, nil
}
func (p *PartialZip) init() error {
var client http.Client
var chuck int64 = 10 * 1024
// get remote zip size
req, _ := http.NewRequest("HEAD", p.URL, nil)
resp, _ := client.Do(req)
p.Size = resp.ContentLength
// pull chuck from end of remote zip
reqRange := fmt.Sprintf("bytes=%d-%d", p.Size-chuck, p.Size)
req, _ = http.NewRequest("GET", p.URL, nil)
req.Header.Add("Range", reqRange)
resp, _ = client.Do(req)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return errors.Wrap(err, "failed to read http response body")
}
defer resp.Body.Close()
r := bytes.NewReader(body)
// parse zip's directory end
end, err := readDirectoryEnd(r, chuck)
if err != nil {
return errors.Wrap(err, "failed to read directory end from remote zip")
}
// z.r = r
p.Files = make([]*File, 0, end.directoryRecords)
// z.Comment = end.comment
rs := io.NewSectionReader(r, 0, chuck)
offset := int64(chuck - (p.Size - int64(end.directoryOffset)))
if _, err = rs.Seek(offset, io.SeekStart); err != nil {
return errors.Wrap(err, "failed to seek to begining of directory")
}
buf := bufio.NewReader(rs)
// The count of files inside a zip is truncated to fit in a uint16.
// Gloss over this by reading headers until we encounter
// a bad one, and then only report an ErrFormat or UnexpectedEOF if
// the file count modulo 65536 is incorrect.
for {
f := &File{zipr: r, zipsize: p.Size}
err = readDirectoryHeader(f, buf)
if err == errFormat || err == io.ErrUnexpectedEOF {
break
}
if err != nil {
return errors.Wrap(err, "failed to read directory header")
}
p.Files = append(p.Files, f)
}
if uint16(len(p.Files)) != uint16(end.directoryRecords) { // only compare 16 bits here
// Return the readDirectoryHeader error if we read
// the wrong number of directory entries.
return errors.Wrap(err, "failed to parse all files listed in directory end")
}
return nil
}
// List lists the files in the remote zip.
// It returns a string array of file paths.
func (p *PartialZip) List() []string {
filePaths := []string{}
for _, file := range p.Files {
filePaths = append(filePaths, file.Name)
}
return filePaths
}
// Get gets a handle to a file from the remote zip.
// It returns an io.ReadCloser and an error, if any.
func (p *PartialZip) Get(path string) (io.ReadCloser, error) {
var client http.Client
var padding uint64 = 1024
for _, file := range p.Files {
// find path in zip directory
if strings.EqualFold(file.Name, path) {
req, _ := http.NewRequest("GET", p.URL, nil)
end := uint64(file.headerOffset) file.CompressedSize64 padding
reqRange := fmt.Sprintf("bytes=%d-%d", file.headerOffset, end)
req.Header.Add("Range", reqRange)
resp, _ := client.Do(req)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "failed to read http response body")
}
dataOffset, err := findBodyOffset(bytes.NewReader(body))
if err != nil {
return nil, errors.Wrap(err, "failed to find data start offset in zip file header")
}
return flate.NewReader(bytes.NewReader(body[dataOffset : uint64(len(body))-padding dataOffset])), nil
}
}
return nil, fmt.Errorf("path %s does not exist in remote zip", path)
}
// Download downloads a file from the remote zip.
// It returns the number of bytes written and an error, if any.
func (p *PartialZip) Download(path string) (int, error) {
var client http.Client
var padding uint64 = 1024
var n int
for _, file := range p.Files {
// find path in zip directory
if strings.EqualFold(file.Name, path) {
req, _ := http.NewRequest("GET", p.URL, nil)
end := uint64(file.headerOffset) file.CompressedSize64 padding
reqRange := fmt.Sprintf("bytes=%d-%d", file.headerOffset, end)
req.Header.Add("Range", reqRange)
resp, _ := client.Do(req)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return n, errors.Wrap(err, "failed to read http response body")
}
dataOffset, err := findBodyOffset(bytes.NewReader(body))
if err != nil {
return n, errors.Wrap(err, "failed to find data start offset in zip file header")
}
enflated, err := ioutil.ReadAll(flate.NewReader(bytes.NewReader(body[dataOffset : uint64(len(body))-padding dataOffset])))
if err != nil {
return n, errors.Wrap(err, "failed to flate decompress data")
}
of, err := os.Create(path)
defer of.Close()
n, err = of.Write(enflated)
if err != nil {
return n, errors.Wrap(err, "failed to write decompressed data to file")
}
return n, nil
}
}
return n, fmt.Errorf("path %s does not exist in remote zip", path)
}