Skip to content

Commit

Permalink
Merge branch 'main' into dn2_more2
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnewhall committed Jul 1, 2024
2 parents 5239405 + 62ee168 commit 5875b1c
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 10 deletions.
41 changes: 31 additions & 10 deletions files.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (
"strings"
)

// ArchiveList is the value returned when searchying for compressed files.
// The map is directory to list of archives in that directory.
type ArchiveList map[string][]string

type archive struct {
// Extension is passed to strings.HasSuffix.
Extension string
Expand Down Expand Up @@ -187,11 +191,11 @@ func (e Exclude) Has(test string) bool {
// so if the rar archive does not have "part" followed by a number in the name, then it will be
// considered an independent archive. Some packagers seem to use different naming schemes,
// so this may need to be updated as time progresses. Use the input to Filter to adjust the output.
func FindCompressedFiles(filter Filter) map[string][]string {
func FindCompressedFiles(filter Filter) ArchiveList {
return findCompressedFiles(filter.Path, &filter, 0)
}

func findCompressedFiles(path string, filter *Filter, depth int) map[string][]string {
func findCompressedFiles(path string, filter *Filter, depth int) ArchiveList {
if filter.MaxDepth > 0 && filter.MaxDepth < depth {
return nil
}
Expand All @@ -205,7 +209,7 @@ func findCompressedFiles(path string, filter *Filter, depth int) map[string][]st
if info, err := dir.Stat(); err != nil {
return nil // unreadable folder?
} else if !info.IsDir() && isArchiveFile(path) {
return map[string][]string{path: {path}} // passed in an archive file; send it back out.
return ArchiveList{path: {path}} // passed in an archive file; send it back out.
}

fileList, err := dir.Readdir(-1)
Expand Down Expand Up @@ -243,13 +247,8 @@ func checkR00ForRarFile(fileList []os.FileInfo, r00file string) bool {

// getCompressedFiles checks file suffixes to find archives to decompress.
// This pays special attention to the widely accepted variance of rar formats.
func getCompressedFiles( //nolint:cyclop
path string,
filter *Filter,
fileList []os.FileInfo,
depth int,
) map[string][]string {
files := map[string][]string{}
func getCompressedFiles(path string, filter *Filter, fileList []os.FileInfo, depth int) ArchiveList { //nolint:cyclop
files := ArchiveList{}

for _, file := range fileList {
switch lowerName := strings.ToLower(file.Name()); {
Expand Down Expand Up @@ -459,3 +458,25 @@ func AllExcept(onlyThese ...string) Exclude {

return output
}

// Count returns the number of unique archives in the archive list.
func (a ArchiveList) Count() int {
var count int

for _, files := range a {
count += len(files)
}

return count
}

// Random returns a random file listing from the archive list.
// If the list only contains one directory, then that is the one returned.
// If the archive list is empty or nil, returns nil.
func (a ArchiveList) Random() []string {
for _, files := range a {
return files
}

return nil
}
66 changes: 66 additions & 0 deletions zip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package xtractr_test

import (
"archive/zip"
_ "embed"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golift.io/xtractr"
)

func TestZip(t *testing.T) {
t.Parallel()
const (
testDataSize = 21
testFileCount = 5
testArchiveCount = 1
)
testFiles := []string{
"README.txt",
"subdir/",
"subdir/subdirfile.txt",
"subdir/level2/",
"subdir/level2/level2file.txt",
}

name, err := os.MkdirTemp(".", "xtractr_test_*_data")
require.NoError(t, err, "creating temp directory failed")
defer os.RemoveAll(name)

zipFile, err := os.Create(filepath.Join(name, "archive.zip"))
require.NoError(t, err)
zipWriter := zip.NewWriter(zipFile)

for _, file := range testFiles {
if file[len(file)-1] == '/' {
_, err = zipWriter.Create(file)
require.NoError(t, err)
} else {
f, err := zipWriter.Create(file)
require.NoError(t, err)
_, err = f.Write([]byte("content"))
require.NoError(t, err)
}
}
err = zipWriter.Close()
require.NoError(t, err)
err = zipFile.Close()
require.NoError(t, err)

zipTestFile := filepath.Join(name, "archive.zip")

size, files, archives, err := xtractr.ExtractFile(&xtractr.XFile{
FilePath: zipTestFile,
OutputDir: filepath.Clean(name),
FileMode: 0o600,
DirMode: 0o700,
})
require.NoError(t, err)
assert.Equal(t, int64(testDataSize), size)
assert.Len(t, files, testFileCount)
assert.Len(t, archives, testArchiveCount)
}

0 comments on commit 5875b1c

Please sign in to comment.