Skip to content

Commit

Permalink
fix: compressor outputs incomplete data
Browse files Browse the repository at this point in the history
  • Loading branch information
jm33-m0 committed Dec 20, 2024
1 parent 40d3478 commit fa5b492
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 28 deletions.
34 changes: 26 additions & 8 deletions cmd/compressor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,52 @@ import (
)

func main() {
file := flag.String("f", "", "Compressed file to decompress")
to_compress := flag.String("c", "", "File to compress")
to_decompress := flag.String("f", "", "Compressed file to decompress")
output := flag.String("o", "", "Output file")
compressionType := flag.String("t", "xz", "Compression type (e.g., bz2, gz, xz, zst, lz4, br)")
flag.Parse()

if *file == "" || *output == "" || *compressionType == "" {
if *output == "" || *compressionType == "" {
flag.Usage()
return
}

file := *to_decompress
if file == "" {
file = *to_compress
}
if file == "" {
log.Fatalf("No file to compress or decompress")
}

compression, ok := arc.CompressionMap[strings.ToLower(*compressionType)]
if !ok {
log.Fatalf("Unsupported compression type: %s", *compressionType)
}

data, err := os.ReadFile(*file)
data, err := os.ReadFile(file)
if err != nil {
log.Fatalf("Error reading file %s: %v", *file, err)
log.Fatalf("Error reading file %s: %v", file, err)
}

out, err := arc.Decompress(data, compression)
if err != nil {
log.Fatalf("Error decompressing file %s: %v", *file, err)
var out []byte
if *to_decompress != "" {
out, err = arc.Decompress(data, compression)
if err != nil {
log.Fatalf("Error decompressing file %s: %v", file, err)
}
} else if *to_compress != "" {
// out, err = arc.Compress(data, compression)
out, err = arc.CompressXz(data)
if err != nil {
log.Fatalf("Error compressing file %s: %v", file, err)
}
}

if err := os.WriteFile(*output, out, 0o644); err != nil {
log.Fatalf("Error writing to file %s: %v", *output, err)
}

log.Printf("File decompressed successfully to %s", *output)
log.Printf("Success: %s", *output)
}
11 changes: 7 additions & 4 deletions compressor.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@ import (
func Compress(data []byte, compression archives.Compression) ([]byte, error) {
var compressedBuf bytes.Buffer

// Wrap the buffer with a BZ2 compressor
// Wrap the buffer with a compressor
compressor, err := compression.OpenWriter(&compressedBuf)
if err != nil {
return nil, fmt.Errorf("CompressBZ2: Failed to create BZ2 compressor: %w", err)
return nil, fmt.Errorf("Compress: Failed to create compressor: %w", err)
}
defer compressor.Close()

// Write data to the compressor
// Writes to compressor will be compressed
_, err = compressor.Write(data)
if err != nil {
return nil, fmt.Errorf("CompressBZ2: Write to compressor failed: %w", err)
return nil, fmt.Errorf("Compress: Write to compressor failed: %w", err)
}

// without this line, the compressed data will be incomplete
compressor.Close()

return compressedBuf.Bytes(), nil
}

Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/jm33-m0/arc

go 1.23.3

require github.com/mholt/archives v0.0.0-20241123043835-f28756b8d6c7
require github.com/mholt/archives v0.0.0-20241216060121-23e0af8fe73d

require (
github.com/STARRY-S/zip v0.2.1 // indirect
Expand All @@ -16,11 +16,11 @@ require (
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/klauspost/pgzip v1.2.6 // indirect
github.com/nwaples/rardecode/v2 v2.0.0-beta.4.0.20241112120701-034e449c6e78 // indirect
github.com/pierrec/lz4/v4 v4.1.21 // indirect
github.com/nwaples/rardecode/v2 v2.0.1 // indirect
github.com/pierrec/lz4/v4 v4.1.22 // indirect
github.com/sorairolake/lzip-go v0.3.5 // indirect
github.com/therootcompany/xz v1.0.1 // indirect
github.com/ulikunitz/xz v0.5.12 // indirect
go4.org v0.0.0-20230225012048-214862532bf5 // indirect
golang.org/x/text v0.20.0 // indirect
golang.org/x/text v0.21.0 // indirect
)
22 changes: 10 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,12 @@ github.com/klauspost/pgzip v1.2.6/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQ
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mholt/archives v0.0.0-20241121231447-a7daedac0a9e h1:KfA9A3mnQZc4jKS+EsCQ57kt94GL8/hlFh23lGE4tE0=
github.com/mholt/archives v0.0.0-20241121231447-a7daedac0a9e/go.mod h1:j/Ire/jm42GN7h90F5kzj6hf6ZFzEH66de+hmjEKu+I=
github.com/mholt/archives v0.0.0-20241123043835-f28756b8d6c7 h1:LwwI6JwAZPERpPkEUTbiPcCzPP8u1my9wq8tjWEsagQ=
github.com/mholt/archives v0.0.0-20241123043835-f28756b8d6c7/go.mod h1:j/Ire/jm42GN7h90F5kzj6hf6ZFzEH66de+hmjEKu+I=
github.com/nwaples/rardecode/v2 v2.0.0-beta.4.0.20241112120701-034e449c6e78 h1:MYzLheyVx1tJVDqfu3YnN4jtnyALNzLvwl+f58TcvQY=
github.com/nwaples/rardecode/v2 v2.0.0-beta.4.0.20241112120701-034e449c6e78/go.mod h1:yntwv/HfMc/Hbvtq9I19D1n58te3h6KsqCf3GxyfBGY=
github.com/pierrec/lz4/v4 v4.1.21 h1:yOVMLb6qSIDP67pl/5F7RepeKYu/VmTyEXvuMI5d9mQ=
github.com/pierrec/lz4/v4 v4.1.21/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
github.com/mholt/archives v0.0.0-20241216060121-23e0af8fe73d h1:Vw3f39TqFSQLA+OyW+8SouppHTYzX8/fDv6Ao8uj3Ho=
github.com/mholt/archives v0.0.0-20241216060121-23e0af8fe73d/go.mod h1:j/Ire/jm42GN7h90F5kzj6hf6ZFzEH66de+hmjEKu+I=
github.com/nwaples/rardecode/v2 v2.0.1 h1:3MN6/R+Y4c7e+21U3yhWuUcf72sYmcmr6jtiuAVSH1A=
github.com/nwaples/rardecode/v2 v2.0.1/go.mod h1:yntwv/HfMc/Hbvtq9I19D1n58te3h6KsqCf3GxyfBGY=
github.com/pierrec/lz4/v4 v4.1.22 h1:cKFw6uJDK+/gfw5BcDL0JL5aBsAFdsIT18eRtLj7VIU=
github.com/pierrec/lz4/v4 v4.1.22/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
Expand Down Expand Up @@ -187,8 +185,8 @@ golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ=
golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down Expand Up @@ -216,8 +214,8 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down

0 comments on commit fa5b492

Please sign in to comment.