Skip to content

Commit

Permalink
creates directory with single file
Browse files Browse the repository at this point in the history
  • Loading branch information
ybirader committed Sep 21, 2023
1 parent c2f561e commit c131596
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ The concurrency of the archiver can be configured using the corresponding flag:
pzip --concurrency 2 /path/to/compressed.zip path/to/file_or_directory1 path/to/file_or_directory2 ... path/to/file_or_directoryN
```
or by using passing the `Concurrency` option:
or by passing the `Concurrency` option:
```go
archiver, err := pzip.NewArchiver(archive, Concurrency(2))
```
Expand Down
3 changes: 2 additions & 1 deletion cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func TestCLI(t *testing.T) {
defer os.RemoveAll(archivePath)

cli := pzip.CLI{archivePath, files, runtime.GOMAXPROCS(0)}
cli.Archive(context.Background())
err := cli.Archive(context.Background())
assert.NoError(t, err)

archiveReader := testutils.GetArchiveReader(t, archivePath)
defer archiveReader.Close()
Expand Down
25 changes: 25 additions & 0 deletions extractor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package pzip

import (
"os"
"path/filepath"

"github.com/klauspost/compress/zip"
)

type Extractor struct {
outputDir string
}

func NewExtractor(outputDir string) *Extractor {
return &Extractor{outputDir: outputDir}
}

func (e *Extractor) Extract(archivePath string) {
archiveReader, _ := zip.OpenReader(archivePath)
defer archiveReader.Close()

file := archiveReader.File[0]

os.Mkdir(filepath.Join(e.outputDir, file.Name), file.Mode())
}
28 changes: 28 additions & 0 deletions extractor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package pzip

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

"github.com/alecthomas/assert/v2"
)

const (
testArchiveFixture = testdataRoot + "test.zip" // test.zip fixture is an archive of the helloDirectory fixture
outputDirPath = testdataRoot + "test"
)

func TestExtract(t *testing.T) {
t.Run("writes output directory with correct name", func(t *testing.T) {
err := os.Mkdir(outputDirPath, 0755)
assert.NoError(t, err)
defer os.RemoveAll(outputDirPath)

extractor := NewExtractor(outputDirPath)
extractor.Extract(testArchiveFixture)

_, err = os.ReadDir(filepath.Join(outputDirPath, "hello"))
assert.NoError(t, err)
})
}
2 changes: 1 addition & 1 deletion internal/testutils/archiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func GetFileInfo(t testing.TB, name string) fs.FileInfo {
t.Helper()

info, err := os.Stat(name)
assert.NoError(t, err, fmt.Sprintf("could not get file into fot %s", name))
assert.NoError(t, err, fmt.Sprintf("could not get file info for %s", name))

return info
}
Expand Down
Binary file added testdata/test.zip
Binary file not shown.

0 comments on commit c131596

Please sign in to comment.