Skip to content

Commit f0d114f

Browse files
authored
Merge pull request marmelroy#114 from peterboni/patch-2
Added includeRootDirectory logic.
2 parents c8cee3e + 8e282b6 commit f0d114f

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

Zip/ZipUtilities.swift

+27-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,27 @@ import Foundation
1010

1111
internal class ZipUtilities {
1212

13+
/*
14+
Include root directory.
15+
Default is true.
16+
17+
e.g. The Test directory contains two files A.txt and B.txt.
18+
19+
As true:
20+
$ zip -r Test.zip Test/
21+
$ unzip -l Test.zip
22+
Test/
23+
Test/A.txt
24+
Test/B.txt
25+
26+
As false:
27+
$ zip -r Test.zip Test/
28+
$ unzip -l Test.zip
29+
A.txt
30+
B.txt
31+
*/
32+
let includeRootDirectory = true
33+
1334
// File manager
1435
let fileManager = FileManager.default
1536

@@ -54,7 +75,7 @@ internal class ZipUtilities {
5475

5576

5677
/**
57-
Recursive function to expand directory contents and parse them into ProcessedFilePath structs.
78+
Expand directory contents and parse them into ProcessedFilePath structs.
5879

5980
- parameter directory: Path of folder as NSURL.
6081

@@ -67,12 +88,15 @@ internal class ZipUtilities {
6788
while let filePathComponent = enumerator.nextObject() as? String {
6889
let path = directory.appendingPathComponent(filePathComponent)
6990
let filePath = path.path
70-
let directoryName = directory.lastPathComponent
7191

7292
var isDirectory: ObjCBool = false
7393
fileManager.fileExists(atPath: filePath, isDirectory: &isDirectory)
7494
if !isDirectory.boolValue {
75-
let fileName = (directoryName as NSString).appendingPathComponent(filePathComponent)
95+
var fileName = filePathComponent
96+
if includeRootDirectory {
97+
let directoryName = directory.lastPathComponent
98+
fileName = (directoryName as NSString).appendingPathComponent(filePathComponent)
99+
}
76100
let processedPath = ProcessedFilePath(filePathURL: path, fileName: fileName)
77101
processedFilePaths.append(processedPath)
78102
}

0 commit comments

Comments
 (0)