lin
2025-07-31 065ea569db06206874bbfa18eb25ff6121aec09b
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
// Copyright 2015 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
 
package zip
 
import (
   "bytes"
   "compress/flate"
   "errors"
   "fmt"
   "hash/crc32"
   "io"
   "io/ioutil"
   "os"
   "path/filepath"
   "sort"
   "strings"
   "sync"
   "syscall"
   "time"
   "unicode"
 
   "github.com/google/blueprint/pathtools"
 
   "android/soong/jar"
   "android/soong/third_party/zip"
)
 
// Block size used during parallel compression of a single file.
const parallelBlockSize = 1 * 1024 * 1024 // 1MB
 
// Minimum file size to use parallel compression. It requires more
// flate.Writer allocations, since we can't change the dictionary
// during Reset
const minParallelFileSize = parallelBlockSize * 6
 
// Size of the ZIP compression window (32KB)
const windowSize = 32 * 1024
 
type nopCloser struct {
   io.Writer
}
 
func (nopCloser) Close() error {
   return nil
}
 
type byteReaderCloser struct {
   *bytes.Reader
   io.Closer
}
 
type pathMapping struct {
   dest, src string
   zipMethod uint16
}
 
type FileArg struct {
   PathPrefixInZip, SourcePrefixToStrip string
   SourceFiles                          []string
   JunkPaths                            bool
   GlobDir                              string
}
 
type FileArgsBuilder struct {
   state FileArg
   err   error
   fs    pathtools.FileSystem
 
   fileArgs []FileArg
}
 
func NewFileArgsBuilder() *FileArgsBuilder {
   return &FileArgsBuilder{
       fs: pathtools.OsFs,
   }
}
 
func (b *FileArgsBuilder) JunkPaths(v bool) *FileArgsBuilder {
   b.state.JunkPaths = v
   b.state.SourcePrefixToStrip = ""
   return b
}
 
func (b *FileArgsBuilder) SourcePrefixToStrip(prefixToStrip string) *FileArgsBuilder {
   b.state.JunkPaths = false
   b.state.SourcePrefixToStrip = prefixToStrip
   return b
}
 
func (b *FileArgsBuilder) PathPrefixInZip(rootPrefix string) *FileArgsBuilder {
   b.state.PathPrefixInZip = rootPrefix
   return b
}
 
func (b *FileArgsBuilder) File(name string) *FileArgsBuilder {
   if b.err != nil {
       return b
   }
 
   arg := b.state
   arg.SourceFiles = []string{name}
   b.fileArgs = append(b.fileArgs, arg)
   return b
}
 
func (b *FileArgsBuilder) Dir(name string) *FileArgsBuilder {
   if b.err != nil {
       return b
   }
 
   arg := b.state
   arg.GlobDir = name
   b.fileArgs = append(b.fileArgs, arg)
   return b
}
 
func (b *FileArgsBuilder) List(name string) *FileArgsBuilder {
   if b.err != nil {
       return b
   }
 
   f, err := b.fs.Open(name)
   if err != nil {
       b.err = err
       return b
   }
   defer f.Close()
 
   list, err := ioutil.ReadAll(f)
   if err != nil {
       b.err = err
       return b
   }
 
   arg := b.state
   arg.SourceFiles = strings.Split(string(list), "\n")
   b.fileArgs = append(b.fileArgs, arg)
   return b
}
 
func (b *FileArgsBuilder) Error() error {
   if b == nil {
       return nil
   }
   return b.err
}
 
func (b *FileArgsBuilder) FileArgs() []FileArg {
   if b == nil {
       return nil
   }
   return b.fileArgs
}
 
type IncorrectRelativeRootError struct {
   RelativeRoot string
   Path         string
}
 
func (x IncorrectRelativeRootError) Error() string {
   return fmt.Sprintf("path %q is outside relative root %q", x.Path, x.RelativeRoot)
}
 
type ZipWriter struct {
   time         time.Time
   createdFiles map[string]string
   createdDirs  map[string]string
   directories  bool
 
   errors   chan error
   writeOps chan chan *zipEntry
 
   cpuRateLimiter    *CPURateLimiter
   memoryRateLimiter *MemoryRateLimiter
 
   compressorPool sync.Pool
   compLevel      int
 
   followSymlinks     pathtools.ShouldFollowSymlinks
   ignoreMissingFiles bool
 
   stderr io.Writer
   fs     pathtools.FileSystem
}
 
