Repository navigation
8000
More file actions
Expand file tree
/
Copy pathZipUtilities.swift
More file actions
108 lines (89 loc) · 3.18 KB
/
Copy pathZipUtilities.swift
File metadata and controls
108 lines (89 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//
// ZipUtilities.swift
// Zip
//
// Created by Roy Marmelstein on 26/01/2016.
// Copyright © 2016 Roy Marmelstein. All rights reserved.
//
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
/**
* ProcessedFilePath struct
*/
internal struct ProcessedFilePath {
let filePathURL: URL
let fileName: String?
func filePath() -> String {
return filePathURL.path
}
}
//MARK: Path processing
/**
Process zip paths
- parameter paths: Paths as NSURL.
- returns: Array of ProcessedFilePath structs.
*/
internal func processZipPaths(_ paths: [URL]) -> [ProcessedFilePath]{
var processedFilePaths = [ProcessedFilePath]()
for path in paths {
let filePath = path.path
var isDirectory: ObjCBool = false
_ = fileManager.fileExists(atPath: filePath, isDirectory: &isDirectory)
if !isDirectory.boolValue {
let processedPath = ProcessedFilePath(filePathURL: path, fileName: path.lastPathComponent)
processedFilePaths.append(processedPath)
}
else {
let directoryContents = expandDirectoryFilePath(path)
processedFilePaths.append(contentsOf: directoryContents)
}
}
return processedFilePaths
}
/**
Expand directory contents and parse them into ProcessedFilePath structs.
- parameter directory: Path of folder as NSURL.
- returns: Array of ProcessedFilePath structs.
*/
internal func expandDirectoryFilePath(_ directory: URL) -> [ProcessedFilePath] {
var processedFilePaths = [ProcessedFilePath]()
let directoryPath = directory.path
if let enumerator = fileManager.enumerator(atPath: directoryPath) {
while let filePathComponent = enumerator.nextObject() as? String {
let path = directory.appendingPathComponent(filePathComponent)
let filePath = path.path
var isDirectory: ObjCBool = false
_ = fileManager.fileExists(atPath: filePath, isDirectory: &isDirectory)
if !isDirectory.boolValue {
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)
}
}
}
return processedFilePaths
}
}