mirror of
https://github.com/Awuqing/BackupX.git
synced 2026-06-05 09:49:37 +08:00
first commit
This commit is contained in:
60
server/pkg/compress/gzip.go
Normal file
60
server/pkg/compress/gzip.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package compress
|
||||
|
||||
import (
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GzipFile(sourcePath string) (string, error) {
|
||||
source, err := os.Open(sourcePath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("open source file: %w", err)
|
||||
}
|
||||
defer source.Close()
|
||||
targetPath := sourcePath + ".gz"
|
||||
target, err := os.Create(targetPath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("create gzip file: %w", err)
|
||||
}
|
||||
defer target.Close()
|
||||
writer := gzip.NewWriter(target)
|
||||
writer.Name = filepath.Base(sourcePath)
|
||||
if _, err := io.Copy(writer, source); err != nil {
|
||||
writer.Close()
|
||||
return "", fmt.Errorf("gzip source file: %w", err)
|
||||
}
|
||||
if err := writer.Close(); err != nil {
|
||||
return "", fmt.Errorf("close gzip writer: %w", err)
|
||||
}
|
||||
return targetPath, nil
|
||||
}
|
||||
|
||||
func GunzipFile(sourcePath string) (string, error) {
|
||||
source, err := os.Open(sourcePath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("open gzip file: %w", err)
|
||||
}
|
||||
defer source.Close()
|
||||
reader, err := gzip.NewReader(source)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("create gzip reader: %w", err)
|
||||
}
|
||||
defer reader.Close()
|
||||
targetPath := strings.TrimSuffix(sourcePath, ".gz")
|
||||
if targetPath == sourcePath {
|
||||
targetPath += ".out"
|
||||
}
|
||||
target, err := os.Create(targetPath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("create target file: %w", err)
|
||||
}
|
||||
defer target.Close()
|
||||
if _, err := io.Copy(target, reader); err != nil {
|
||||
return "", fmt.Errorf("gunzip file: %w", err)
|
||||
}
|
||||
return targetPath, nil
|
||||
}
|
||||
29
server/pkg/compress/gzip_test.go
Normal file
29
server/pkg/compress/gzip_test.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package compress
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGzipAndGunzipFile(t *testing.T) {
|
||||
sourcePath := filepath.Join(t.TempDir(), "payload.txt")
|
||||
if err := os.WriteFile(sourcePath, []byte("payload"), 0o644); err != nil {
|
||||
t.Fatalf("WriteFile returned error: %v", err)
|
||||
}
|
||||
compressedPath, err := GzipFile(sourcePath)
|
||||
if err != nil {
|
||||
t.Fatalf("GzipFile returned error: %v", err)
|
||||
}
|
||||
decompressedPath, err := GunzipFile(compressedPath)
|
||||
if err != nil {
|
||||
t.Fatalf("GunzipFile returned error: %v", err)
|
||||
}
|
||||
content, err := os.ReadFile(decompressedPath)
|
||||
if err != nil {
|
||||
t.Fatalf("ReadFile returned error: %v", err)
|
||||
}
|
||||
if string(content) != "payload" {
|
||||
t.Fatalf("unexpected decompressed content: %s", string(content))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user