Skip to content

Commit

Permalink
decompress files
Browse files Browse the repository at this point in the history
  • Loading branch information
ybirader committed Sep 21, 2023
1 parent abda90f commit 035fd19
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
8 changes: 7 additions & 1 deletion extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pzip

import (
"errors"
"io"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -34,10 +35,15 @@ func (e *Extractor) Extract(archivePath string) (err error) {
return derrors.Errorf("ERROR: could not create directory %s: %v", e.relativeToOutputDir(file.Name), err)
}
} else {
_, err = os.Create(e.relativeToOutputDir(file.Name))
outputFile, err := os.Create(e.relativeToOutputDir(file.Name))
if err != nil {
return derrors.Errorf("ERROR: could not create file %s: %v", e.relativeToOutputDir(file.Name), err)
}
defer outputFile.Close()

fileContent, _ := file.Open()
defer fileContent.Close()
io.Copy(outputFile, fileContent)
}
}

Expand Down
6 changes: 5 additions & 1 deletion extractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (
)

func TestExtract(t *testing.T) {
t.Run("writes empty archive files to output directory", func(t *testing.T) {
t.Run("writes decompressed archive files to output directory", func(t *testing.T) {
err := os.Mkdir(outputDirPath, 0755)
assert.NoError(t, err)
defer os.RemoveAll(outputDirPath)
Expand All @@ -31,6 +31,10 @@ func TestExtract(t *testing.T) {
assertDirContains(t, files, "hello.txt")
assertDirContains(t, files, "hello.txt")

info, err := os.Lstat(filepath.Join(outputDirPath, "hello", "hello.txt"))
assert.NoError(t, err)
assert.NotZero(t, info.Size())

files, err = os.ReadDir(filepath.Join(outputDirPath, "hello", "nested"))
assert.NoError(t, err)
assert.Equal(t, 1, len(files))
Expand Down

0 comments on commit 035fd19

Please sign in to comment.