From 30e38dcc8042854b6a2e7c92c7220126309a1cc2 Mon Sep 17 00:00:00 2001 From: Francesco Paolo Severino Date: Wed, 28 Aug 2024 12:56:30 +0200 Subject: [PATCH 1/2] Fix inconsistent behaviour between macOS and Linux --- Sources/Zip/QuickZip.swift | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/Sources/Zip/QuickZip.swift b/Sources/Zip/QuickZip.swift index eb0aad80..8bd793f4 100644 --- a/Sources/Zip/QuickZip.swift +++ b/Sources/Zip/QuickZip.swift @@ -9,15 +9,6 @@ import Foundation extension Zip { - // Get search path directory. For tvOS Documents directory doesn't exist. - fileprivate class var searchPathDirectory: FileManager.SearchPathDirectory { - #if os(tvOS) - .cachesDirectory - #else - .documentDirectory - #endif - } - /** Quickly unzips a file. @@ -49,11 +40,8 @@ extension Zip { - Returns: `URL` of the destination folder. */ public class func quickUnzipFile(_ path: URL, progress: ((_ progress: Double) -> ())?) throws -> URL { - let fileExtension = path.pathExtension - let fileName = path.lastPathComponent - let directoryName = fileName.replacingOccurrences(of: ".\(fileExtension)", with: "") - let documentsUrl = FileManager.default.urls(for: self.searchPathDirectory, in: .userDomainMask)[0] - let destinationUrl = documentsUrl.appendingPathComponent(directoryName, isDirectory: true) + let directoryName = path.lastPathComponent.replacingOccurrences(of: ".\(path.pathExtension)", with: "") + let destinationUrl = FileManager.default.temporaryDirectory.appendingPathComponent(directoryName, isDirectory: true) try self.unzipFile(path, destination: destinationUrl, progress: progress) return destinationUrl } @@ -90,8 +78,7 @@ extension Zip { - Returns: `URL` of the destination folder. */ public class func quickZipFiles(_ paths: [URL], fileName: String, progress: ((_ progress: Double) -> ())?) throws -> URL { - let documentsUrl = FileManager.default.urls(for: self.searchPathDirectory, in: .userDomainMask)[0] as URL - let destinationUrl = documentsUrl.appendingPathComponent("\(fileName).zip") + let destinationUrl = FileManager.default.temporaryDirectory.appendingPathComponent("\(fileName).zip") try self.zipFiles(paths: paths, zipFilePath: destinationUrl, progress: progress) return destinationUrl } From 3e45a3fdcc999f146fa049a5cf58e1232dec05dd Mon Sep 17 00:00:00 2001 From: Francesco Paolo Severino Date: Thu, 29 Aug 2024 13:18:07 +0200 Subject: [PATCH 2/2] Check for `.zip` extension in quick functions --- Sources/Zip/QuickZip.swift | 13 ++++++++++--- Tests/ZipTests/ZipTests.swift | 2 +- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/Sources/Zip/QuickZip.swift b/Sources/Zip/QuickZip.swift index 8bd793f4..3870a327 100644 --- a/Sources/Zip/QuickZip.swift +++ b/Sources/Zip/QuickZip.swift @@ -40,8 +40,8 @@ extension Zip { - Returns: `URL` of the destination folder. */ public class func quickUnzipFile(_ path: URL, progress: ((_ progress: Double) -> ())?) throws -> URL { - let directoryName = path.lastPathComponent.replacingOccurrences(of: ".\(path.pathExtension)", with: "") - let destinationUrl = FileManager.default.temporaryDirectory.appendingPathComponent(directoryName, isDirectory: true) + let destinationUrl = FileManager.default.temporaryDirectory + .appendingPathComponent(path.deletingPathExtension().lastPathComponent, isDirectory: true) try self.unzipFile(path, destination: destinationUrl, progress: progress) return destinationUrl } @@ -78,7 +78,14 @@ extension Zip { - Returns: `URL` of the destination folder. */ public class func quickZipFiles(_ paths: [URL], fileName: String, progress: ((_ progress: Double) -> ())?) throws -> URL { - let destinationUrl = FileManager.default.temporaryDirectory.appendingPathComponent("\(fileName).zip") + var fileNameWithExtension = fileName + if !fileName.hasSuffix(".zip") { + fileNameWithExtension += ".zip" + } + + print("fileNameWithExtension: \(fileNameWithExtension)") + + let destinationUrl = FileManager.default.temporaryDirectory.appendingPathComponent(fileNameWithExtension) try self.zipFiles(paths: paths, zipFilePath: destinationUrl, progress: progress) return destinationUrl } diff --git a/Tests/ZipTests/ZipTests.swift b/Tests/ZipTests/ZipTests.swift index e27cb4e2..60b4a90a 100644 --- a/Tests/ZipTests/ZipTests.swift +++ b/Tests/ZipTests/ZipTests.swift @@ -119,7 +119,7 @@ final class ZipTests: XCTestCase { func testQuickZip() throws { let imageURL1 = url(forResource: "3crBXeO", withExtension: "gif")! let imageURL2 = url(forResource: "kYkLkPf", withExtension: "gif")! - let destinationURL = try Zip.quickZipFiles([imageURL1, imageURL2], fileName: "archive") + let destinationURL = try Zip.quickZipFiles([imageURL1, imageURL2], fileName: "archive.zip") XCTAssertTrue(FileManager.default.fileExists(atPath: destinationURL.path)) try XCTAssertGreaterThan(Data(contentsOf: destinationURL).count, 0) addTeardownBlock {