use SSZipArchive (#28)

use SSZipArchive
This commit is contained in:
Michele Balistreri 2022-07-04 14:39:28 +02:00 committed by GitHub
parent 874575436e
commit 17381678bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 11 deletions

View File

@ -1,6 +1,6 @@
Pod::Spec.new do |spec| Pod::Spec.new do |spec|
spec.name = 'Keycard' spec.name = 'Keycard'
spec.version = '3.0.5' spec.version = '3.0.6'
spec.authors = {'Bitgamma' => 'opensource@bitgamma.com'} spec.authors = {'Bitgamma' => 'opensource@bitgamma.com'}
spec.homepage = 'https://github.com/status-im/Keycard.swift' spec.homepage = 'https://github.com/status-im/Keycard.swift'
spec.license = { :type => 'Apache' } spec.license = { :type => 'Apache' }
@ -12,5 +12,5 @@ Pod::Spec.new do |spec|
spec.frameworks = 'CoreNFC' spec.frameworks = 'CoreNFC'
spec.dependency 'CryptoSwift' spec.dependency 'CryptoSwift'
spec.dependency 'secp256k1' spec.dependency 'secp256k1'
spec.dependency 'Zip' spec.dependency 'SSZipArchive'
end end

View File

@ -20,12 +20,12 @@
} }
}, },
{ {
"package": "Zip", "package": "ZipArchive",
"repositoryURL": "https://github.com/marmelroy/Zip.git", "repositoryURL": "https://github.com/ZipArchive/ZipArchive.git",
"state": { "state": {
"branch": null, "branch": null,
"revision": "67fa55813b9e7b3b9acee9c0ae501def28746d76", "revision": "38e0ce0598e06b034271f296a8e15b149c91aa19",
"version": "2.1.2" "version": "2.4.3"
} }
} }
] ]

View File

@ -17,14 +17,14 @@ let package = Package(
dependencies: [ dependencies: [
.package(url: "https://github.com/status-im/secp256k1.swift.git", .branch("master")), .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/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: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite. // 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. // Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target( .target(
name: "Keycard", name: "Keycard",
dependencies: ["secp256k1", "CryptoSwift", "Zip"]), dependencies: ["secp256k1", "CryptoSwift", "ZipArchive"]),
.testTarget( .testTarget(
name: "KeycardTests", name: "KeycardTests",
dependencies: ["Keycard"]), dependencies: ["Keycard"]),

View File

@ -1,5 +1,5 @@
import Foundation import Foundation
import Zip import SSZipArchive
struct FileLoader { struct FileLoader {
private static let blockSize = 247 // 255 - 8 bytes for MAC private static let blockSize = 247 // 255 - 8 bytes for MAC
@ -25,8 +25,7 @@ struct FileLoader {
} }
private static func unzipFileAndGetSubfilesURLs(fileURL: URL) throws -> [URL] { private static func unzipFileAndGetSubfilesURLs(fileURL: URL) throws -> [URL] {
Zip.addCustomFileExtension("cap") let unzipDirectory = Self.quickUnzipFile(fileURL)
let unzipDirectory = try Zip.quickUnzipFile(fileURL)
var files = [URL](repeating: unzipDirectory, count: fileNames.count) var files = [URL](repeating: unzipDirectory, count: fileNames.count)
if let enumerator = FileManager.default.enumerator(at: unzipDirectory, includingPropertiesForKeys: [.isRegularFileKey], options: [.skipsHiddenFiles, .skipsPackageDescendants]) { if let enumerator = FileManager.default.enumerator(at: unzipDirectory, includingPropertiesForKeys: [.isRegularFileKey], options: [.skipsHiddenFiles, .skipsPackageDescendants]) {
for case let fileURL as URL in enumerator { for case let fileURL as URL in enumerator {
@ -38,10 +37,23 @@ struct FileLoader {
// Remove files that were not presen in the CAP file // Remove files that were not presen in the CAP file
files = files.filter { $0 != unzipDirectory } files = files.filter { $0 != unzipDirectory }
} }
guard !files.isEmpty else { throw Error.fileIsEmpty } guard !files.isEmpty else { throw Error.fileIsEmpty }
return files 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] { private static func encodeFullLength(_ length: Int) throws -> [UInt8] {
if length < 0x80 { if length < 0x80 {
return [UInt8(length)] return [UInt8(length)]