Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compressing archive to save storage costs #66

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
71 changes: 71 additions & 0 deletions internal/tar/file_utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package tar

import (
"context"
"os"
"path/filepath"
"testing"
)

func TestTaritAndUntar(t *testing.T) {
// Create a temporary directory for testing
tempDir, err := os.MkdirTemp("", "tarit_test")
if err != nil {
t.Fatalf("Failed to create temp directory: %v", err)
}
defer os.RemoveAll(tempDir)

// Create a test file structure
testDir := filepath.Join(tempDir, "foo-archive")
err = os.Mkdir(testDir, 0o755)
if err != nil {
t.Fatalf("Failed to create test directory: %v", err)
}

testFile := filepath.Join(testDir, "test_file.txt")
err = os.WriteFile(testFile, []byte("Test content"), 0o644)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}

// Test Tarit
tarFile := filepath.Join(tempDir, "test.tar.gz")
err = Tarit(testDir, tarFile)
if err != nil {
t.Fatalf("Tarit failed: %v", err)
}

// Check if the tar file was created
if _, err := os.Stat(tarFile); os.IsNotExist(err) {
t.Fatalf("Tar file was not created")
}

// Test Untar
extractDir := filepath.Join(tempDir, "bar-archive-extracted")
err = os.Mkdir(extractDir, 0o755)
if err != nil {
t.Fatalf("Failed to create extraction directory: %v", err)
}

tarFileHandle, err := os.Open(tarFile)
if err != nil {
t.Fatalf("Failed to open tar file: %v", err)
}
defer tarFileHandle.Close()

err = Untar(context.Background(), tarFileHandle, extractDir)
if err != nil {
t.Fatalf("Untar failed: %v", err)
}

// Check if the extracted file exists and has the correct content
extractedFile := filepath.Join(extractDir, "test_file.txt")
content, err := os.ReadFile(extractedFile)
if err != nil {
t.Fatalf("Failed to read extracted file: %v", err)
}

if string(content) != "Test content" {
t.Errorf("Extracted file content mismatch. Expected 'Test content', got '%s'", string(content))
}
}