LEFT | RIGHT |
(no file at all) | |
| 1 // Copyright 2010 The Go Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style |
| 3 // license that can be found in the LICENSE file. |
| 4 |
| 5 package zip |
| 6 |
| 7 import ( |
| 8 "compress/flate" |
| 9 "io" |
| 10 "io/ioutil" |
| 11 "sync" |
| 12 ) |
| 13 |
| 14 // A Compressor returns a compressing writer, writing to the |
| 15 // provided writer. On Close, any pending data should be flushed. |
| 16 type Compressor func(io.Writer) (io.WriteCloser, error) |
| 17 |
| 18 // Decompressor is a function that wraps a Reader with a decompressing Reader. |
| 19 // The decompressed ReadCloser is returned to callers who open files from |
| 20 // within the archive. These callers are responsible for closing this reader |
| 21 // when they're finished reading. |
| 22 type Decompressor func(io.Reader) io.ReadCloser |
| 23 |
| 24 var ( |
| 25 mu sync.RWMutex // guards compressor and decompressor maps |
| 26 |
| 27 compressors = map[uint16]Compressor{ |
| 28 Store: func(w io.Writer) (io.WriteCloser, error) { return &nop
Closer{w}, nil }, |
| 29 Deflate: func(w io.Writer) (io.WriteCloser, error) { return flat
e.NewWriter(w, 5) }, |
| 30 } |
| 31 |
| 32 decompressors = map[uint16]Decompressor{ |
| 33 Store: ioutil.NopCloser, |
| 34 Deflate: flate.NewReader, |
| 35 } |
| 36 ) |
| 37 |
| 38 // RegisterDecompressor allows custom decompressors for a specified method ID. |
| 39 func RegisterDecompressor(method uint16, d Decompressor) { |
| 40 mu.Lock() |
| 41 defer mu.Unlock() |
| 42 |
| 43 if _, ok := decompressors[method]; ok { |
| 44 panic("decompressor already registered") |
| 45 } |
| 46 decompressors[method] = d |
| 47 } |
| 48 |
| 49 // RegisterCompressor registers custom compressors for a specified method ID. |
| 50 // The common methods Store and Deflate are built in. |
| 51 func RegisterCompressor(method uint16, comp Compressor) { |
| 52 mu.Lock() |
| 53 defer mu.Unlock() |
| 54 |
| 55 if _, ok := compressors[method]; ok { |
| 56 panic("compressor already registered") |
| 57 } |
| 58 compressors[method] = comp |
| 59 } |
| 60 |
| 61 func compressor(method uint16) Compressor { |
| 62 mu.RLock() |
| 63 defer mu.RUnlock() |
| 64 return compressors[method] |
| 65 } |
| 66 |
| 67 func decompressor(method uint16) Decompressor { |
| 68 mu.RLock() |
| 69 defer mu.RUnlock() |
| 70 return decompressors[method] |
| 71 } |
LEFT | RIGHT |