aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuard/ZipArchive
diff options
context:
space:
mode:
authorRoopesh Chander <roop@roopc.net>2018-10-29 01:04:46 +0530
committerRoopesh Chander <roop@roopc.net>2018-10-29 01:51:49 +0530
commitde3dde84fddf36869621bb57dcd243a1f203eaf1 (patch)
tree3425f06d175762abc2da64c397ca4c3e18470275 /WireGuard/WireGuard/ZipArchive
parentZip: Include minizip in our Xcode project (diff)
downloadwireguard-apple-de3dde84fddf36869621bb57dcd243a1f203eaf1.tar.xz
wireguard-apple-de3dde84fddf36869621bb57dcd243a1f203eaf1.zip
Zip: Add zip file reader
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to 'WireGuard/WireGuard/ZipArchive')
-rw-r--r--WireGuard/WireGuard/ZipArchive/ZipArchive.swift78
1 files changed, 78 insertions, 0 deletions
diff --git a/WireGuard/WireGuard/ZipArchive/ZipArchive.swift b/WireGuard/WireGuard/ZipArchive/ZipArchive.swift
new file mode 100644
index 00000000..ea6df174
--- /dev/null
+++ b/WireGuard/WireGuard/ZipArchive/ZipArchive.swift
@@ -0,0 +1,78 @@
+// SPDX-License-Identifier: MIT
+// Copyright © 2018 WireGuard LLC. All rights reserved.
+
+import Foundation
+
+enum ZipArchiveError: Error {
+ case cantOpenInputZipFile
+ case badArchive
+}
+
+class ZipArchive {
+
+ static func unarchive(url: URL, requiredFileExtensions: [String]) throws -> [(fileName: String, contents: Data)] {
+
+ var results: [(fileName: String, contents: Data)] = []
+
+ guard let zipFile = unzOpen64(url.path) else {
+ throw ZipArchiveError.cantOpenInputZipFile
+ }
+ defer {
+ unzClose(zipFile)
+ }
+ guard (unzGoToFirstFile(zipFile) == UNZ_OK) else {
+ throw ZipArchiveError.badArchive
+ }
+
+ var resultOfGoToNextFile: Int32
+ repeat {
+ guard (unzOpenCurrentFile(zipFile) == UNZ_OK) else {
+ throw ZipArchiveError.badArchive
+ }
+
+ let bufferSize = 1024
+ var fileNameBuffer = UnsafeMutablePointer<Int8>.allocate(capacity: bufferSize)
+ var dataBuffer = UnsafeMutablePointer<Int8>.allocate(capacity: bufferSize)
+
+ defer {
+ fileNameBuffer.deallocate()
+ dataBuffer.deallocate()
+ }
+
+ guard (unzGetCurrentFileInfo64(zipFile, nil, fileNameBuffer, UInt(bufferSize), nil, 0, nil, 0) == UNZ_OK) else {
+ throw ZipArchiveError.badArchive
+ }
+
+ let fileName = String(cString: fileNameBuffer)
+ let fileExtension = URL(string: fileName)?.pathExtension ?? ""
+
+ if (requiredFileExtensions.contains(fileExtension)) {
+ var unzippedData = Data()
+ var bytesRead: Int32 = 0
+ repeat {
+ bytesRead = unzReadCurrentFile(zipFile, dataBuffer, UInt32(bufferSize))
+ if (bytesRead > 0) {
+ let dataRead = dataBuffer.withMemoryRebound(to: UInt8.self, capacity: bufferSize) {
+ (buf: UnsafeMutablePointer<UInt8>) -> Data in
+ return Data(bytes: buf, count: Int(bytesRead))
+ }
+ unzippedData.append(dataRead)
+ }
+ } while (bytesRead > 0)
+ results.append((fileName: fileName, contents: unzippedData))
+ }
+
+ guard (unzCloseCurrentFile(zipFile) == UNZ_OK) else {
+ throw ZipArchiveError.badArchive
+ }
+
+ resultOfGoToNextFile = unzGoToNextFile(zipFile)
+ } while (resultOfGoToNextFile == UNZ_OK)
+
+ if (resultOfGoToNextFile == UNZ_END_OF_LIST_OF_FILE) {
+ return results
+ } else {
+ throw ZipArchiveError.badArchive
+ }
+ }
+}