File: compress.go

package info (click to toggle)
aptly 1.6.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 49,928 kB
  • sloc: python: 10,398; sh: 252; makefile: 184
file content (38 lines) | stat: -rw-r--r-- 722 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package utils

import (
	"io"
	"os"
	"os/exec"

	"github.com/klauspost/pgzip"
)

// 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
func CompressFile(source *os.File, onlyGzip bool) error {
	gzPath := source.Name() + ".gz"
	gzFile, err := os.Create(gzPath)
	if err != nil {
		return err
	}
	defer func() {
		_ = gzFile.Close()
	}()

	gzWriter := pgzip.NewWriter(gzFile)
	defer func() {
		_ = gzWriter.Close()
	}()

	_, _ = source.Seek(0, 0)
	_, err = io.Copy(gzWriter, source)
	if err != nil || onlyGzip {
		return err
	}

	cmd := exec.Command("bzip2", "-k", "-f", source.Name())
	return cmd.Run()
}