build: Use maximum compression when archiving

GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/4229
This commit is contained in:
Ross Smith II 2017-07-17 13:20:13 +00:00 committed by Jakob Borg
parent fa4226cae4
commit 0ca2ed7ad7
1 changed files with 35 additions and 11 deletions

View File

@ -12,6 +12,7 @@ import (
"archive/tar" "archive/tar"
"archive/zip" "archive/zip"
"bytes" "bytes"
"compress/flate"
"compress/gzip" "compress/gzip"
"crypto/sha256" "crypto/sha256"
"errors" "errors"
@ -420,7 +421,7 @@ func build(target target, tags []string) {
tags = append(target.tags, tags...) tags = append(target.tags, tags...)
rmr(target.binaryName) rmr(target.BinaryName())
args := []string{"build", "-i", "-v", "-ldflags", ldflags()} args := []string{"build", "-i", "-v", "-ldflags", ldflags()}
if len(tags) > 0 { if len(tags) > 0 {
args = append(args, "-tags", strings.Join(tags, " ")) args = append(args, "-tags", strings.Join(tags, " "))
@ -449,12 +450,12 @@ func buildTar(target target) {
build(target, tags) build(target, tags)
if goos == "darwin" { if goos == "darwin" {
macosCodesign(target.binaryName) macosCodesign(target.BinaryName())
} }
for i := range target.archiveFiles { for i := range target.archiveFiles {
target.archiveFiles[i].src = strings.Replace(target.archiveFiles[i].src, "{{binary}}", target.binaryName, 1) target.archiveFiles[i].src = strings.Replace(target.archiveFiles[i].src, "{{binary}}", target.BinaryName(), 1)
target.archiveFiles[i].dst = strings.Replace(target.archiveFiles[i].dst, "{{binary}}", target.binaryName, 1) target.archiveFiles[i].dst = strings.Replace(target.archiveFiles[i].dst, "{{binary}}", target.BinaryName(), 1)
target.archiveFiles[i].dst = name + "/" + target.archiveFiles[i].dst target.archiveFiles[i].dst = name + "/" + target.archiveFiles[i].dst
} }
@ -463,8 +464,6 @@ func buildTar(target target) {
} }
func buildZip(target target) { func buildZip(target target) {
target.binaryName += ".exe"
name := archiveName(target) name := archiveName(target)
filename := name + ".zip" filename := name + ".zip"
@ -477,8 +476,8 @@ func buildZip(target target) {
build(target, tags) build(target, tags)
for i := range target.archiveFiles { for i := range target.archiveFiles {
target.archiveFiles[i].src = strings.Replace(target.archiveFiles[i].src, "{{binary}}", target.binaryName, 1) target.archiveFiles[i].src = strings.Replace(target.archiveFiles[i].src, "{{binary}}", target.BinaryName(), 1)
target.archiveFiles[i].dst = strings.Replace(target.archiveFiles[i].dst, "{{binary}}", target.binaryName, 1) target.archiveFiles[i].dst = strings.Replace(target.archiveFiles[i].dst, "{{binary}}", target.BinaryName(), 1)
target.archiveFiles[i].dst = name + "/" + target.archiveFiles[i].dst target.archiveFiles[i].dst = name + "/" + target.archiveFiles[i].dst
} }
@ -503,8 +502,8 @@ func buildDeb(target target) {
build(target, []string{"noupgrade"}) build(target, []string{"noupgrade"})
for i := range target.installationFiles { for i := range target.installationFiles {
target.installationFiles[i].src = strings.Replace(target.installationFiles[i].src, "{{binary}}", target.binaryName, 1) target.installationFiles[i].src = strings.Replace(target.installationFiles[i].src, "{{binary}}", target.BinaryName(), 1)
target.installationFiles[i].dst = strings.Replace(target.installationFiles[i].dst, "{{binary}}", target.binaryName, 1) target.installationFiles[i].dst = strings.Replace(target.installationFiles[i].dst, "{{binary}}", target.BinaryName(), 1)
} }
for _, af := range target.installationFiles { for _, af := range target.installationFiles {
@ -919,7 +918,10 @@ func tarGz(out string, files []archiveFile) {
log.Fatal(err) log.Fatal(err)
} }
gw := gzip.NewWriter(fd) gw, err := gzip.NewWriterLevel(fd, gzip.BestCompression)
if err != nil {
log.Fatal(err)
}
tw := tar.NewWriter(gw) tw := tar.NewWriter(gw)
for _, f := range files { for _, f := range files {
@ -972,6 +974,21 @@ func zipFile(out string, files []archiveFile) {
zw := zip.NewWriter(fd) zw := zip.NewWriter(fd)
var fw *flate.Writer
// Register the deflator.
zw.RegisterCompressor(zip.Deflate, func(out io.Writer) (io.WriteCloser, error) {
var err error
if fw == nil {
// Creating a flate compressor for every file is
// expensive, create one and reuse it.
fw, err = flate.NewWriter(out, flate.BestCompression)
} else {
fw.Reset(out)
}
return fw, err
})
for _, f := range files { for _, f := range files {
sf, err := os.Open(f.src) sf, err := os.Open(f.src)
if err != nil { if err != nil {
@ -1166,3 +1183,10 @@ func gopath() string {
// The gopath is not valid. // The gopath is not valid.
return "" return ""
} }
func (t target) BinaryName() string {
if goos == "windows" {
return t.binaryName + ".exe"
}
return t.binaryName
}