forked from krolaw/zipstream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
centralDirectory.go
66 lines (61 loc) · 1.62 KB
/
centralDirectory.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package zipstream
import (
"github.com/klauspost/compress/zip"
"bufio"
"encoding/binary"
"errors"
"io"
)
// We're not interested in the central directories data, we just want to skip over it,
// clearing the stream of the current zip, in case anything needs to be sent over the
// same stream.
func discardCentralDirectory(br *bufio.Reader) error {
for {
sigBytes, err := br.Peek(4)
if err != nil {
return err
}
switch sig := binary.LittleEndian.Uint32(sigBytes); sig {
case directoryHeaderSignature:
if err := discardDirectoryHeaderRecord(br); err != nil {
return err
}
case directoryEndSignature:
if err := discardDirectoryEndRecord(br); err != nil {
return err
}
return io.EOF
case directory64EndSignature:
return errors.New("Zip64 not yet supported")
case directory64LocSignature: // Not sure what this is yet
return errors.New("Zip64 not yet supported")
default:
return zip.ErrFormat
}
}
}
func discardDirectoryHeaderRecord(br *bufio.Reader) error {
if _, err := br.Discard(28); err != nil {
return err
}
lb, err := br.Peek(6)
if err != nil {
return err
}
lengths := int(binary.LittleEndian.Uint16(lb[:2])) + // File name length
int(binary.LittleEndian.Uint16(lb[2:4])) + // Extra field length
int(binary.LittleEndian.Uint16(lb[4:])) // File comment length
_, err = br.Discard(18 + lengths)
return err
}
func discardDirectoryEndRecord(br *bufio.Reader) error {
if _, err := br.Discard(20); err != nil {
return err
}
commentLength, err := br.Peek(2)
if err != nil {
return err
}
_, err = br.Discard(2 + int(binary.LittleEndian.Uint16(commentLength)))
return err
}