diff --git a/README.md b/README.md index 0ffd5b1..9100616 100644 --- a/README.md +++ b/README.md @@ -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)) ``` diff --git a/cli_test.go b/cli_test.go index 0a31cc8..3a37b17 100644 --- a/cli_test.go +++ b/cli_test.go @@ -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() diff --git a/extractor.go b/extractor.go new file mode 100644 index 0000000..117af27 --- /dev/null +++ b/extractor.go @@ -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()) +} diff --git a/extractor_test.go b/extractor_test.go new file mode 100644 index 0000000..78d2ff4 --- /dev/null +++ b/extractor_test.go @@ -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) + }) +} diff --git a/internal/testutils/archiver.go b/internal/testutils/archiver.go index 3d7d05f..afcc76b 100644 --- a/internal/testutils/archiver.go +++ b/internal/testutils/archiver.go @@ -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 } diff --git a/testdata/test.zip b/testdata/test.zip new file mode 100644 index 0000000..421ef39 Binary files /dev/null and b/testdata/test.zip differ