使用 SSZipArchive 解压缩文件而不提取文件的内容
Unzip a file using SSZipArchive not extracting the contents of the file
我正在使用 SSZipArchive
提取文件的内容,该文件是我从 Internet 下载并保存在 Documents
中的。我下载的资源是zip文件,下载成功后会解压
@IBAction func downloadData(sender: UIButton)
{ if let myAudioDataFromUrl = NSData(contentsOfURL: audioUrl){
// after downloading your data you need to save it to your destination url
if myAudioDataFromUrl.writeToURL(destinationUrl, atomically: true) {
print("file saved")
printTimestamp()
let folderName = "Aspire Brochure"//audioUrl.lastPathComponent!.stringByReplacingOccurrencesOfString(".zip", withString: "")
let destination = documentsUrl.URLByAppendingPathComponent(folderName)
let fileManager = NSFileManager.defaultManager()
print("This is the folderName \(destinationUrl)")
print("This is the destination \(destination)")
let success = fileManager.fileExistsAtPath(destination.absoluteString) as Bool
if success == false {
do {
print("it is here")
try! fileManager.createDirectoryAtPath(destination.absoluteString.stringByReplacingOccurrencesOfString("file://", withString: ""), withIntermediateDirectories: true, attributes: nil)
print("it hascome here ")
}
}
// let success = fileManager.fileExistsAtPath(destPath) as Bool
print("trying to unzip the file")
//let fileManager = NSFileManager.defaultManager()
let checkFile = NSFileManager.defaultManager().fileExistsAtPath(destination.absoluteString.stringByReplacingOccurrencesOfString("file://", withString: ""))
print("this is the check value response \(checkFile)")
var tmp = SSZipArchive.unzipFileAtPath(destinationUrl.absoluteString, toDestination: destination.absoluteString )
//var tmp = SSZipArchive.unzipFileAtPath(destination.absoluteString.stringByReplacingOccurrencesOfString("file://", withString: ""), toDestination: destination.absoluteString )
print("unzipped the file \(tmp)")
} else {
print("error saving file")
}
}
}
正在创建文件,但没有提取任何内容。
运行 今天我花了几个小时才弄明白。 SSZipArchive
returns false
当找不到 zip 文件时。这可能是因为您返回的 url 是一个 [NSURL absoluteString];
,它的开头有 file://
。只需在 url 上调用 [NSURL path];
就可以了
我正在使用 SSZipArchive
提取文件的内容,该文件是我从 Internet 下载并保存在 Documents
中的。我下载的资源是zip文件,下载成功后会解压
@IBAction func downloadData(sender: UIButton)
{ if let myAudioDataFromUrl = NSData(contentsOfURL: audioUrl){
// after downloading your data you need to save it to your destination url
if myAudioDataFromUrl.writeToURL(destinationUrl, atomically: true) {
print("file saved")
printTimestamp()
let folderName = "Aspire Brochure"//audioUrl.lastPathComponent!.stringByReplacingOccurrencesOfString(".zip", withString: "")
let destination = documentsUrl.URLByAppendingPathComponent(folderName)
let fileManager = NSFileManager.defaultManager()
print("This is the folderName \(destinationUrl)")
print("This is the destination \(destination)")
let success = fileManager.fileExistsAtPath(destination.absoluteString) as Bool
if success == false {
do {
print("it is here")
try! fileManager.createDirectoryAtPath(destination.absoluteString.stringByReplacingOccurrencesOfString("file://", withString: ""), withIntermediateDirectories: true, attributes: nil)
print("it hascome here ")
}
}
// let success = fileManager.fileExistsAtPath(destPath) as Bool
print("trying to unzip the file")
//let fileManager = NSFileManager.defaultManager()
let checkFile = NSFileManager.defaultManager().fileExistsAtPath(destination.absoluteString.stringByReplacingOccurrencesOfString("file://", withString: ""))
print("this is the check value response \(checkFile)")
var tmp = SSZipArchive.unzipFileAtPath(destinationUrl.absoluteString, toDestination: destination.absoluteString )
//var tmp = SSZipArchive.unzipFileAtPath(destination.absoluteString.stringByReplacingOccurrencesOfString("file://", withString: ""), toDestination: destination.absoluteString )
print("unzipped the file \(tmp)")
} else {
print("error saving file")
}
}
}
正在创建文件,但没有提取任何内容。
运行 今天我花了几个小时才弄明白。 SSZipArchive
returns false
当找不到 zip 文件时。这可能是因为您返回的 url 是一个 [NSURL absoluteString];
,它的开头有 file://
。只需在 url 上调用 [NSURL path];
就可以了