type zipEntry struct {
   fh *zip.FileHeader
 
   // List of delayed io.Reader
   futureReaders chan chan io.Reader
 
   // Only used for passing into the MemoryRateLimiter to ensure we
   // release as much memory as much as we request
   allocatedSize int64
}
 
type ZipArgs struct {
   FileArgs                 []FileArg
   OutputFilePath           string
   EmulateJar               bool
   AddDirectoryEntriesToZip bool
   CompressionLevel         int
   ManifestSourcePath       string
   NumParallelJobs          int
   NonDeflatedFiles         map[string]bool
   WriteIfChanged           bool
   StoreSymlinks            bool
   IgnoreMissingFiles       bool
 
   Stderr     io.Writer
   Filesystem pathtools.FileSystem
}
 
const NOQUOTE = '\x00'
 
func ReadRespFile(bytes []byte) []string {
   var args []string
   var arg []rune
 
   isEscaping := false
   quotingStart := NOQUOTE
   for _, c := range string(bytes) {
       switch {
       case isEscaping:
           if quotingStart == '"' {
               if !(c == '"' || c == '\\') {
                   // '\"' or '\\' will be escaped under double quoting.
                   arg = append(arg, '\\')
               }
           }
           arg = append(arg, c)
           isEscaping = false
       case c == '\\' && quotingStart != '\'':
           isEscaping = true
       case quotingStart == NOQUOTE && (c == '\'' || c == '"'):
           quotingStart = c
       case quotingStart != NOQUOTE && c == quotingStart:
           quotingStart = NOQUOTE
       case quotingStart == NOQUOTE && unicode.IsSpace(c):
           // Current character is a space outside quotes
           if len(arg) != 0 {
               args = append(args, string(arg))
           }
           arg = arg[:0]
       default:
           arg = append(arg, c)
       }
   }
 
   if len(arg) != 0 {
       args = append(args, string(arg))
   }
 
   return args
}
 
func ZipTo(args ZipArgs, w io.Writer) error {
   if args.EmulateJar {
       args.AddDirectoryEntriesToZip = true
   }
 
   // Have Glob follow symlinks if they are not being stored as symlinks in the zip file.
   followSymlinks := pathtools.ShouldFollowSymlinks(!args.StoreSymlinks)
 
   z := &ZipWriter{
       time:               jar.DefaultTime,
       createdDirs:        make(map[string]string),
       createdFiles:       make(map[string]string),
       directories:        args.AddDirectoryEntriesToZip,
       compLevel:          args.CompressionLevel,
       followSymlinks:     followSymlinks,
       ignoreMissingFiles: args.IgnoreMissingFiles,
       stderr:             args.Stderr,
       fs:                 args.Filesystem,
   }
 
   if z.fs == nil {
       z.fs = pathtools.OsFs
   }
 
   if z.stderr == nil {
       z.stderr = os.Stderr
   }
 
   pathMappings := []pathMapping{}
 
   noCompression := args.CompressionLevel == 0
 
   for _, fa := range args.FileArgs {
       var srcs []string
       for _, s := range fa.SourceFiles {
           s = strings.TrimSpace(s)
           if s == "" {
               continue
           }
 
           globbed, _, err := z.fs.Glob(s, nil, followSymlinks)
           if err != nil {
               return err
           }
           if len(globbed) == 0 {
               err := &os.PathError{
                   Op:   "lstat",
                   Path: s,
                   Err:  os.ErrNotExist,
               }
               if args.IgnoreMissingFiles {
                   fmt.Fprintln(z.stderr, "warning:", err)
               } else {
                   return err
               }
           }
           srcs = append(srcs, globbed...)
       }
       if fa.GlobDir != "" {
           if exists, isDir, err := z.fs.Exists(fa.GlobDir); err != nil {
               return err
           } else if !exists && !args.IgnoreMissingFiles {
               err := &os.PathError{
                   Op:   "lstat",
                   Path: fa.GlobDir,
                   Err:  os.ErrNotExist,
               }
               if args.IgnoreMissingFiles {
                   fmt.Fprintln(z.stderr, "warning:", err)
               } else {
                   return err
               }
           } else if !isDir && !args.IgnoreMissingFiles {
               err := &os.PathError{
                   Op:   "lstat",
                   Path: fa.GlobDir,
                   Err:  syscall.ENOTDIR,
               }
               if args.IgnoreMissingFiles {
                   fmt.Fprintln(z.stderr, "warning:", err)
               } else {
                   return err
               }
           }
           globbed, _, err := z.fs.Glob(filepath.Join(fa.GlobDir, "**/*"), nil, followSymlinks)
           if err != nil {
               return err
           }
           srcs = append(srcs, globbed...)
       }
       for _, src := range srcs {
           err := fillPathPairs(fa, src, &pathMappings, args.NonDeflatedFiles, noCompression)
           if err != nil {
               return err
           }
       }
   }
 
   return z.write(w, pathMappings, args.ManifestSourcePath, args.EmulateJar, args.NumParallelJobs)
}
 
