Debian Codegraph @

lib/zipslicer/tarzip.go

raw symbols SCIP
1 //
2 // Copyright (c) SAS Institute Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 package zipslicer
18
19 import (
20 "archive/tar"
21 "errors"
22 "fmt"
23 "io"
24 "io/ioutil"
25 "os"
26 )
27
28 const (
29 TarMemberCD = "zipdir.bin"
30 TarMemberZip = "contents.zip"
31 )
32
33 // Make a tar archive with two members:
34 // - the central directory of the zip file
35 // - the complete zip file
36 // This lets us process the zip in one pass, which normally isn't possible with
37 // the directory at the end.
38 func ZipToTar(r *os.File, w io.Writer) error {
39 size, err := r.Seek(0, io.SeekEnd)
40 if err != nil {
41 return err
42 }
43 dirLoc, err := FindDirectory(r, size)
44 if err != nil {
45 return err
46 }
47 tw := tar.NewWriter(w)
48 if _, err := r.Seek(dirLoc, 0); err != nil {
49 return err
50 }
51 if err := tarAddStream(tw, r, TarMemberCD, size-dirLoc); err != nil {
52 return err
53 }
54 if _, err := r.Seek(0, 0); err != nil {
55 return err
56 }
57 if err := tarAddStream(tw, r, TarMemberZip, size); err != nil {
58 return err
59 }
60 return tw.Close()
61 }
62
63 func tarAddStream(tw *tar.Writer, r io.Reader, name string, size int64) error {
64 hdr := &tar.Header{Name: name, Mode: 0644, Size: size}
65 if err := tw.WriteHeader(hdr); err != nil {
66 return err
67 }
68 if _, err := io.CopyN(tw, r, size); err != nil {
69 return err
70 }
71 return nil
72 }
73
74 // Read a tar stream produced by ZipToTar and return the zip directory. Files
75 // must be read from the zip in order or an error will be raised.
76 func ReadZipTar(r io.Reader) (*Directory, error) {
77 tr := tar.NewReader(r)
78 hdr, err := tr.Next()
79 if err != nil {
80 return nil, fmt.Errorf("error reading tar: %w", err)
81 } else if hdr.Name != TarMemberCD {
82 return nil, errors.New("invalid tarzip")
83 }
84 zipdir, err := ioutil.ReadAll(tr)
85 if err != nil {
86 return nil, fmt.Errorf("error reading tar: %w", err)
87 }
88 hdr, err = tr.Next()
89 if err != nil {
90 return nil, err
91 } else if hdr.Name != TarMemberZip {
92 return nil, errors.New("invalid tarzip")
93 }
94 zr := &zipTarReader{tr: tr}
95 return ReadStream(zr, hdr.Size, zipdir)
96 }
97
98 type zipTarReader struct {
99 tr *tar.Reader
100 }
101
102 func (z *zipTarReader) Read(d []byte) (int, error) {
103 if z.tr == nil {
104 return 0, io.EOF
105 }
106 n, err := z.tr.Read(d)
107 if err == io.EOF {
108 _, err2 := z.tr.Next()
109 if err2 == nil {
110 err = errors.New("invalid tarzip")
111 } else if err2 != io.EOF {
112 err = err2
113 }
114 z.tr = nil
115 }
116 return n, err
117 }