Skip to content

Commit 8bb5a13

Browse files
committed
make preserve paths work with output dir
1 parent 59f219b commit 8bb5a13

File tree

1 file changed

+43
-29
lines changed

1 file changed

+43
-29
lines changed

pkg/xt/xt.go

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,49 +22,63 @@ func Extract(job *Job) {
2222

2323
total := 0
2424
count := 0
25+
size := int64(0)
26+
fCount := 0
27+
start := time.Now()
2528

2629
for _, files := range archives {
2730
total += len(files)
2831
}
2932

30-
for _, files := range archives {
31-
for _, fileName := range files {
32-
count++
33-
log.Printf("==> Extracting Archive (%d/%d): %s", count, total, fileName)
34-
35-
file := &xtractr.XFile{
36-
FilePath: fileName, // Path to archive being extracted.
37-
OutputDir: job.Output, // Folder to extract archive into.
38-
FileMode: job.FileMode.Mode(), // Write files with this mode.
39-
DirMode: job.DirMode.Mode(), // Write folders with this mode.
40-
Passwords: job.Passwords, // (RAR/7zip) Archive password(s).
41-
SquashRoot: job.SquashRoot, // Remove single root folder?
42-
}
43-
44-
file.SetLogger(job)
45-
46-
start := time.Now()
33+
for folder, files := range archives {
34+
count++
35+
for _, archiveName := range files {
36+
log.Printf("==> Extracting Archive (%d/%d): %s", count, total, archiveName)
4737

48-
// If preserving the file hierarchy, set the output directory to the
49-
// folder of the archive being extracted.
50-
if job.Preserve {
51-
file.OutputDir = filepath.Dir(fileName)
52-
}
53-
54-
size, files, _, err := xtractr.ExtractFile(file)
55-
if err != nil {
56-
log.Printf("[ERROR] Archive: %s: %v", fileName, err)
57-
continue
58-
}
38+
fSize, files, duration := job.processArchive(folder, archiveName)
39+
size += fSize
40+
fCount += len(files)
5941

6042
log.Printf("==> Extracted Archive %s in %v: bytes: %d, files: %d",
61-
fileName, time.Since(start).Round(time.Millisecond), size, len(files))
43+
archiveName, duration.Round(time.Millisecond), size, len(files))
6244

6345
if len(files) > 0 {
6446
log.Printf("==> Files:\n - %s", strings.Join(files, "\n - "))
6547
}
6648
}
6749
}
50+
51+
log.Printf("==> Done.\n==> Extracted %d archives; wrote %d files totalling %d bytes in %v",
52+
total, fCount, size, time.Since(start).Round(time.Millisecond))
53+
}
54+
55+
func (j *Job) processArchive(folder, archiveName string) (int64, []string, time.Duration) {
56+
file := &xtractr.XFile{
57+
FilePath: archiveName, // Path to archive being extracted.
58+
OutputDir: j.Output, // Folder to extract archive into.
59+
FileMode: j.FileMode.Mode(), // Write files with this mode.
60+
DirMode: j.DirMode.Mode(), // Write folders with this mode.
61+
Passwords: j.Passwords, // (RAR/7zip) Archive password(s).
62+
SquashRoot: j.SquashRoot, // Remove single root folder?
63+
}
64+
file.SetLogger(j)
65+
66+
// If preserving the file hierarchy: set the output directory to the same path as the input file.
67+
if j.Preserve {
68+
// Remove input path prefix from fileName,
69+
// append fileName.Dir to job.Output,
70+
// extract file into job.Output/file(sub)Folder(s).
71+
file.OutputDir = filepath.Join(j.Output, filepath.Dir(strings.TrimPrefix(folder, archiveName)))
72+
}
73+
74+
start := time.Now()
75+
76+
size, files, _, err := xtractr.ExtractFile(file)
77+
if err != nil {
78+
log.Printf("[ERROR] Archive: %s: %v", archiveName, err)
79+
}
80+
81+
return size, files, time.Since(start)
6882
}
6983

7084
func (j *Job) getArchives() map[string][]string {

0 commit comments

Comments
 (0)