Пример #1
0
// writes compressed file to the tar
func writeCompressed(tw *tar.Writer, fn string, info Info,
	compressMethod string) (err error) {
	if sfh, err := os.Open(fn); err == nil {
		defer sfh.Close()
		if cfn, err := compressor.CompressToTemp(sfh, compressMethod); err == nil {
			logger.Debugf("compressed file: %s", cfn)
			defer os.Remove(cfn)
			if cfh, err := os.Open(cfn); err == nil {
				if fi, err := cfh.Stat(); err == nil {
					defer cfh.Close()
					end := compressor.ShorterMethod(compressMethod)
					if hdr, err := FileTarHeader(fn); err == nil {
						hdr.Name = info.Get(InfoPref+"Id") + SuffData + end
						hdr.Size = fi.Size()
						hdr.Mode = 0400
						err = WriteTar(tw, hdr, cfh)
					}
				}
			}
		}
	}
	if err != nil {
		logger.Criticalf("couldn't write %s into %s: %s", fn, tw, err)
	}
	return
}
Пример #2
0
func TestCompress(c *testing.T) {
	var (
		sf  *os.File
		cfn string
		err error
	)
	if sf, err = os.Open("tarhelper_test.go"); err != nil {
		c.Fatalf("opening file: %s", err)
	}
	var s []byte
	if s, err = ioutil.ReadAll(sf); err != nil {
		c.Fatalf("reading %s: %s", sf, err)
	}
	c.Logf("read %d.", len(s))
	if _, err := sf.Seek(0, 0); err != nil {
		c.Fatalf("seeking %s: %s", sf, err)
	}
	if cfn, err = compressor.CompressToTemp(sf, "gz"); err != nil {
		c.Fatalf("compressing %s: %s", sf, err)
	}
	defer os.Remove(cfn)
	c.Log(fmt.Sprintf("cfn=%s", cfn))
	var df io.Reader
	if df, err = os.Open(cfn); err != nil {
		c.Fatalf("opening %s: %s", cfn, err)
	}
	var r io.Reader
	if r, err = gzip.NewReader(df); err != nil {
		c.Fatalf("opening %s: %s", df, err)
	}
	var d []byte
	if d, err = ioutil.ReadAll(r); err != nil {
		c.Fatalf("reading %s: %s", r, err)
	}
	if len(s) != len(d) {
		c.Fatalf("len(s)=%d len(d)=%d", len(s), len(d))
	}
	for i := 0; i < len(s); i += 1 {
		if s[i] != d[i] {
			c.Logf("%d: s=%s d=%s", i, s[i], d[i])
		}
	}
}