CompressFile compresses file specified by source to .gz & .bz2 It uses internal gzip and external bzip2, see: https://code.google.com/p/go/issues/detail?id=4828
(source *os.File, onlyGzip bool)
| 13 | // It uses internal gzip and external bzip2, see: |
| 14 | // https://code.google.com/p/go/issues/detail?id=4828 |
| 15 | func CompressFile(source *os.File, onlyGzip bool) error { |
| 16 | gzPath := source.Name() + ".gz" |
| 17 | gzFile, err := os.Create(gzPath) |
| 18 | if err != nil { |
| 19 | return err |
| 20 | } |
| 21 | defer func() { |
| 22 | _ = gzFile.Close() |
| 23 | }() |
| 24 | |
| 25 | gzWriter := pgzip.NewWriter(gzFile) |
| 26 | defer func() { |
| 27 | _ = gzWriter.Close() |
| 28 | }() |
| 29 | |
| 30 | _, _ = source.Seek(0, 0) |
| 31 | _, err = io.Copy(gzWriter, source) |
| 32 | if err != nil || onlyGzip { |
| 33 | return err |
| 34 | } |
| 35 | |
| 36 | cmd := exec.Command("bzip2", "-k", "-f", source.Name()) |
| 37 | return cmd.Run() |
| 38 | } |