Skip to content

Commit

Permalink
Fix inconsistent behaviour between macOS and Linux (#8)
Browse files Browse the repository at this point in the history
* Fix inconsistent behaviour between macOS and Linux

* Check for `.zip` extension in quick functions
  • Loading branch information
fpseverino authored Aug 29, 2024
1 parent 2d2e186 commit 567d515
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
26 changes: 10 additions & 16 deletions Sources/Zip/QuickZip.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 destinationUrl = FileManager.default.temporaryDirectory
.appendingPathComponent(path.deletingPathExtension().lastPathComponent, isDirectory: true)
try self.unzipFile(path, destination: destinationUrl, progress: progress)
return destinationUrl
}
Expand Down Expand Up @@ -90,8 +78,14 @@ 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")
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
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/ZipTests/ZipTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 567d515

Please sign in to comment.