forked from juju/charmstore
/
zip.go
49 lines (43 loc) · 1.31 KB
/
zip.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
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package charmstore // import "gopkg.in/juju/charmstore.v5/internal/charmstore"
import (
"archive/zip"
"compress/flate"
"io"
"gopkg.in/errgo.v1"
"gopkg.in/juju/charmstore.v5/internal/mongodoc"
)
// ZipFileReader returns a reader that will read
// content referred to by f within zipr, which should
// refer to the contents of a zip file,
func ZipFileReader(zipr io.ReadSeeker, f mongodoc.ZipFile) (io.Reader, error) {
if _, err := zipr.Seek(f.Offset, 0); err != nil {
return nil, errgo.Notef(err, "cannot seek to %d in zip content", f.Offset)
}
content := io.LimitReader(zipr, f.Size)
if !f.Compressed {
return content, nil
}
return flate.NewReader(content), nil
}
// NewZipFile returns a new mongodoc zip file
// reference to the given zip file.
func NewZipFile(f *zip.File) (mongodoc.ZipFile, error) {
offset, err := f.DataOffset()
if err != nil {
return mongodoc.ZipFile{}, errgo.Notef(err, "cannot determine data offset for %q", f.Name)
}
zf := mongodoc.ZipFile{
Offset: offset,
Size: int64(f.CompressedSize64),
}
switch f.Method {
case zip.Store:
case zip.Deflate:
zf.Compressed = true
default:
return mongodoc.ZipFile{}, errgo.Newf("unknown zip compression method for %q", f.Name)
}
return zf, nil
}