func Zip(args ZipArgs) error {
   if args.OutputFilePath == "" {
       return fmt.Errorf("output file path must be nonempty")
   }
 
   buf := &bytes.Buffer{}
   var out io.Writer = buf
 
   if !args.WriteIfChanged {
       f, err := os.Create(args.OutputFilePath)
       if err != nil {
           return err
       }
 
       defer f.Close()
       defer func() {
           if err != nil {
               os.Remove(args.OutputFilePath)
           }
       }()
 
       out = f
   }
 
   err := ZipTo(args, out)
   if err != nil {
       return err
   }
 
   if args.WriteIfChanged {
       err := pathtools.WriteFileIfChanged(args.OutputFilePath, buf.Bytes(), 0666)
       if err != nil {
           return err
       }
   }
 
   return nil
}
 
func fillPathPairs(fa FileArg, src string, pathMappings *[]pathMapping,
   nonDeflatedFiles map[string]bool, noCompression bool) error {
 
   var dest string
 
   if fa.JunkPaths {
       dest = filepath.Base(src)
   } else {
       var err error
       dest, err = filepath.Rel(fa.SourcePrefixToStrip, src)
       if err != nil {
           return err
       }
       if strings.HasPrefix(dest, "../") {
           return IncorrectRelativeRootError{
               Path:         src,
               RelativeRoot: fa.SourcePrefixToStrip,
           }
       }
 
   }
   dest = filepath.Join(fa.PathPrefixInZip, dest)
 
   zipMethod := zip.Deflate
   if _, found := nonDeflatedFiles[dest]; found || noCompression {
       zipMethod = zip.Store
   }
   *pathMappings = append(*pathMappings,
       pathMapping{dest: dest, src: src, zipMethod: zipMethod})
 
   return nil
}
 
func jarSort(mappings []pathMapping) {
   less := func(i int, j int) (smaller bool) {
       return jar.EntryNamesLess(mappings[i].dest, mappings[j].dest)
   }
   sort.SliceStable(mappings, less)
}
 
