func writer(t *testing.T, tname string, w io.Writer, atype string) archive.WriteCloser { switch atype { case gzipType: return gzip.NewWriter(w) case tarType: return tar.NewWriter(w) case zipType: return zip.NewWriter(w) } t.Fatalf("%s: unrecognized archive type: %s", tname, atype) panic("execution continued after (*testing.T).Fatalf") }
// writeFiles iterates through the files we want and writes them as a zipped // file. func writeFiles(t *testing.T, f *os.File, tname string, want map[string][]byte) { w := zip.NewWriter(f) defer w.Close() // Write zipped files for fname, fbytes := range want { fi := iotest.FileInfo(t, fbytes) // Write the file err := w.NextFile(fname, fi) switch err { case io.EOF: break default: t.Fatalf("%s: write header for next file: %v", tname, err) case nil: // Proceed below } if _, err := io.Copy(w, bytes.NewReader(fbytes)); err != nil { t.Fatalf("%s: copy to writer: %v", tname, err) } } }
type unarchiver func(f *os.File) (archive.ReadCloser, error) type compressionType struct { extension string handleMultipleEntries bool archiver archiver 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",