blob: ae379f52eba81f0b23a222b51a4cd0cefdbef927 [file] [log] [blame]
Colin Cross2fe66872015-03-30 17:20:39 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Jeff Gaston11b5c512017-10-12 12:19:14 -070015package zip
Colin Cross2fe66872015-03-30 17:20:39 -070016
17import (
Dan Willemsen017d8932016-08-04 15:43:03 -070018 "bytes"
19 "compress/flate"
Jeff Gastona2976952017-08-22 17:51:25 -070020 "errors"
Colin Cross2fe66872015-03-30 17:20:39 -070021 "fmt"
Dan Willemsen017d8932016-08-04 15:43:03 -070022 "hash/crc32"
Colin Cross2fe66872015-03-30 17:20:39 -070023 "io"
24 "io/ioutil"
25 "os"
26 "path/filepath"
Jeff Gastona2976952017-08-22 17:51:25 -070027 "sort"
Colin Cross2fe66872015-03-30 17:20:39 -070028 "strings"
Dan Willemsen017d8932016-08-04 15:43:03 -070029 "sync"
Colin Cross1d98ee22018-09-18 17:05:15 -070030 "syscall"
Colin Cross2fe66872015-03-30 17:20:39 -070031 "time"
Colin Crossfd708b52021-03-23 14:16:05 -070032
33 "android/soong/response"
Dan Willemsen017d8932016-08-04 15:43:03 -070034
Colin Crossf83c1502017-11-10 13:11:02 -080035 "github.com/google/blueprint/pathtools"
36
Jeff Gastona2976952017-08-22 17:51:25 -070037 "android/soong/jar"
Dan Willemsen017d8932016-08-04 15:43:03 -070038 "android/soong/third_party/zip"
Colin Cross2fe66872015-03-30 17:20:39 -070039)
40
Dan Willemsen017d8932016-08-04 15:43:03 -070041// Block size used during parallel compression of a single file.
42const parallelBlockSize = 1 * 1024 * 1024 // 1MB
43
44// Minimum file size to use parallel compression. It requires more
45// flate.Writer allocations, since we can't change the dictionary
46// during Reset
47const minParallelFileSize = parallelBlockSize * 6
48
49// Size of the ZIP compression window (32KB)
50const windowSize = 32 * 1024
51
52type nopCloser struct {
53 io.Writer
54}
55
56func (nopCloser) Close() error {
57 return nil
58}
59
Jeff Gastoncef50b92017-08-23 15:41:35 -070060type byteReaderCloser struct {
Colin Cross635acc92017-09-12 22:50:46 -070061 *bytes.Reader
Jeff Gastoncef50b92017-08-23 15:41:35 -070062 io.Closer
63}
64
Nan Zhang9067b042017-03-17 14:04:43 -070065type pathMapping struct {
66 dest, src string
Nan Zhangf281bd82017-04-25 16:47:45 -070067 zipMethod uint16
68}
69
Jeff Gastonc3bdc972017-10-12 12:18:19 -070070type FileArg struct {
71 PathPrefixInZip, SourcePrefixToStrip string
72 SourceFiles []string
Colin Crossb7c69112018-09-18 16:51:43 -070073 JunkPaths bool
Jeff Gastonc3bdc972017-10-12 12:18:19 -070074 GlobDir string
75}
76
Colin Crossfe945b42018-09-27 15:00:07 -070077type FileArgsBuilder struct {
78 state FileArg
79 err error
80 fs pathtools.FileSystem
81
82 fileArgs []FileArg
83}
84
85func NewFileArgsBuilder() *FileArgsBuilder {
86 return &FileArgsBuilder{
87 fs: pathtools.OsFs,
88 }
89}
90
91func (b *FileArgsBuilder) JunkPaths(v bool) *FileArgsBuilder {
92 b.state.JunkPaths = v
93 b.state.SourcePrefixToStrip = ""
94 return b
95}
96
97func (b *FileArgsBuilder) SourcePrefixToStrip(prefixToStrip string) *FileArgsBuilder {
98 b.state.JunkPaths = false
99 b.state.SourcePrefixToStrip = prefixToStrip
100 return b
101}
102
103func (b *FileArgsBuilder) PathPrefixInZip(rootPrefix string) *FileArgsBuilder {
104 b.state.PathPrefixInZip = rootPrefix
105 return b
106}
107
108func (b *FileArgsBuilder) File(name string) *FileArgsBuilder {
109 if b.err != nil {
110 return b
111 }
112
113 arg := b.state
114 arg.SourceFiles = []string{name}
115 b.fileArgs = append(b.fileArgs, arg)
116 return b
117}
118
119func (b *FileArgsBuilder) Dir(name string) *FileArgsBuilder {
120 if b.err != nil {
121 return b
122 }
123
124 arg := b.state
125 arg.GlobDir = name
126 b.fileArgs = append(b.fileArgs, arg)
127 return b
128}
129
Sasha Smundak8eedba62020-11-16 19:00:27 -0800130// List reads the file names from the given file and adds them to the source files list.
Colin Crossfe945b42018-09-27 15:00:07 -0700131func (b *FileArgsBuilder) List(name string) *FileArgsBuilder {
132 if b.err != nil {
133 return b
134 }
135
136 f, err := b.fs.Open(name)
137 if err != nil {
138 b.err = err
139 return b
140 }
141 defer f.Close()
142
143 list, err := ioutil.ReadAll(f)
144 if err != nil {
145 b.err = err
146 return b
147 }
148
149 arg := b.state
Jiyong Park04bbf982019-11-04 13:18:41 +0900150 arg.SourceFiles = strings.Fields(string(list))
Colin Crossfe945b42018-09-27 15:00:07 -0700151 b.fileArgs = append(b.fileArgs, arg)
152 return b
153}
154
Sasha Smundak8eedba62020-11-16 19:00:27 -0800155// RspFile reads the file names from given .rsp file and adds them to the source files list.
Colin Cross053fca12020-08-19 13:51:47 -0700156func (b *FileArgsBuilder) RspFile(name string) *FileArgsBuilder {
157 if b.err != nil {
158 return b
159 }
160
161 f, err := b.fs.Open(name)
162 if err != nil {
163 b.err = err
164 return b
165 }
166 defer f.Close()
167
Colin Crossfd708b52021-03-23 14:16:05 -0700168 arg := b.state
169 arg.SourceFiles, err = response.ReadRspFile(f)
Colin Cross053fca12020-08-19 13:51:47 -0700170 if err != nil {
171 b.err = err
172 return b
173 }
Colin Crosscaf4d4c2021-02-03 15:15:14 -0800174 for i := range arg.SourceFiles {
175 arg.SourceFiles[i] = pathtools.MatchEscape(arg.SourceFiles[i])
176 }
Colin Cross053fca12020-08-19 13:51:47 -0700177 b.fileArgs = append(b.fileArgs, arg)
178 return b
179}
180
Colin Crossfe945b42018-09-27 15:00:07 -0700181func (b *FileArgsBuilder) Error() error {
182 if b == nil {
183 return nil
184 }
185 return b.err
186}
187
188func (b *FileArgsBuilder) FileArgs() []FileArg {
189 if b == nil {
190 return nil
191 }
192 return b.fileArgs
193}
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700194
Colin Cross1d98ee22018-09-18 17:05:15 -0700195type IncorrectRelativeRootError struct {
196 RelativeRoot string
197 Path string
198}
199
200func (x IncorrectRelativeRootError) Error() string {
201 return fmt.Sprintf("path %q is outside relative root %q", x.Path, x.RelativeRoot)
202}
203
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700204type ZipWriter struct {
Colin Crosse5580972017-08-30 17:40:21 -0700205 time time.Time
206 createdFiles map[string]string
207 createdDirs map[string]string
208 directories bool
Colin Crosse19c7932015-04-24 15:08:38 -0700209
Dan Willemsen017d8932016-08-04 15:43:03 -0700210 errors chan error
211 writeOps chan chan *zipEntry
212
Jeff Gaston175f34c2017-08-17 21:43:21 -0700213 cpuRateLimiter *CPURateLimiter
214 memoryRateLimiter *MemoryRateLimiter
Dan Willemsen017d8932016-08-04 15:43:03 -0700215
216 compressorPool sync.Pool
217 compLevel int
Colin Cross05518bc2018-09-27 15:06:19 -0700218
Colin Cross4be8f9e2018-09-28 15:16:48 -0700219 followSymlinks pathtools.ShouldFollowSymlinks
220 ignoreMissingFiles bool
Colin Cross09f11052018-09-21 15:12:39 -0700221
Colin Cross4be8f9e2018-09-28 15:16:48 -0700222 stderr io.Writer
223 fs pathtools.FileSystem
Dan Willemsen017d8932016-08-04 15:43:03 -0700224}
225
226type zipEntry struct {
227 fh *zip.FileHeader
228
229 // List of delayed io.Reader
230 futureReaders chan chan io.Reader
Jeff Gaston175f34c2017-08-17 21:43:21 -0700231
232 // Only used for passing into the MemoryRateLimiter to ensure we
233 // release as much memory as much as we request
234 allocatedSize int64
Colin Cross2fe66872015-03-30 17:20:39 -0700235}
236
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700237type ZipArgs struct {
Colin Crossfe945b42018-09-27 15:00:07 -0700238 FileArgs []FileArg
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700239 OutputFilePath string
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700240 EmulateJar bool
Colin Cross9cb51db2019-06-17 14:12:41 -0700241 SrcJar bool
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700242 AddDirectoryEntriesToZip bool
243 CompressionLevel int
244 ManifestSourcePath string
245 NumParallelJobs int
246 NonDeflatedFiles map[string]bool
Colin Crossf83c1502017-11-10 13:11:02 -0800247 WriteIfChanged bool
Colin Cross09f11052018-09-21 15:12:39 -0700248 StoreSymlinks bool
Colin Cross4be8f9e2018-09-28 15:16:48 -0700249 IgnoreMissingFiles bool
Colin Cross09f11052018-09-21 15:12:39 -0700250
Colin Cross4be8f9e2018-09-28 15:16:48 -0700251 Stderr io.Writer
Colin Cross09f11052018-09-21 15:12:39 -0700252 Filesystem pathtools.FileSystem
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700253}
Colin Cross2fe66872015-03-30 17:20:39 -0700254
Sasha Smundak8eedba62020-11-16 19:00:27 -0800255func zipTo(args ZipArgs, w io.Writer) error {
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700256 if args.EmulateJar {
257 args.AddDirectoryEntriesToZip = true
Jeff Gaston8edbb3a2017-08-22 20:05:28 -0700258 }
259
Colin Cross09f11052018-09-21 15:12:39 -0700260 // Have Glob follow symlinks if they are not being stored as symlinks in the zip file.
261 followSymlinks := pathtools.ShouldFollowSymlinks(!args.StoreSymlinks)
262
Colin Cross05518bc2018-09-27 15:06:19 -0700263 z := &ZipWriter{
Colin Cross4be8f9e2018-09-28 15:16:48 -0700264 time: jar.DefaultTime,
265 createdDirs: make(map[string]string),
266 createdFiles: make(map[string]string),
267 directories: args.AddDirectoryEntriesToZip,
268 compLevel: args.CompressionLevel,
269 followSymlinks: followSymlinks,
270 ignoreMissingFiles: args.IgnoreMissingFiles,
271 stderr: args.Stderr,
272 fs: args.Filesystem,
Colin Cross2fe66872015-03-30 17:20:39 -0700273 }
Colin Cross05518bc2018-09-27 15:06:19 -0700274
275 if z.fs == nil {
276 z.fs = pathtools.OsFs
277 }
278
Colin Cross4be8f9e2018-09-28 15:16:48 -0700279 if z.stderr == nil {
280 z.stderr = os.Stderr
281 }
282
Nan Zhang9067b042017-03-17 14:04:43 -0700283 pathMappings := []pathMapping{}
Nan Zhang9067b042017-03-17 14:04:43 -0700284
Colin Crossd3216292018-09-14 15:06:31 -0700285 noCompression := args.CompressionLevel == 0
286
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700287 for _, fa := range args.FileArgs {
Colin Cross1d98ee22018-09-18 17:05:15 -0700288 var srcs []string
289 for _, s := range fa.SourceFiles {
290 s = strings.TrimSpace(s)
291 if s == "" {
292 continue
293 }
294
Colin Cross82ea3fb2021-04-05 17:48:26 -0700295 result, err := z.fs.Glob(s, nil, followSymlinks)
Colin Cross1d98ee22018-09-18 17:05:15 -0700296 if err != nil {
297 return err
298 }
Colin Cross82ea3fb2021-04-05 17:48:26 -0700299 if len(result.Matches) == 0 {
Colin Cross4be8f9e2018-09-28 15:16:48 -0700300 err := &os.PathError{
301 Op: "lstat",
Colin Cross1d98ee22018-09-18 17:05:15 -0700302 Path: s,
303 Err: os.ErrNotExist,
304 }
Colin Cross4be8f9e2018-09-28 15:16:48 -0700305 if args.IgnoreMissingFiles {
Dan Willemsenedc934c2018-12-27 12:41:25 -0800306 fmt.Fprintln(z.stderr, "warning:", err)
Colin Cross4be8f9e2018-09-28 15:16:48 -0700307 } else {
308 return err
309 }
Colin Cross1d98ee22018-09-18 17:05:15 -0700310 }
Colin Cross82ea3fb2021-04-05 17:48:26 -0700311 srcs = append(srcs, result.Matches...)
Colin Cross1d98ee22018-09-18 17:05:15 -0700312 }
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700313 if fa.GlobDir != "" {
Colin Cross1d98ee22018-09-18 17:05:15 -0700314 if exists, isDir, err := z.fs.Exists(fa.GlobDir); err != nil {
315 return err
Colin Cross4be8f9e2018-09-28 15:16:48 -0700316 } else if !exists && !args.IgnoreMissingFiles {
317 err := &os.PathError{
318 Op: "lstat",
Colin Cross1d98ee22018-09-18 17:05:15 -0700319 Path: fa.GlobDir,
320 Err: os.ErrNotExist,
321 }
Colin Cross4be8f9e2018-09-28 15:16:48 -0700322 if args.IgnoreMissingFiles {
Dan Willemsenedc934c2018-12-27 12:41:25 -0800323 fmt.Fprintln(z.stderr, "warning:", err)
Colin Cross4be8f9e2018-09-28 15:16:48 -0700324 } else {
325 return err
326 }
327 } else if !isDir && !args.IgnoreMissingFiles {
328 err := &os.PathError{
329 Op: "lstat",
Colin Cross1d98ee22018-09-18 17:05:15 -0700330 Path: fa.GlobDir,
331 Err: syscall.ENOTDIR,
332 }
Colin Cross4be8f9e2018-09-28 15:16:48 -0700333 if args.IgnoreMissingFiles {
Dan Willemsenedc934c2018-12-27 12:41:25 -0800334 fmt.Fprintln(z.stderr, "warning:", err)
Colin Cross4be8f9e2018-09-28 15:16:48 -0700335 } else {
336 return err
337 }
Colin Cross1d98ee22018-09-18 17:05:15 -0700338 }
Colin Cross82ea3fb2021-04-05 17:48:26 -0700339 result, err := z.fs.Glob(filepath.Join(fa.GlobDir, "**/*"), nil, followSymlinks)
Colin Cross1d98ee22018-09-18 17:05:15 -0700340 if err != nil {
341 return err
342 }
Colin Cross82ea3fb2021-04-05 17:48:26 -0700343 srcs = append(srcs, result.Matches...)
Colin Cross7b10cf12017-08-30 14:12:21 -0700344 }
345 for _, src := range srcs {
Colin Crossb7c69112018-09-18 16:51:43 -0700346 err := fillPathPairs(fa, src, &pathMappings, args.NonDeflatedFiles, noCompression)
Colin Crossd3216292018-09-14 15:06:31 -0700347 if err != nil {
Colin Cross05518bc2018-09-27 15:06:19 -0700348 return err
Nan Zhang9067b042017-03-17 14:04:43 -0700349 }
350 }
351 }
352
Colin Cross9cb51db2019-06-17 14:12:41 -0700353 return z.write(w, pathMappings, args.ManifestSourcePath, args.EmulateJar, args.SrcJar, args.NumParallelJobs)
Colin Cross05518bc2018-09-27 15:06:19 -0700354}
355
Sasha Smundak8eedba62020-11-16 19:00:27 -0800356// Zip creates an output zip archive from given sources.
Colin Cross05518bc2018-09-27 15:06:19 -0700357func Zip(args ZipArgs) error {
358 if args.OutputFilePath == "" {
359 return fmt.Errorf("output file path must be nonempty")
360 }
361
Colin Crossf83c1502017-11-10 13:11:02 -0800362 buf := &bytes.Buffer{}
363 var out io.Writer = buf
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700364
Colin Cross665b6782021-03-15 15:28:40 -0700365 var zipErr error
366
Colin Crossf83c1502017-11-10 13:11:02 -0800367 if !args.WriteIfChanged {
368 f, err := os.Create(args.OutputFilePath)
369 if err != nil {
370 return err
371 }
372
373 defer f.Close()
374 defer func() {
Colin Cross665b6782021-03-15 15:28:40 -0700375 if zipErr != nil {
Colin Crossf83c1502017-11-10 13:11:02 -0800376 os.Remove(args.OutputFilePath)
377 }
378 }()
379
380 out = f
381 }
382
Colin Cross665b6782021-03-15 15:28:40 -0700383 zipErr = zipTo(args, out)
384 if zipErr != nil {
385 return zipErr
Colin Crossf83c1502017-11-10 13:11:02 -0800386 }
387
388 if args.WriteIfChanged {
389 err := pathtools.WriteFileIfChanged(args.OutputFilePath, buf.Bytes(), 0666)
390 if err != nil {
391 return err
392 }
393 }
394
395 return nil
Colin Cross2fe66872015-03-30 17:20:39 -0700396}
397
Colin Crossb7c69112018-09-18 16:51:43 -0700398func fillPathPairs(fa FileArg, src string, pathMappings *[]pathMapping,
Colin Crossd3216292018-09-14 15:06:31 -0700399 nonDeflatedFiles map[string]bool, noCompression bool) error {
400
Colin Crossb7c69112018-09-18 16:51:43 -0700401 var dest string
402
403 if fa.JunkPaths {
404 dest = filepath.Base(src)
405 } else {
406 var err error
407 dest, err = filepath.Rel(fa.SourcePrefixToStrip, src)
408 if err != nil {
409 return err
410 }
Colin Cross1d98ee22018-09-18 17:05:15 -0700411 if strings.HasPrefix(dest, "../") {
412 return IncorrectRelativeRootError{
413 Path: src,
414 RelativeRoot: fa.SourcePrefixToStrip,
415 }
416 }
Nan Zhang9067b042017-03-17 14:04:43 -0700417 }
Colin Crossb7c69112018-09-18 16:51:43 -0700418 dest = filepath.Join(fa.PathPrefixInZip, dest)
Nan Zhang9067b042017-03-17 14:04:43 -0700419
Nan Zhangf281bd82017-04-25 16:47:45 -0700420 zipMethod := zip.Deflate
Colin Crossd3216292018-09-14 15:06:31 -0700421 if _, found := nonDeflatedFiles[dest]; found || noCompression {
Nan Zhangf281bd82017-04-25 16:47:45 -0700422 zipMethod = zip.Store
423 }
424 *pathMappings = append(*pathMappings,
425 pathMapping{dest: dest, src: src, zipMethod: zipMethod})
Nan Zhang9067b042017-03-17 14:04:43 -0700426
427 return nil
428}
429
Jeff Gastona2976952017-08-22 17:51:25 -0700430func jarSort(mappings []pathMapping) {
Sasha Smundak8eedba62020-11-16 19:00:27 -0800431 sort.SliceStable(mappings, func(i int, j int) bool {
Jeff Gastona2976952017-08-22 17:51:25 -0700432 return jar.EntryNamesLess(mappings[i].dest, mappings[j].dest)
Sasha Smundak8eedba62020-11-16 19:00:27 -0800433 })
Jeff Gastona2976952017-08-22 17:51:25 -0700434}
435
Colin Cross9cb51db2019-06-17 14:12:41 -0700436func (z *ZipWriter) write(f io.Writer, pathMappings []pathMapping, manifest string, emulateJar, srcJar bool,
437 parallelJobs int) error {
438
Dan Willemsen017d8932016-08-04 15:43:03 -0700439 z.errors = make(chan error)
440 defer close(z.errors)
Colin Cross2fe66872015-03-30 17:20:39 -0700441
Dan Willemsen017d8932016-08-04 15:43:03 -0700442 // This channel size can be essentially unlimited -- it's used as a fifo
443 // queue decouple the CPU and IO loads. Directories don't require any
444 // compression time, but still cost some IO. Similar with small files that
445 // can be very fast to compress. Some files that are more difficult to
446 // compress won't take a corresponding longer time writing out.
447 //
448 // The optimum size here depends on your CPU and IO characteristics, and
449 // the the layout of your zip file. 1000 was chosen mostly at random as
450 // something that worked reasonably well for a test file.
451 //
452 // The RateLimit object will put the upper bounds on the number of
453 // parallel compressions and outstanding buffers.
454 z.writeOps = make(chan chan *zipEntry, 1000)
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700455 z.cpuRateLimiter = NewCPURateLimiter(int64(parallelJobs))
Jeff Gaston175f34c2017-08-17 21:43:21 -0700456 z.memoryRateLimiter = NewMemoryRateLimiter(0)
457 defer func() {
458 z.cpuRateLimiter.Stop()
459 z.memoryRateLimiter.Stop()
460 }()
Jeff Gastona2976952017-08-22 17:51:25 -0700461
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700462 if manifest != "" && !emulateJar {
Colin Cross635acc92017-09-12 22:50:46 -0700463 return errors.New("must specify --jar when specifying a manifest via -m")
Jeff Gastona2976952017-08-22 17:51:25 -0700464 }
465
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700466 if emulateJar {
Colin Cross635acc92017-09-12 22:50:46 -0700467 // manifest may be empty, in which case addManifest will fill in a default
468 pathMappings = append(pathMappings, pathMapping{jar.ManifestFile, manifest, zip.Deflate})
469
Jeff Gastona2976952017-08-22 17:51:25 -0700470 jarSort(pathMappings)
471 }
472
Dan Willemsen017d8932016-08-04 15:43:03 -0700473 go func() {
474 var err error
475 defer close(z.writeOps)
476
Nan Zhang9067b042017-03-17 14:04:43 -0700477 for _, ele := range pathMappings {
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700478 if emulateJar && ele.dest == jar.ManifestFile {
Jeff Gastoncef50b92017-08-23 15:41:35 -0700479 err = z.addManifest(ele.dest, ele.src, ele.zipMethod)
480 } else {
Colin Cross9cb51db2019-06-17 14:12:41 -0700481 err = z.addFile(ele.dest, ele.src, ele.zipMethod, emulateJar, srcJar)
Jeff Gastoncef50b92017-08-23 15:41:35 -0700482 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700483 if err != nil {
484 z.errors <- err
485 return
486 }
487 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700488 }()
489
490 zipw := zip.NewWriter(f)
491
492 var currentWriteOpChan chan *zipEntry
493 var currentWriter io.WriteCloser
494 var currentReaders chan chan io.Reader
495 var currentReader chan io.Reader
496 var done bool
497
498 for !done {
499 var writeOpsChan chan chan *zipEntry
500 var writeOpChan chan *zipEntry
501 var readersChan chan chan io.Reader
502
503 if currentReader != nil {
504 // Only read and process errors
505 } else if currentReaders != nil {
506 readersChan = currentReaders
507 } else if currentWriteOpChan != nil {
508 writeOpChan = currentWriteOpChan
509 } else {
510 writeOpsChan = z.writeOps
511 }
512
513 select {
514 case writeOp, ok := <-writeOpsChan:
515 if !ok {
516 done = true
517 }
518
519 currentWriteOpChan = writeOp
520
521 case op := <-writeOpChan:
522 currentWriteOpChan = nil
523
Colin Crossf83c1502017-11-10 13:11:02 -0800524 var err error
Dan Willemsen017d8932016-08-04 15:43:03 -0700525 if op.fh.Method == zip.Deflate {
526 currentWriter, err = zipw.CreateCompressedHeader(op.fh)
527 } else {
528 var zw io.Writer
Jeff Gastonc5eb66d2017-08-24 14:11:27 -0700529
530 op.fh.CompressedSize64 = op.fh.UncompressedSize64
531
532 zw, err = zipw.CreateHeaderAndroid(op.fh)
Dan Willemsen017d8932016-08-04 15:43:03 -0700533 currentWriter = nopCloser{zw}
534 }
535 if err != nil {
536 return err
537 }
538
539 currentReaders = op.futureReaders
540 if op.futureReaders == nil {
541 currentWriter.Close()
542 currentWriter = nil
543 }
Jeff Gaston175f34c2017-08-17 21:43:21 -0700544 z.memoryRateLimiter.Finish(op.allocatedSize)
Dan Willemsen017d8932016-08-04 15:43:03 -0700545
546 case futureReader, ok := <-readersChan:
547 if !ok {
548 // Done with reading
549 currentWriter.Close()
550 currentWriter = nil
551 currentReaders = nil
552 }
553
554 currentReader = futureReader
555
556 case reader := <-currentReader:
Colin Crossf83c1502017-11-10 13:11:02 -0800557 _, err := io.Copy(currentWriter, reader)
Dan Willemsen017d8932016-08-04 15:43:03 -0700558 if err != nil {
559 return err
560 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700561
562 currentReader = nil
563
Colin Crossf83c1502017-11-10 13:11:02 -0800564 case err := <-z.errors:
Colin Cross2fe66872015-03-30 17:20:39 -0700565 return err
566 }
567 }
568
Dan Willemsen017d8932016-08-04 15:43:03 -0700569 // One last chance to catch an error
570 select {
Colin Crossf83c1502017-11-10 13:11:02 -0800571 case err := <-z.errors:
Dan Willemsen017d8932016-08-04 15:43:03 -0700572 return err
573 default:
574 zipw.Close()
575 return nil
Colin Cross2fe66872015-03-30 17:20:39 -0700576 }
Colin Cross2fe66872015-03-30 17:20:39 -0700577}
578
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700579// imports (possibly with compression) <src> into the zip at sub-path <dest>
Colin Cross9cb51db2019-06-17 14:12:41 -0700580func (z *ZipWriter) addFile(dest, src string, method uint16, emulateJar, srcJar bool) error {
Dan Willemsen017d8932016-08-04 15:43:03 -0700581 var fileSize int64
Dan Willemsen10462b32017-03-15 19:02:51 -0700582 var executable bool
Dan Willemsen017d8932016-08-04 15:43:03 -0700583
Colin Cross09f11052018-09-21 15:12:39 -0700584 var s os.FileInfo
585 var err error
586 if z.followSymlinks {
587 s, err = z.fs.Stat(src)
588 } else {
589 s, err = z.fs.Lstat(src)
590 }
591
592 if err != nil {
Colin Cross4be8f9e2018-09-28 15:16:48 -0700593 if os.IsNotExist(err) && z.ignoreMissingFiles {
594 fmt.Fprintln(z.stderr, "warning:", err)
595 return nil
596 }
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700597 return err
Colin Cross9cb51db2019-06-17 14:12:41 -0700598 }
599
600 createParentDirs := func(dest, src string) error {
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700601 if err := z.writeDirectory(filepath.Dir(dest), src, emulateJar); err != nil {
Colin Crosse5580972017-08-30 17:40:21 -0700602 return err
603 }
604
605 if prev, exists := z.createdDirs[dest]; exists {
606 return fmt.Errorf("destination %q is both a directory %q and a file %q", dest, prev, src)
607 }
608 if prev, exists := z.createdFiles[dest]; exists {
609 return fmt.Errorf("destination %q has two files %q and %q", dest, prev, src)
610 }
611
612 z.createdFiles[dest] = src
613
Colin Cross9cb51db2019-06-17 14:12:41 -0700614 return nil
615 }
616
617 if s.IsDir() {
618 if z.directories {
619 return z.writeDirectory(dest, src, emulateJar)
620 }
621 return nil
622 } else if s.Mode()&os.ModeSymlink != 0 {
623 err = createParentDirs(dest, src)
624 if err != nil {
625 return err
626 }
627
628 return z.writeSymlink(dest, src)
629 } else if s.Mode().IsRegular() {
630 r, err := z.fs.Open(src)
631 if err != nil {
632 return err
633 }
634
635 if srcJar && filepath.Ext(src) == ".java" {
636 // rewrite the destination using the package path if it can be determined
637 pkg, err := jar.JavaPackage(r, src)
638 if err != nil {
639 // ignore errors for now, leaving the file at in its original location in the zip
640 } else {
641 dest = filepath.Join(filepath.Join(strings.Split(pkg, ".")...), filepath.Base(src))
642 }
643
644 _, err = r.Seek(0, io.SeekStart)
645 if err != nil {
646 return err
647 }
Colin Crosse5580972017-08-30 17:40:21 -0700648 }
649
Dan Willemsen017d8932016-08-04 15:43:03 -0700650 fileSize = s.Size()
Dan Willemsen10462b32017-03-15 19:02:51 -0700651 executable = s.Mode()&0100 != 0
Colin Cross957cc4e2015-04-24 15:10:32 -0700652
Colin Cross9cb51db2019-06-17 14:12:41 -0700653 header := &zip.FileHeader{
654 Name: dest,
655 Method: method,
656 UncompressedSize64: uint64(fileSize),
657 }
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700658
Chris Grossfa5b4e92021-06-02 12:56:08 -0700659 mode := os.FileMode(0644)
Colin Cross9cb51db2019-06-17 14:12:41 -0700660 if executable {
Chris Grossfa5b4e92021-06-02 12:56:08 -0700661 mode = 0755
Colin Cross9cb51db2019-06-17 14:12:41 -0700662 }
Colin Crosse070c3a2021-05-11 16:03:57 -0700663 header.SetMode(mode)
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700664
Colin Cross9cb51db2019-06-17 14:12:41 -0700665 err = createParentDirs(dest, src)
666 if err != nil {
667 return err
668 }
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700669
Colin Cross9cb51db2019-06-17 14:12:41 -0700670 return z.writeFileContents(header, r)
671 } else {
672 return fmt.Errorf("%s is not a file, directory, or symlink", src)
673 }
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700674}
675
Sasha Smundak8eedba62020-11-16 19:00:27 -0800676func (z *ZipWriter) addManifest(dest string, src string, _ uint16) error {
Colin Crosse5580972017-08-30 17:40:21 -0700677 if prev, exists := z.createdDirs[dest]; exists {
678 return fmt.Errorf("destination %q is both a directory %q and a file %q", dest, prev, src)
679 }
680 if prev, exists := z.createdFiles[dest]; exists {
681 return fmt.Errorf("destination %q has two files %q and %q", dest, prev, src)
682 }
683
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700684 if err := z.writeDirectory(filepath.Dir(dest), src, true); err != nil {
Colin Cross635acc92017-09-12 22:50:46 -0700685 return err
Jeff Gastoncef50b92017-08-23 15:41:35 -0700686 }
687
Colin Cross05518bc2018-09-27 15:06:19 -0700688 var contents []byte
689 if src != "" {
690 f, err := z.fs.Open(src)
691 if err != nil {
692 return err
693 }
694
695 contents, err = ioutil.ReadAll(f)
696 f.Close()
697 if err != nil {
698 return err
699 }
700 }
701
702 fh, buf, err := jar.ManifestFileContents(contents)
Colin Cross635acc92017-09-12 22:50:46 -0700703 if err != nil {
704 return err
Jeff Gastoncef50b92017-08-23 15:41:35 -0700705 }
706
Colin Cross635acc92017-09-12 22:50:46 -0700707 reader := &byteReaderCloser{bytes.NewReader(buf), ioutil.NopCloser(nil)}
708
709 return z.writeFileContents(fh, reader)
Jeff Gastoncef50b92017-08-23 15:41:35 -0700710}
711
Colin Cross05518bc2018-09-27 15:06:19 -0700712func (z *ZipWriter) writeFileContents(header *zip.FileHeader, r pathtools.ReaderAtSeekerCloser) (err error) {
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700713
714 header.SetModTime(z.time)
715
Dan Willemsen017d8932016-08-04 15:43:03 -0700716 compressChan := make(chan *zipEntry, 1)
717 z.writeOps <- compressChan
718
719 // Pre-fill a zipEntry, it will be sent in the compressChan once
720 // we're sure about the Method and CRC.
721 ze := &zipEntry{
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700722 fh: header,
Dan Willemsen10462b32017-03-15 19:02:51 -0700723 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700724
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700725 ze.allocatedSize = int64(header.UncompressedSize64)
Jeff Gaston175f34c2017-08-17 21:43:21 -0700726 z.cpuRateLimiter.Request()
727 z.memoryRateLimiter.Request(ze.allocatedSize)
Dan Willemsen017d8932016-08-04 15:43:03 -0700728
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700729 fileSize := int64(header.UncompressedSize64)
730 if fileSize == 0 {
731 fileSize = int64(header.UncompressedSize)
732 }
733
734 if header.Method == zip.Deflate && fileSize >= minParallelFileSize {
Dan Willemsen017d8932016-08-04 15:43:03 -0700735 wg := new(sync.WaitGroup)
736
737 // Allocate enough buffer to hold all readers. We'll limit
738 // this based on actual buffer sizes in RateLimit.
739 ze.futureReaders = make(chan chan io.Reader, (fileSize/parallelBlockSize)+1)
740
741 // Calculate the CRC in the background, since reading the entire
742 // file could take a while.
743 //
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700744 // We could split this up into chunks as well, but it's faster
Dan Willemsen017d8932016-08-04 15:43:03 -0700745 // than the compression. Due to the Go Zip API, we also need to
746 // know the result before we can begin writing the compressed
747 // data out to the zipfile.
748 wg.Add(1)
Jeff Gaston175f34c2017-08-17 21:43:21 -0700749 go z.crcFile(r, ze, compressChan, wg)
Dan Willemsen017d8932016-08-04 15:43:03 -0700750
751 for start := int64(0); start < fileSize; start += parallelBlockSize {
752 sr := io.NewSectionReader(r, start, parallelBlockSize)
753 resultChan := make(chan io.Reader, 1)
754 ze.futureReaders <- resultChan
755
Jeff Gaston175f34c2017-08-17 21:43:21 -0700756 z.cpuRateLimiter.Request()
Dan Willemsen017d8932016-08-04 15:43:03 -0700757
758 last := !(start+parallelBlockSize < fileSize)
759 var dict []byte
760 if start >= windowSize {
761 dict, err = ioutil.ReadAll(io.NewSectionReader(r, start-windowSize, windowSize))
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700762 if err != nil {
763 return err
764 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700765 }
766
767 wg.Add(1)
Jeff Gaston175f34c2017-08-17 21:43:21 -0700768 go z.compressPartialFile(sr, dict, last, resultChan, wg)
Dan Willemsen017d8932016-08-04 15:43:03 -0700769 }
770
771 close(ze.futureReaders)
772
773 // Close the file handle after all readers are done
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700774 go func(wg *sync.WaitGroup, closer io.Closer) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700775 wg.Wait()
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700776 closer.Close()
Dan Willemsen017d8932016-08-04 15:43:03 -0700777 }(wg, r)
778 } else {
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700779 go func() {
780 z.compressWholeFile(ze, r, compressChan)
781 r.Close()
782 }()
Dan Willemsen017d8932016-08-04 15:43:03 -0700783 }
784
785 return nil
786}
787
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700788func (z *ZipWriter) crcFile(r io.Reader, ze *zipEntry, resultChan chan *zipEntry, wg *sync.WaitGroup) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700789 defer wg.Done()
Jeff Gaston175f34c2017-08-17 21:43:21 -0700790 defer z.cpuRateLimiter.Finish()
Dan Willemsen017d8932016-08-04 15:43:03 -0700791
792 crc := crc32.NewIEEE()
793 _, err := io.Copy(crc, r)
794 if err != nil {
795 z.errors <- err
796 return
797 }
798
799 ze.fh.CRC32 = crc.Sum32()
800 resultChan <- ze
801 close(resultChan)
802}
803
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700804func (z *ZipWriter) compressPartialFile(r io.Reader, dict []byte, last bool, resultChan chan io.Reader, wg *sync.WaitGroup) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700805 defer wg.Done()
806
807 result, err := z.compressBlock(r, dict, last)
808 if err != nil {
809 z.errors <- err
810 return
811 }
812
Jeff Gaston175f34c2017-08-17 21:43:21 -0700813 z.cpuRateLimiter.Finish()
814
Dan Willemsen017d8932016-08-04 15:43:03 -0700815 resultChan <- result
816}
817
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700818func (z *ZipWriter) compressBlock(r io.Reader, dict []byte, last bool) (*bytes.Buffer, error) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700819 buf := new(bytes.Buffer)
820 var fw *flate.Writer
821 var err error
822 if len(dict) > 0 {
823 // There's no way to Reset a Writer with a new dictionary, so
824 // don't use the Pool
825 fw, err = flate.NewWriterDict(buf, z.compLevel, dict)
826 } else {
827 var ok bool
828 if fw, ok = z.compressorPool.Get().(*flate.Writer); ok {
829 fw.Reset(buf)
830 } else {
831 fw, err = flate.NewWriter(buf, z.compLevel)
832 }
833 defer z.compressorPool.Put(fw)
834 }
835 if err != nil {
836 return nil, err
837 }
838
839 _, err = io.Copy(fw, r)
840 if err != nil {
841 return nil, err
842 }
843 if last {
844 fw.Close()
845 } else {
846 fw.Flush()
847 }
848
849 return buf, nil
850}
851
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700852func (z *ZipWriter) compressWholeFile(ze *zipEntry, r io.ReadSeeker, compressChan chan *zipEntry) {
Dan Willemsen017d8932016-08-04 15:43:03 -0700853
Dan Willemsen017d8932016-08-04 15:43:03 -0700854 crc := crc32.NewIEEE()
Dan Willemsena8b55022017-03-15 21:49:26 -0700855 _, err := io.Copy(crc, r)
Colin Cross2fe66872015-03-30 17:20:39 -0700856 if err != nil {
Dan Willemsen017d8932016-08-04 15:43:03 -0700857 z.errors <- err
858 return
Colin Cross2fe66872015-03-30 17:20:39 -0700859 }
860
Dan Willemsena8b55022017-03-15 21:49:26 -0700861 ze.fh.CRC32 = crc.Sum32()
Colin Cross2fe66872015-03-30 17:20:39 -0700862
Dan Willemsen017d8932016-08-04 15:43:03 -0700863 _, err = r.Seek(0, 0)
Colin Cross2fe66872015-03-30 17:20:39 -0700864 if err != nil {
Dan Willemsen017d8932016-08-04 15:43:03 -0700865 z.errors <- err
866 return
Colin Cross2fe66872015-03-30 17:20:39 -0700867 }
868
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700869 readFile := func(reader io.ReadSeeker) ([]byte, error) {
870 _, err := reader.Seek(0, 0)
Nan Zhangf281bd82017-04-25 16:47:45 -0700871 if err != nil {
872 return nil, err
873 }
874
Jeff Gaston66dd6e52017-08-23 15:12:48 -0700875 buf, err := ioutil.ReadAll(reader)
Nan Zhangf281bd82017-04-25 16:47:45 -0700876 if err != nil {
877 return nil, err
878 }
879
880 return buf, nil
881 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700882
Dan Willemsena8b55022017-03-15 21:49:26 -0700883 ze.futureReaders = make(chan chan io.Reader, 1)
Dan Willemsen017d8932016-08-04 15:43:03 -0700884 futureReader := make(chan io.Reader, 1)
885 ze.futureReaders <- futureReader
886 close(ze.futureReaders)
887
Nan Zhangf281bd82017-04-25 16:47:45 -0700888 if ze.fh.Method == zip.Deflate {
889 compressed, err := z.compressBlock(r, nil, true)
890 if err != nil {
891 z.errors <- err
892 return
893 }
894 if uint64(compressed.Len()) < ze.fh.UncompressedSize64 {
895 futureReader <- compressed
Nan Zhangf281bd82017-04-25 16:47:45 -0700896 } else {
897 buf, err := readFile(r)
898 if err != nil {
899 z.errors <- err
900 return
901 }
902 ze.fh.Method = zip.Store
903 futureReader <- bytes.NewReader(buf)
Nan Zhangf281bd82017-04-25 16:47:45 -0700904 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700905 } else {
Nan Zhangf281bd82017-04-25 16:47:45 -0700906 buf, err := readFile(r)
Dan Willemsen017d8932016-08-04 15:43:03 -0700907 if err != nil {
908 z.errors <- err
909 return
910 }
Dan Willemsen017d8932016-08-04 15:43:03 -0700911 ze.fh.Method = zip.Store
912 futureReader <- bytes.NewReader(buf)
Dan Willemsen017d8932016-08-04 15:43:03 -0700913 }
Nan Zhangf281bd82017-04-25 16:47:45 -0700914
Jeff Gaston175f34c2017-08-17 21:43:21 -0700915 z.cpuRateLimiter.Finish()
916
Dan Willemsen017d8932016-08-04 15:43:03 -0700917 close(futureReader)
918
919 compressChan <- ze
920 close(compressChan)
Colin Cross2fe66872015-03-30 17:20:39 -0700921}
Colin Crosse19c7932015-04-24 15:08:38 -0700922
Colin Crosse5580972017-08-30 17:40:21 -0700923// writeDirectory annotates that dir is a directory created for the src file or directory, and adds
924// the directory entry to the zip file if directories are enabled.
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700925func (z *ZipWriter) writeDirectory(dir string, src string, emulateJar bool) error {
Jeff Gaston2d174132017-08-15 18:05:56 -0700926 // clean the input
Colin Crosse5580972017-08-30 17:40:21 -0700927 dir = filepath.Clean(dir)
Jeff Gaston2d174132017-08-15 18:05:56 -0700928
929 // discover any uncreated directories in the path
Sasha Smundak8eedba62020-11-16 19:00:27 -0800930 var zipDirs []string
Colin Crosse5580972017-08-30 17:40:21 -0700931 for dir != "" && dir != "." {
932 if _, exists := z.createdDirs[dir]; exists {
933 break
934 }
Jeff Gaston2d174132017-08-15 18:05:56 -0700935
Colin Crosse5580972017-08-30 17:40:21 -0700936 if prev, exists := z.createdFiles[dir]; exists {
937 return fmt.Errorf("destination %q is both a directory %q and a file %q", dir, src, prev)
938 }
939
940 z.createdDirs[dir] = src
Jeff Gaston2d174132017-08-15 18:05:56 -0700941 // parent directories precede their children
Colin Crosse5580972017-08-30 17:40:21 -0700942 zipDirs = append([]string{dir}, zipDirs...)
Jeff Gaston2d174132017-08-15 18:05:56 -0700943
Colin Crosse5580972017-08-30 17:40:21 -0700944 dir = filepath.Dir(dir)
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700945 }
946
Colin Crosse5580972017-08-30 17:40:21 -0700947 if z.directories {
948 // make a directory entry for each uncreated directory
949 for _, cleanDir := range zipDirs {
Colin Cross635acc92017-09-12 22:50:46 -0700950 var dirHeader *zip.FileHeader
Colin Crosse19c7932015-04-24 15:08:38 -0700951
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700952 if emulateJar && cleanDir+"/" == jar.MetaDir {
Colin Cross635acc92017-09-12 22:50:46 -0700953 dirHeader = jar.MetaDirFileHeader()
954 } else {
955 dirHeader = &zip.FileHeader{
956 Name: cleanDir + "/",
957 }
Chris Grossfa5b4e92021-06-02 12:56:08 -0700958 dirHeader.SetMode(0755 | os.ModeDir)
Colin Crosse5580972017-08-30 17:40:21 -0700959 }
Jeff Gaston8edbb3a2017-08-22 20:05:28 -0700960
Colin Cross635acc92017-09-12 22:50:46 -0700961 dirHeader.SetModTime(z.time)
962
Colin Crosse5580972017-08-30 17:40:21 -0700963 ze := make(chan *zipEntry, 1)
964 ze <- &zipEntry{
965 fh: dirHeader,
966 }
967 close(ze)
968 z.writeOps <- ze
Colin Crosse19c7932015-04-24 15:08:38 -0700969 }
Colin Crosse19c7932015-04-24 15:08:38 -0700970 }
971
972 return nil
973}
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700974
Jeff Gastonc3bdc972017-10-12 12:18:19 -0700975func (z *ZipWriter) writeSymlink(rel, file string) error {
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700976 fileHeader := &zip.FileHeader{
977 Name: rel,
978 }
979 fileHeader.SetModTime(z.time)
Colin Cross297d9bc2018-06-22 16:37:47 -0700980 fileHeader.SetMode(0777 | os.ModeSymlink)
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700981
Colin Cross05518bc2018-09-27 15:06:19 -0700982 dest, err := z.fs.Readlink(file)
Dan Willemsena59a3bc2016-08-03 17:47:23 -0700983 if err != nil {
984 return err
985 }
986
Colin Cross297d9bc2018-06-22 16:37:47 -0700987 fileHeader.UncompressedSize64 = uint64(len(dest))
988 fileHeader.CRC32 = crc32.ChecksumIEEE([]byte(dest))
989
Dan Willemsen017d8932016-08-04 15:43:03 -0700990 ze := make(chan *zipEntry, 1)
991 futureReaders := make(chan chan io.Reader, 1)
992 futureReader := make(chan io.Reader, 1)
993 futureReaders <- futureReader
994 close(futureReaders)
995 futureReader <- bytes.NewBufferString(dest)
996 close(futureReader)
997
Dan Willemsen017d8932016-08-04 15:43:03 -0700998 ze <- &zipEntry{
999 fh: fileHeader,
1000 futureReaders: futureReaders,
1001 }
1002 close(ze)
1003 z.writeOps <- ze
1004
1005 return nil
Dan Willemsena59a3bc2016-08-03 17:47:23 -07001006}