func (z *ZipWriter) write(f io.Writer, pathMappings []pathMapping, manifest string, emulateJar bool, parallelJobs int) error {
   z.errors = make(chan error)
   defer close(z.errors)
 
   // This channel size can be essentially unlimited -- it's used as a fifo
   // queue decouple the CPU and IO loads. Directories don't require any
   // compression time, but still cost some IO. Similar with small files that
   // can be very fast to compress. Some files that are more difficult to
   // compress won't take a corresponding longer time writing out.
   //
   // The optimum size here depends on your CPU and IO characteristics, and
   // the the layout of your zip file. 1000 was chosen mostly at random as
   // something that worked reasonably well for a test file.
   //
   // The RateLimit object will put the upper bounds on the number of
   // parallel compressions and outstanding buffers.
   z.writeOps = make(chan chan *zipEntry, 1000)
   z.cpuRateLimiter = NewCPURateLimiter(int64(parallelJobs))
   z.memoryRateLimiter = NewMemoryRateLimiter(0)
   defer func() {
       z.cpuRateLimiter.Stop()
       z.memoryRateLimiter.Stop()
   }()
 
   if manifest != "" && !emulateJar {
       return errors.New("must specify --jar when specifying a manifest via -m")
   }
 
   if emulateJar {
       // manifest may be empty, in which case addManifest will fill in a default
       pathMappings = append(pathMappings, pathMapping{jar.ManifestFile, manifest, zip.Deflate})
 
       jarSort(pathMappings)
   }
 
   go func() {
       var err error
       defer close(z.writeOps)
 
       for _, ele := range pathMappings {
           if emulateJar && ele.dest == jar.ManifestFile {
               err = z.addManifest(ele.dest, ele.src, ele.zipMethod)
           } else {
               err = z.addFile(ele.dest, ele.src, ele.zipMethod, emulateJar)
           }
           if err != nil {
               z.errors <- err
               return
           }
       }
   }()
 
   zipw := zip.NewWriter(f)
 
   var currentWriteOpChan chan *zipEntry
   var currentWriter io.WriteCloser
   var currentReaders chan chan io.Reader
   var currentReader chan io.Reader
   var done bool
 
   for !done {
       var writeOpsChan chan chan *zipEntry
       var writeOpChan chan *zipEntry
       var readersChan chan chan io.Reader
 
       if currentReader != nil {
           // Only read and process errors
       } else if currentReaders != nil {
           readersChan = currentReaders
       } else if currentWriteOpChan != nil {
           writeOpChan = currentWriteOpChan
       } else {
           writeOpsChan = z.writeOps
       }
 
       select {
       case writeOp, ok := <-writeOpsChan:
           if !ok {
               done = true
           }
 
           currentWriteOpChan = writeOp
 
       case op := <-writeOpChan:
           currentWriteOpChan = nil
 
           var err error
           if op.fh.Method == zip.Deflate {
               currentWriter, err = zipw.CreateCompressedHeader(op.fh)
           } else {
               var zw io.Writer
 
               op.fh.CompressedSize64 = op.fh.UncompressedSize64
 
               zw, err = zipw.CreateHeaderAndroid(op.fh)
               currentWriter = nopCloser{zw}
           }
           if err != nil {
               return err
           }
 
           currentReaders = op.futureReaders
           if op.futureReaders == nil {
               currentWriter.Close()
               currentWriter = nil
           }
           z.memoryRateLimiter.Finish(op.allocatedSize)
 
       case futureReader, ok := <-readersChan:
           if !ok {
               // Done with reading
               currentWriter.Close()
               currentWriter = nil
               currentReaders = nil
           }
 
           currentReader = futureReader
 
       case reader := <-currentReader:
           _, err := io.Copy(currentWriter, reader)
           if err != nil {
               return err
           }
 
           currentReader = nil
 
       case err := <-z.errors:
           return err
       }
   }
 
   // One last chance to catch an error
   select {
   case err := <-z.errors:
       return err
   default:
       zipw.Close()
       return nil
   }
}
 
// imports (possibly with compression) <src> into the zip at sub-path <dest>
func (z *ZipWriter) addFile(dest, src string, method uint16, emulateJar bool) error {
   var fileSize int64
   var executable bool
 
   var s os.FileInfo
   var err error
   if z.followSymlinks {
       s, err = z.fs.Stat(src)
   } else {
       s, err = z.fs.Lstat(src)
   }
 
   if err != nil {
       if os.IsNotExist(err) && z.ignoreMissingFiles {
           fmt.Fprintln(z.stderr, "warning:", err)
           return nil
       }
       return err
   } else if s.IsDir() {
       if z.directories {
           return z.writeDirectory(dest, src, emulateJar)
       }
       return nil
   } else {
       if err := z.writeDirectory(filepath.Dir(dest), src, emulateJar); err != nil {
           return err
       }
 
       if prev, exists := z.createdDirs[dest]; exists {
           return fmt.Errorf("destination %q is both a directory %q and a file %q", dest, prev, src)
       }
       if prev, exists := z.createdFiles[dest]; exists {
           return fmt.Errorf("destination %q has two files %q and %q", dest, prev, src)
       }
 
       z.createdFiles[dest] = src
 
       if s.Mode()&os.ModeSymlink != 0 {
           return z.writeSymlink(dest, src)
       } else if !s.Mode().IsRegular() {
           return fmt.Errorf("%s is not a file, directory, or symlink", src)
       }
 
       fileSize = s.Size()
       executable = s.Mode()&0100 != 0
   }
 
   r, err := z.fs.Open(src)
   if err != nil {
       return err
   }
 
   header := &zip.FileHeader{
       Name:               dest,
       Method:             method,
       UncompressedSize64: uint64(fileSize),
   }
 
   if executable {
       header.SetMode(0700)
   }
 
   return z.writeFileContents(header, r)
}
 
func (z *ZipWriter) addManifest(dest string, src string, method uint16) error {
   if prev, exists := z.createdDirs[dest]; exists {
       return fmt.Errorf("destination %q is both a directory %q and a file %q", dest, prev, src)
   }
   if prev, exists := z.createdFiles[dest]; exists {
       return fmt.Errorf("destination %q has two files %q and %q", dest, prev, src)
   }
 
   if err := z.writeDirectory(filepath.Dir(dest), src, true); err != nil {
       return err
   }
 
   var contents []byte
   if src != "" {
       f, err := z.fs.Open(src)
       if err != nil {
           return err
       }
 
       contents, err = ioutil.ReadAll(f)
       f.Close()
       if err != nil {
           return err
       }
   }
 
   fh, buf, err := jar.ManifestFileContents(contents)
   if err != nil {
       return err
   }
 
   reader := &byteReaderCloser{bytes.NewReader(buf), ioutil.NopCloser(nil)}
 
   return z.writeFileContents(fh, reader)
}
 
