From 8e282b606cc6d4b947ddb5dbb3e534fc42a643d8 Mon Sep 17 00:00:00 2001 From: peterboni Date: Sat, 25 Nov 2017 16:03:04 +0800 Subject: [PATCH] Added includeRootDirectory logic. Enables zipping of directory contents, i.e. not including root directory in zip. Equivalent to user cd'ing into directory and zipping. Functionality left as default, but logic is now there to enable above by either toggling boolean, or modifying other functions to enable toggling per call. Also, expandDirectoryFilePath function comment changed. It's not a recursive function :) --- Zip/ZipUtilities.swift | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/Zip/ZipUtilities.swift b/Zip/ZipUtilities.swift index 4759336..d2394b6 100644 --- a/Zip/ZipUtilities.swift +++ b/Zip/ZipUtilities.swift @@ -10,6 +10,27 @@ import Foundation internal class ZipUtilities { + /* + Include root directory. + Default is true. + + e.g. The Test directory contains two files A.txt and B.txt. + + As true: + $ zip -r Test.zip Test/ + $ unzip -l Test.zip + Test/ + Test/A.txt + Test/B.txt + + As false: + $ zip -r Test.zip Test/ + $ unzip -l Test.zip + A.txt + B.txt + */ + let includeRootDirectory = true + // File manager let fileManager = FileManager.default @@ -54,7 +75,7 @@ internal class ZipUtilities { /** - Recursive function to expand directory contents and parse them into ProcessedFilePath structs. + Expand directory contents and parse them into ProcessedFilePath structs. - parameter directory: Path of folder as NSURL. @@ -67,12 +88,15 @@ internal class ZipUtilities { while let filePathComponent = enumerator.nextObject() as? String { let path = directory.appendingPathComponent(filePathComponent) let filePath = path.path - let directoryName = directory.lastPathComponent var isDirectory: ObjCBool = false fileManager.fileExists(atPath: filePath, isDirectory: &isDirectory) if !isDirectory.boolValue { - let fileName = (directoryName as NSString).appendingPathComponent(filePathComponent) + var fileName = filePathComponent + if includeRootDirectory { + let directoryName = directory.lastPathComponent + fileName = (directoryName as NSString).appendingPathComponent(filePathComponent) + } let processedPath = ProcessedFilePath(filePathURL: path, fileName: fileName) processedFilePaths.append(processedPath) } -- 2.26.2