-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
43 lines (33 loc) · 805 Bytes
/
main.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
package mszip
import (
"bytes"
"compress/flate"
"errors"
"io"
)
type MsZip struct {
dictionary []byte
}
func New() MsZip {
return MsZip{
[]byte(""),
}
}
func (m MsZip) Decompress(input io.ReadSeeker, decompressedSize int) ([]byte, error) {
output := make([]byte, decompressedSize)
magic := make([]byte, 2)
input.Read(magic)
if !bytes.Equal(magic, []byte("CK")) { // CK = Chris Kirmse, official Microsoft purloiner
return nil, errors.New("file is corrupted")
}
// last 32k of decompressed data of previous block is used as dictionary
dictOffset := len(output) - (1 << 15)
if dictOffset < 0 {
dictOffset = 0
}
decompressor := flate.NewReaderDict(input, m.dictionary)
decompressor.Read(output)
decompressor.Close()
m.dictionary = output[dictOffset:]
return output, nil
}