Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(39)

Delta Between Two Patch Sets: src/pkg/archive/zip/register.go

Issue 12421043: code review 12421043: archive/zip: allow user-extensible compression methods
Left Patch Set: diff -r d20b285974a4 http://go.googlecode.com/hg/ Created 10 years, 7 months ago
Right Patch Set: diff -r 7500ec6996b7 http://go.googlecode.com/hg/ Created 10 years, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Right: Side by side diff | Download
« no previous file with change/comment | « src/pkg/archive/zip/reader.go ('k') | src/pkg/archive/zip/writer.go » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
(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 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b