func (z *ZipWriter) writeFileContents(header *zip.FileHeader, r pathtools.ReaderAtSeekerCloser) (err error) {
 
   header.SetModTime(z.time)
 
   compressChan := make(chan *zipEntry, 1)
   z.writeOps <- compressChan
 
   // Pre-fill a zipEntry, it will be sent in the compressChan once
   // we're sure about the Method and CRC.
   ze := &zipEntry{
       fh: header,
   }
 
   ze.allocatedSize = int64(header.UncompressedSize64)
   z.cpuRateLimiter.Request()
   z.memoryRateLimiter.Request(ze.allocatedSize)
 
   fileSize := int64(header.UncompressedSize64)
   if fileSize == 0 {
       fileSize = int64(header.UncompressedSize)
   }
 
   if header.Method == zip.Deflate && fileSize >= minParallelFileSize {
       wg := new(sync.WaitGroup)
 
       // Allocate enough buffer to hold all readers. We'll limit
       // this based on actual buffer sizes in RateLimit.
       ze.futureReaders = make(chan chan io.Reader, (fileSize/parallelBlockSize)+1)
 
       // Calculate the CRC in the background, since reading the entire
       // file could take a while.
       //
       // We could split this up into chunks as well, but it's faster
       // than the compression. Due to the Go Zip API, we also need to
       // know the result before we can begin writing the compressed
       // data out to the zipfile.
       wg.Add(1)
       go z.crcFile(r, ze, compressChan, wg)
 
       for start := int64(0); start < fileSize; start += parallelBlockSize {
           sr := io.NewSectionReader(r, start, parallelBlockSize)
           resultChan := make(chan io.Reader, 1)
           ze.futureReaders <- resultChan
 
           z.cpuRateLimiter.Request()
 
           last := !(start+parallelBlockSize < fileSize)
           var dict []byte
           if start >= windowSize {
               dict, err = ioutil.ReadAll(io.NewSectionReader(r, start-windowSize, windowSize))
               if err != nil {
                   return err
               }
           }
 
           wg.Add(1)
           go z.compressPartialFile(sr, dict, last, resultChan, wg)
       }
 
       close(ze.futureReaders)
 
       // Close the file handle after all readers are done
       go func(wg *sync.WaitGroup, closer io.Closer) {
           wg.Wait()
           closer.Close()
       }(wg, r)
   } else {
       go func() {
           z.compressWholeFile(ze, r, compressChan)
           r.Close()
       }()
   }
 
   return nil
}
 
func (z *ZipWriter) crcFile(r io.Reader, ze *zipEntry, resultChan chan *zipEntry, wg *sync.WaitGroup) {
   defer wg.Done()
   defer z.cpuRateLimiter.Finish()
 
   crc := crc32.NewIEEE()
   _, err := io.Copy(crc, r)
   if err != nil {
       z.errors <- err
       return
   }
 
   ze.fh.CRC32 = crc.Sum32()
   resultChan <- ze
   close(resultChan)
}
 
func (z *ZipWriter) compressPartialFile(r io.Reader, dict []byte, last bool, resultChan chan io.Reader, wg *sync.WaitGroup) {
   defer wg.Done()
 
   result, err := z.compressBlock(r, dict, last)
   if err != nil {
       z.errors <- err
       return
   }
 
   z.cpuRateLimiter.Finish()
 
   resultChan <- result
}
 
func (z *ZipWriter) compressBlock(r io.Reader, dict []byte, last bool) (*bytes.Buffer, error) {
   buf := new(bytes.Buffer)
   var fw *flate.Writer
   var err error
   if len(dict) > 0 {
       // There's no way to Reset a Writer with a new dictionary, so
       // don't use the Pool
       fw, err = flate.NewWriterDict(buf, z.compLevel, dict)
   } else {
       var ok bool
       if fw, ok = z.compressorPool.Get().(*flate.Writer); ok {
           fw.Reset(buf)
       } else {
           fw, err = flate.NewWriter(buf, z.compLevel)
       }
       defer z.compressorPool.Put(fw)
   }
   if err != nil {
       return nil, err
   }
 
   _, err = io.Copy(fw, r)
   if err != nil {
       return nil, err
   }
   if last {
       fw.Close()
   } else {
       fw.Flush()
   }
 
   return buf, nil
}
 
