From 17381678bb981c3c553bb7202a26e642cecb1817 Mon Sep 17 00:00:00 2001 From: Michele Balistreri Date: Mon, 4 Jul 2022 14:39:28 +0200 Subject: [PATCH] use SSZipArchive (#28) use SSZipArchive --- Keycard.podspec | 4 ++-- Package.resolved | 8 ++++---- Package.swift | 4 ++-- Sources/Keycard/FileLoader.swift | 18 +++++++++++++++--- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/Keycard.podspec b/Keycard.podspec index 6f006e3..aef8e2c 100644 --- a/Keycard.podspec +++ b/Keycard.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |spec| spec.name = 'Keycard' - spec.version = '3.0.5' + spec.version = '3.0.6' spec.authors = {'Bitgamma' => 'opensource@bitgamma.com'} spec.homepage = 'https://github.com/status-im/Keycard.swift' spec.license = { :type => 'Apache' } @@ -12,5 +12,5 @@ Pod::Spec.new do |spec| spec.frameworks = 'CoreNFC' spec.dependency 'CryptoSwift' spec.dependency 'secp256k1' - spec.dependency 'Zip' + spec.dependency 'SSZipArchive' end diff --git a/Package.resolved b/Package.resolved index 950acc6..49d9723 100644 --- a/Package.resolved +++ b/Package.resolved @@ -20,12 +20,12 @@ } }, { - "package": "Zip", - "repositoryURL": "https://github.com/marmelroy/Zip.git", + "package": "ZipArchive", + "repositoryURL": "https://github.com/ZipArchive/ZipArchive.git", "state": { "branch": null, - "revision": "67fa55813b9e7b3b9acee9c0ae501def28746d76", - "version": "2.1.2" + "revision": "38e0ce0598e06b034271f296a8e15b149c91aa19", + "version": "2.4.3" } } ] diff --git a/Package.swift b/Package.swift index e6ac0ff..d3fa8ce 100644 --- a/Package.swift +++ b/Package.swift @@ -17,14 +17,14 @@ let package = Package( dependencies: [ .package(url: "https://github.com/status-im/secp256k1.swift.git", .branch("master")), .package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", .upToNextMinor(from: "1.0.0")), - .package(url: "https://github.com/marmelroy/Zip.git", .upToNextMinor(from: "2.1.0")) + .package(url: "https://github.com/ZipArchive/ZipArchive.git", .upToNextMinor(from: "2.4.3")) ], targets: [ // Targets are the basic building blocks of a package. A target can define a module or a test suite. // Targets can depend on other targets in this package, and on products in packages which this package depends on. .target( name: "Keycard", - dependencies: ["secp256k1", "CryptoSwift", "Zip"]), + dependencies: ["secp256k1", "CryptoSwift", "ZipArchive"]), .testTarget( name: "KeycardTests", dependencies: ["Keycard"]), diff --git a/Sources/Keycard/FileLoader.swift b/Sources/Keycard/FileLoader.swift index dc4e46b..4258fb6 100644 --- a/Sources/Keycard/FileLoader.swift +++ b/Sources/Keycard/FileLoader.swift @@ -1,5 +1,5 @@ import Foundation -import Zip +import SSZipArchive struct FileLoader { private static let blockSize = 247 // 255 - 8 bytes for MAC @@ -25,8 +25,7 @@ struct FileLoader { } private static func unzipFileAndGetSubfilesURLs(fileURL: URL) throws -> [URL] { - Zip.addCustomFileExtension("cap") - let unzipDirectory = try Zip.quickUnzipFile(fileURL) + let unzipDirectory = Self.quickUnzipFile(fileURL) var files = [URL](repeating: unzipDirectory, count: fileNames.count) if let enumerator = FileManager.default.enumerator(at: unzipDirectory, includingPropertiesForKeys: [.isRegularFileKey], options: [.skipsHiddenFiles, .skipsPackageDescendants]) { for case let fileURL as URL in enumerator { @@ -38,10 +37,23 @@ struct FileLoader { // Remove files that were not presen in the CAP file files = files.filter { $0 != unzipDirectory } } + guard !files.isEmpty else { throw Error.fileIsEmpty } return files } + private static func quickUnzipFile(_ path: URL) -> URL { + let fileExtension = path.pathExtension + let fileName = path.lastPathComponent + let directoryName = fileName.replacingOccurrences(of: ".\(fileExtension)", with: "") + let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] + + let destinationUrl = documentsUrl.appendingPathComponent(directoryName, isDirectory: true) + SSZipArchive.unzipFile(atPath: path.path, toDestination: destinationUrl.path) + + return destinationUrl + } + private static func encodeFullLength(_ length: Int) throws -> [UInt8] { if length < 0x80 { return [UInt8(length)]