编辑2:
我尝试了 marmelroy ( https://github.com/marmelroy/Zip ) 的 Zip,但它在创建女巫的 zip 文件上也失败ZipArchive
了。
然后我用 Zip 创建了另一个 zip 文件,这个文件可以很好地解压。
对我来说似乎有问题ZipArchive
。我将改用 Zip,对我来说就是这样......
谢谢你的答案!
结束编辑 2。
我需要解压缩某些文件,所以我手动安装了SSZipArchive
. SSZipArchive
将所有 3 个文件夹 ( , minizip
, )复制aes
到项目中,添加#import "ZipArchive.h"
到我的 bridging.h 中并且所有构建都很好。
编辑:
按照推荐使用迦太基,但行为相同。
结束编辑。
我用Xcode 8 / Swift 3
在没有解压缩任何文件的情况下进行了几次测试后,我使用SSZipArchive
.
let file = "file.txt"
let text = "some text" //just a text
let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
let path = dir?.appendingPathComponent(file)
//writing
do {
try text.write(to: path!, atomically: false, encoding: String.Encoding.utf8)
}
catch {
print("Failed writing")
}
这里我 print() Documents 目录:["file.txt"]
let zipPath = tempZipPath() //tempZipPath is taken from the SSZipArchiveExample except it uses DocumentDirectory instead of Cache...
print("zipPath: \(zipPath)")
print zipPath: /var/mobile/Containers/Data/Application/EB439A61-07B9-4910-BF28-03E85C50B292/Documents/93428A1B-E5B6-434F-B049-632B7519B126.zip
let success = SSZipArchive.createZipFile(atPath: zipPath, withContentsOfDirectory: (dir?.absoluteString)!)
if success {
print("zipped")
} else {
print(" NOT zipped")
}
再次是 Documentsdirectories 条目:["93428A1B-E5B6-434F-B049-632B7519B126.zip", "file.txt"]
// this is again taken from the original Example
guard let unzipPath = tempUnzipPath() else {
return
}
print("unzipPath: \(unzipPath)")
print unzipPath: /var/mobile/Containers/Data/Application/EB439A61-07B9-4910-BF28-03E85C50B292/Documents/E4FC7DE6-F21B-46D8-9953-DCBF86E2268E
读取这个新目录的条目:[]
let filePath = zipPath
var fileSize : UInt64 = 0
do {
let attr : NSDictionary? = try FileManager.default.attributesOfItem(atPath: filePath) as NSDictionary?
if let _attr = attr {
fileSize = _attr.fileSize();
print("fileSize: \(fileSize)")
}
} catch {
print("Error: \(error)")
}
这写道22
。我还尝试阅读工作正常的 zip 文件
从这部分开始,我很绝望该怎么办
let url = URL(fileURLWithPath: zipPath)
print("url: \(url)")
print("url.path: \(url.path)")
print("url.absoluteString: \(url.absoluteString)")
print("url.absoluteURL: \(url.absoluteURL)")
print("url.absoluteURL.absoluteString: \(url.absoluteURL.absoluteString)")
这些行的输出对我来说都很好:
网址:file:///var/mobile/Containers/Data/Application/EB439A61-07B9-4910-BF28-03E85C50B292/Documents/93428A1B-E5B6-434F-B049-632B7519B126.zip
url.path:/var/mobile/Containers/Data/Application/EB439A61-07B9-4910-BF28-03E85C50B292/Documents/93428A1B-E5B6-434F-B049-632B7519B126.zip
url.absoluteString:file:///var/mobile/Containers/Data/Application/EB439A61-07B9-4910-BF28-03E85C50B292/Documents/93428A1B-E5B6-434F-B049-632B7519B126.zip
url.absoluteURL:file:///var/mobile/Containers/Data/Application/EB439A61-07B9-4910-BF28-03E85C50B292/Documents/93428A1B-E5B6-434F-B049-632B7519B126.zip
url.absoluteURL.absoluteString:file:///var/mobile/Containers/Data/Application/EB439A61-07B9-4910-BF28-03E85C50B292/Documents/93428A1B-E5B6-434F-B049-632B7519B126.zip
然后我全部尝试了(当然是字符串。不允许使用 url)
var success2 = SSZipArchive.unzipFile(atPath: zipPath, toDestination: unzipPath)
if !success2 {
print("zipPath")
}
success2 = SSZipArchive.unzipFile(atPath: url.path, toDestination: unzipPath)
if !success2 {
print("url.path")
}
success2 = SSZipArchive.unzipFile(atPath: url.absoluteString, toDestination: unzipPath)
if !success2 {
print("url.absoluteString")
}
success2 = SSZipArchive.unzipFile(atPath: url.absoluteURL.path, toDestination: unzipPath)
if !success2 {
print("url.absoluteURL.path")
}
它打印每一行,所以他们都失败了。最后,这里是“漫长”的道路unipFileAtPath
。我也试过这个zipPath
,......所有相同的结果。
do {
success2 = try SSZipArchive.unzipFileAtPath(url.path, toDestination: unzipPath, overwrite: true, password: nil, delegate: nil)
if !success2 {
return
}
} catch {
print("error \(error)")
print("error \(error.localizedDescription)")
}
error Error Domain=SSZipArchiveErrorDomain Code=-1 "未能打开 zip 文件" UserInfo={NSLocalizedDescription=未能打开 zip 文件} 错误未能打开 zip 文件
这里是 to func 来获取路径字符串。顺便提一句。我修改tempZipPath
所以返回一个 URL 或 URL.path
但是,嘿:我很绝望
func tempZipPath() -> String {
var path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
path += "/\(UUID().uuidString).zip"
return path
}
func tempUnzipPath() -> String? {
var path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
path += "/\(UUID().uuidString)"
let url = URL(fileURLWithPath: path)
do {
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil)
} catch {
return nil
}
return url.path
}
有什么建议么?有什么需要考虑手动安装的SSZipArchive
吗?
正如您在上面看到的,提示“ use .path instead of .absoluteString
”对我不起作用。
绝望的tartsigam