// Untar files from input channel and pass the result // to the output channel. func Uncompress() gonzo.Stage { return func(ctx context.Context, in <-chan gonzo.File, out chan<- gonzo.File) error { for { select { case file, ok := <-in: if !ok { return nil } content, err := gzip.NewReader(file) if err != nil { return err } fs := gonzo.NewFile( &doublecloser{content, file.Close}, gonzo.FileInfoFrom(file.FileInfo()), ) out <- fs case <-ctx.Done(): return ctx.Err() } } } }
// Untar files from input channel and pass the result to the output channel. func Untar(opt Options) gonzo.Stage { return func(ctx context.Context, in <-chan gonzo.File, out chan<- gonzo.File) error { //Check patterns. pluck := len(opt.Pluck) > 0 if pluck { err := match.Good(opt.Pluck...) if err != nil { return err } } for { select { case file, ok := <-in: if !ok { return nil } context.WithValue(ctx, "archive", file.FileInfo().Name()).Debug("Untaring") tr := tar.NewReader(file) defer file.Close() // Iterate through the files in the archive. for { hdr, err := tr.Next() if err == io.EOF { // end of tar archive break } if err != nil { return err } name := strip(opt.StripComponenets, hdr.Name) if pluck && !match.Any(name, opt.Pluck...) { continue } context.WithValue(ctx, "file", name).Debug("Untaring") content := new(bytes.Buffer) n, err := content.ReadFrom(tr) if err != nil { return err } fs := gonzo.NewFile(ioutil.NopCloser(content), gonzo.FileInfoFrom(hdr.FileInfo())) fs.FileInfo().SetName(name) fs.FileInfo().SetSize(int64(n)) out <- fs } case <-ctx.Done(): return ctx.Err() } } } }
// A simple helper function that opens the file from the given path and // returns a pointer to a gonzo.File or an error. func Read(path string) (gonzo.File, error) { Stat, err := os.Stat(path) if err != nil { return nil, err } if Stat.IsDir() { return nil, ErrIsDir } f, err := os.Open(path) if err != nil { return nil, err } return gonzo.NewFile(f, gonzo.FileInfoFrom(Stat)), nil }
// Unzip the zip files from input channel and pass the result // to the output channel. func Unzip() gonzo.Stage { return func(ctx context.Context, in <-chan gonzo.File, out chan<- gonzo.File) error { for { select { case file, ok := <-in: if !ok { return nil } raw, err := ioutil.ReadAll(file) if err != nil { return err } file.Close() r, err := zip.NewReader(bytes.NewReader(raw), int64(len(raw))) if err != nil { return err } //counter := c.Counter("unzipping", len(r.File)) // Iterate through the files in the archive, for _, f := range r.File { ctx = context.WithValue(ctx, "file", f.Name) ctx.Info("Unziping") //counter.Set(i+1, f.Name) content, err := f.Open() if err != nil { } fs := gonzo.NewFile(content, gonzo.FileInfoFrom(f.FileInfo())) fs.FileInfo().SetName(f.Name) out <- fs } case <-ctx.Done(): return ctx.Err() } } } }