Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cancellable Progress #217

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions Zip/QuickZip.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,42 @@ extension Zip {
return try quickUnzipFile(path, progress: nil)
}

/**
Quick unzip a file. Unzips to a new folder inside the app's documents folder with the zip file's name.

- parameter path: Path of zipped file. NSURL.
- parameter progress: A Cancellable progress

- throws: Error if unzipping fails or if file is not found. Can be printed with a description variable.

- notes: Supports implicit progress composition

- returns: NSURL of the destination folder.
*/
public class func quickUnzipFile(_ path: URL, progress: Progress?) throws -> URL {
let fileManager = FileManager.default

let fileExtension = path.pathExtension
let fileName = path.lastPathComponent

let directoryName = fileName.replacingOccurrences(of: ".\(fileExtension)", with: "")

#if os(Linux)
// urls(for:in:) is not yet implemented on Linux
// See https://github.com/apple/swift-corelibs-foundation/blob/swift-4.2-branch/Foundation/FileManager.swift#L125
let documentsUrl = fileManager.temporaryDirectory
#else
let documentsUrl = fileManager.urls(for: self.searchPathDirectory(), in: .userDomainMask)[0]
#endif
do {
let destinationUrl = documentsUrl.appendingPathComponent(directoryName, isDirectory: true)
try self.unzipFile(path, destination: destinationUrl, overwrite: true, password: nil, progress: progress)
return destinationUrl
}catch{
throw(ZipError.unzipFail)
}
}

/**
Quick unzip a file. Unzips to a new folder inside the app's documents folder with the zip file's name.

Expand All @@ -52,7 +88,7 @@ extension Zip {

- returns: NSURL of the destination folder.
*/
public class func quickUnzipFile(_ path: URL, progress: ((_ progress: Double) -> ())?) throws -> URL {
public class func quickUnzipFile(_ path: URL, progressCallback: ((_ progress: Double) -> ())?) throws -> URL {
let fileManager = FileManager.default

let fileExtension = path.pathExtension
Expand All @@ -69,7 +105,7 @@ extension Zip {
#endif
do {
let destinationUrl = documentsUrl.appendingPathComponent(directoryName, isDirectory: true)
try self.unzipFile(path, destination: destinationUrl, overwrite: true, password: nil, progress: progress)
try self.unzipFile(path, destination: destinationUrl, overwrite: true, password: nil, progressCallback: progressCallback)
return destinationUrl
}catch{
throw(ZipError.unzipFail)
Expand Down
Loading