예제 #1
0
func reader(t *testing.T, tname string, buf *bytes.Buffer, atype string, fname string) archive.ReadCloser {
	switch atype {
	case gzipType:
		gr, err := gzip.NewReader(buf, fname)
		if err != nil {
			t.Fatalf("%s: %v", tname, err)
		}
		return gr
	case tarType:
		return archive.NopCloser(tar.NewReader(buf))
	case zipType:
		zr, err := zip.NewReader(
			bytes.NewReader(buf.Bytes()),
			int64(buf.Len()))
		if err != nil {
			t.Fatalf("%s: new zip reader: %v", tname, err)
		}
		return archive.NopCloser(zr)
	}
	t.Fatalf("%s: unrecognized archive type: %s", tname, atype)
	panic("execution continued after (*testing.T).Fatalf")
}
예제 #2
0
파일: zip_test.go 프로젝트: cihub/seelog
// readFiles iterates through zipped files and ensures they are the same.
func readFiles(t *testing.T, f *os.File, tname string, want map[string][]byte) {
	// Get zip Reader
	fi, err := f.Stat()
	if err != nil {
		t.Fatalf("%s: stat zipped file: %v", tname, err)
	}
	r, err := zip.NewReader(f, fi.Size())
	if err != nil {
		t.Fatalf("%s: %v", tname, err)
	}

	for {
		fname, err := r.NextFile()
		switch err {
		case io.EOF:
			return
		default:
			t.Fatalf("%s: read header for next file: %v", tname, err)
		case nil: // Proceed below
		}

		wantBytes, ok := want[fname]
		if !ok {
			t.Errorf("%s: read unwanted file: %v", tname, fname)
			continue
		}

		gotBytes, err := ioutil.ReadAll(r)
		if err != nil {
			t.Fatalf("%s: read file: %v", tname, err)
		}

		if !bytes.Equal(gotBytes, wantBytes) {
			t.Errorf("%s: %q = %q but want %q", tname, fname, gotBytes, wantBytes)
		}
	}
}
예제 #3
0
	unarchiver            unarchiver
}

var compressionTypes = map[rollingArchiveType]compressionType{
	rollingArchiveZip: {
		extension:             ".zip",
		handleMultipleEntries: true,
		archiver: func(f *os.File, _ bool) archive.WriteCloser {
			return zip.NewWriter(f)
		},
		unarchiver: func(f *os.File) (archive.ReadCloser, error) {
			fi, err := f.Stat()
			if err != nil {
				return nil, err
			}
			r, err := zip.NewReader(f, fi.Size())
			if err != nil {
				return nil, err
			}
			return archive.NopCloser(r), nil
		},
	},
	rollingArchiveGzip: {
		extension:             ".gz",
		handleMultipleEntries: false,
		archiver: func(f *os.File, exploded bool) archive.WriteCloser {
			gw := gzip.NewWriter(f)
			if exploded {
				return gw
			}
			return tar.NewWriteMultiCloser(gw, gw)