Skip to content

Commit

Permalink
Compressing archive to save storage costs
Browse files Browse the repository at this point in the history
Enable compressing to save storage costs. Archive is compressed
by gzip. If in future zstd is supported natively by Go, we can
move to that.
  • Loading branch information
khrm committed Oct 4, 2024
1 parent 58c716d commit a3c50fe
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions internal/tar/file-utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package tar

import (
"archive/tar"
"bytes"
"compress/gzip"
"context"
"io"
"log"
"os"
"path/filepath"
"strings"
Expand All @@ -12,16 +15,19 @@ import (
)

func Tarit(source, target string) error {
var buf bytes.Buffer

// write the .tar.gzip
tarfile, err := os.Create(target)
if err != nil {
return err
}
defer tarfile.Close()

tarball := tar.NewWriter(tarfile)
defer tarball.Close()
gz := gzip.NewWriter(&buf)
tarball := tar.NewWriter(gz)

return filepath.Walk(source,
err = filepath.Walk(source,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
Expand Down Expand Up @@ -49,6 +55,18 @@ func Tarit(source, target string) error {
_, err = io.Copy(tarball, file)
return err
})
if err != nil {
return err
}

gz.Close()
tarball.Close()

if _, err := io.Copy(tarfile, &buf); err != nil {
log.Println("error copying tarfile", err)
return err
}
return nil
}

func Untar(ctx context.Context, file *os.File, target string) error {
Expand All @@ -57,5 +75,5 @@ func Untar(ctx context.Context, file *os.File, target string) error {
return err
}
defer f.Close()
return extract.Archive(ctx, f, target, nil)
return extract.Gz(ctx, f, target, nil)
}

0 comments on commit a3c50fe

Please sign in to comment.