1
0
mirror of https://github.com/krolaw/zipstream.git synced 2025-04-04 11:45:29 +00:00
Files
zipstream/reader_test.go
Richard Warburton 4a67be70fe Modernise
2024-11-09 16:47:54 +13:00

64 lines
1.1 KiB
Go

package zipstream
import (
"archive/zip"
"bytes"
"io"
"math/rand"
"testing"
)
func TestReader(t *testing.T) {
testReader(t, []byte(`<poc><firstName>Juan</firstName></poc>`))
s := new(bytes.Buffer)
io.Copy(s, io.LimitReader(rand.New(rand.NewSource(1)), 16384))
testReader(t, s.Bytes())
}
func testReader(t *testing.T, s []byte) {
var wbuf bytes.Buffer
for j := 0; j < 2; j++ {
z := zip.NewWriter(&wbuf)
for i := 0; i < 2; i++ {
zw, err := z.Create("tmp")
if err != nil {
t.Fatal(err)
}
if _, err := zw.Write(s); err != nil {
t.Fatal(err)
}
}
if err := z.Close(); err != nil {
t.Fatal(err)
}
}
zr := NewReader(&wbuf)
for j := 0; j < 2; j++ {
fcount := 0
for {
_, err := zr.Next()
if err != nil {
if err != io.EOF {
t.Fatal(err)
}
if fcount != 2 {
t.Fatal("Embeded file missing", j, fcount, err)
}
break // No more files
}
fcount++
s2, err := io.ReadAll(zr)
if err != nil {
t.Fatal(err)
}
if bytes.Compare(s, s2) != 0 {
t.Fatal("Decompressed data does not match original")
}
}
}
}