Skip to content

Commit

Permalink
perf: compress larger files in metadata.zip
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-azarchs committed Dec 19, 2023
1 parent c0b57a5 commit 06bfc8b
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions martian/util/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,19 @@ func addZipFile(filePath string, out io.Writer) error {
return nil
}

// isCompressedExtension returns true if the file path has an extension
// that typically implies compressed data.
//
// Typically CreateZip is used only for metadata files, none of which have these
// extensions, but checking just to be safe.
func isCompressedExtension(p string) bool {
switch filepath.Ext(p) {
case ".gz", ".png", ".jpg", "*.jpeg", "*.zip":
return true
}
return false
}

func CreateZip(zipPath string, filePaths []string) error {
f, err := os.Create(zipPath)
if err != nil {
Expand All @@ -251,6 +264,12 @@ func CreateZip(zipPath string, filePaths []string) error {
return err
}
header.Name = relPath
// Turn on compression for files > 1kB.
// For smaller files, the overhead of starting a deflate stream isn't
// really worth the trouble.
if info.Size() > 1024 && !isCompressedExtension(relPath) {
header.Method = zip.Deflate
}
out, err := zw.CreateHeader(header)
if err != nil {
return err
Expand Down

0 comments on commit 06bfc8b

Please sign in to comment.