Exemplo n.º 1
0
// A simple helper function that reads the file from the given path and
// returns a pointer to a slurp.File or an error.
func Read(path string) (*slurp.File, error) {
	Stat, err := os.Stat(path)
	if err != nil {
		return nil, err
	}

	f, err := os.Open(path)
	if err != nil {
		return nil, err
	}

	fs := &slurp.File{Reader: f, Path: path, FileInfo: slurp.FileInfoFrom(Stat)}

	return fs, nil
}
Exemplo n.º 2
0
// Unzip the zip files from input channel and pass the result
// to the output channel.
func Unzip(c *slurp.C) slurp.Stage {
	return func(in <-chan slurp.File, out chan<- slurp.File) {

		var wg sync.WaitGroup
		for file := range in {

			wg.Add(1)
			go func(file slurp.File) {
				defer wg.Done()

				raw, err := ioutil.ReadAll(file)
				file.Close()

				r, err := zip.NewReader(bytes.NewReader(raw), int64(len(raw)))
				if err != nil {
					c.Error(err)
					return
				}

				counter := c.Counter("unzipping", len(r.File))

				// Iterate through the files in the archive,
				for i, f := range r.File {
					counter.Set(i+1, f.Name)

					content, err := f.Open()
					if err != nil {
					}
					fs := slurp.File{Reader: content, Dir: "", Path: f.Name, FileInfo: slurp.FileInfoFrom(f.FileInfo())}

					out <- fs

				}
			}(file)

		}
		wg.Wait()
	}
}