func (z *ZipWriter) compressWholeFile(ze *zipEntry, r io.ReadSeeker, compressChan chan *zipEntry) {
 
   crc := crc32.NewIEEE()
   _, err := io.Copy(crc, r)
   if err != nil {
       z.errors <- err
       return
   }
 
   ze.fh.CRC32 = crc.Sum32()
 
   _, err = r.Seek(0, 0)
   if err != nil {
       z.errors <- err
       return
   }
 
   readFile := func(reader io.ReadSeeker) ([]byte, error) {
       _, err := reader.Seek(0, 0)
       if err != nil {
           return nil, err
       }
 
       buf, err := ioutil.ReadAll(reader)
       if err != nil {
           return nil, err
       }
 
       return buf, nil
   }
 
   ze.futureReaders = make(chan chan io.Reader, 1)
   futureReader := make(chan io.Reader, 1)
   ze.futureReaders <- futureReader
   close(ze.futureReaders)
 
   if ze.fh.Method == zip.Deflate {
       compressed, err := z.compressBlock(r, nil, true)
       if err != nil {
           z.errors <- err
           return
       }
       if uint64(compressed.Len()) < ze.fh.UncompressedSize64 {
           futureReader <- compressed
       } else {
           buf, err := readFile(r)
           if err != nil {
               z.errors <- err
               return
           }
           ze.fh.Method = zip.Store
           futureReader <- bytes.NewReader(buf)
       }
   } else {
       buf, err := readFile(r)
       if err != nil {
           z.errors <- err
           return
       }
       ze.fh.Method = zip.Store
       futureReader <- bytes.NewReader(buf)
   }
 
   z.cpuRateLimiter.Finish()
 
   close(futureReader)
 
   compressChan <- ze
   close(compressChan)
}
 
// writeDirectory annotates that dir is a directory created for the src file or directory, and adds
// the directory entry to the zip file if directories are enabled.
func (z *ZipWriter) writeDirectory(dir string, src string, emulateJar bool) error {
   // clean the input
   dir = filepath.Clean(dir)
 
   // discover any uncreated directories in the path
   zipDirs := []string{}
   for dir != "" && dir != "." {
       if _, exists := z.createdDirs[dir]; exists {
           break
       }
 
       if prev, exists := z.createdFiles[dir]; exists {
           return fmt.Errorf("destination %q is both a directory %q and a file %q", dir, src, prev)
       }
 
       z.createdDirs[dir] = src
       // parent directories precede their children
       zipDirs = append([]string{dir}, zipDirs...)
 
       dir = filepath.Dir(dir)
   }
 
   if z.directories {
       // make a directory entry for each uncreated directory
       for _, cleanDir := range zipDirs {
           var dirHeader *zip.FileHeader
 
           if emulateJar && cleanDir+"/" == jar.MetaDir {
               dirHeader = jar.MetaDirFileHeader()
           } else {
               dirHeader = &zip.FileHeader{
                   Name: cleanDir + "/",
               }
               dirHeader.SetMode(0700 | os.ModeDir)
           }
 
           dirHeader.SetModTime(z.time)
 
           ze := make(chan *zipEntry, 1)
           ze <- &zipEntry{
               fh: dirHeader,
           }
           close(ze)
           z.writeOps <- ze
       }
   }
 
   return nil
}
 
func (z *ZipWriter) writeSymlink(rel, file string) error {
   fileHeader := &zip.FileHeader{
       Name: rel,
   }
   fileHeader.SetModTime(z.time)
   fileHeader.SetMode(0777 | os.ModeSymlink)
 
   dest, err := z.fs.Readlink(file)
   if err != nil {
       return err
   }
 
   fileHeader.UncompressedSize64 = uint64(len(dest))
   fileHeader.CRC32 = crc32.ChecksumIEEE([]byte(dest))
 
   ze := make(chan *zipEntry, 1)
   futureReaders := make(chan chan io.Reader, 1)
   futureReader := make(chan io.Reader, 1)
   futureReaders <- futureReader
   close(futureReaders)
   futureReader <- bytes.NewBufferString(dest)
   close(futureReader)
 
   ze <- &zipEntry{
       fh:            fileHeader,
       futureReaders: futureReaders,
   }
   close(ze)
   z.writeOps <- ze
 
   return nil
}