是否存在与Java包java.util.zip
中包含的Objective-C类相同的Objective-C类?
是执行CLI命令唯一的选择吗?从Cocoa应用程序创建ZIP归档文件
回答
从iOS8/OSX10.10开始,有一种使用NSFileCoordinatorReadingOptions.ForUploading
创建zip档案的内置方法。一个简单的例子创建ZIP档案没有任何非可可的依赖关系:
public extension NSURL {
/// Creates a zip archive of the file/folder represented by this URL and returns a references to the zipped file
///
/// - parameter dest: the destination URL; if nil, the destination will be this URL with ".zip" appended
func zip(dest: NSURL? = nil) throws -> NSURL {
let destURL = dest ?? self.URLByAppendingPathExtension("zip")
let fm = NSFileManager.defaultManager()
var isDir: ObjCBool = false
let srcDir: NSURL
let srcDirIsTemporary: Bool
if let path = self.path where self.fileURL && fm.fileExistsAtPath(path, isDirectory: &isDir) && isDir.boolValue == true {
// this URL is a directory: just zip it in-place
srcDir = self
srcDirIsTemporary = false
} else {
// otherwise we need to copy the simple file to a temporary directory in order for
// NSFileCoordinatorReadingOptions.ForUploading to actually zip it up
srcDir = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent(NSUUID().UUIDString)
try fm.createDirectoryAtURL(srcDir, withIntermediateDirectories: true, attributes: nil)
let tmpURL = srcDir.URLByAppendingPathComponent(self.lastPathComponent ?? "file")
try fm.copyItemAtURL(self, toURL: tmpURL)
srcDirIsTemporary = true
}
let coord = NSFileCoordinator()
var error: NSError?
// coordinateReadingItemAtURL is invoked synchronously, but the passed in zippedURL is only valid
// for the duration of the block, so it needs to be copied out
coord.coordinateReadingItemAtURL(srcDir, options: NSFileCoordinatorReadingOptions.ForUploading, error: &error) { (zippedURL: NSURL) -> Void in
do {
try fm.copyItemAtURL(zippedURL, toURL: destURL)
} catch let err {
error = err as NSError
}
}
if srcDirIsTemporary { try fm.removeItemAtURL(srcDir) }
if let error = error { throw error }
return destURL
}
}
public extension NSData {
/// Creates a zip archive of this data via a temporary file and returns the zipped contents
func zip() throws -> NSData {
let tmpURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent(NSUUID().UUIDString)
try self.writeToURL(tmpURL, options: NSDataWritingOptions.DataWritingAtomic)
let zipURL = try tmpURL.zip()
let fm = NSFileManager.defaultManager()
let zippedData = try NSData(contentsOfURL: zipURL, options: NSDataReadingOptions())
try fm.removeItemAtURL(tmpURL) // clean up
try fm.removeItemAtURL(zipURL)
return zippedData
}
}
有一个zip框架,http://code.google.com/p/zip-framework/,但它似乎处于早期(版本0.1)阶段。
上cocoadev其他答案:http://cocoadev.com/ZipArchiveLibraryForCocoa从那里
一个答案:ZipKit,https://github.com/kolpanic/ZipKit
ZipKit似乎更好的名字它的方法比ZipArchive(即使我不明白为什么它的前缀的另外的方法)。 – kiamlaluno 2009-12-18 13:52:54
前缀或后缀类别方法有助于避免名称冲突,如果苹果在Cocoa中添加同名方法。 – 2009-12-19 07:17:25
ZipKit现在位于https://github.com/kolpanic/ZipKit,不再位于bitbucket(令人震惊) – uchuugaka 2013-10-17 00:58:02
退房http://code.google.com/p/ziparchive/。这是一个压缩文件的类。 Google是你的朋友!
除了在您自己的过程中阅读和编写zip归档文件,使用NSTask运行zip
和unzip
并不是件耻辱。
使用此方法将允许不更改代码以支持新功能。我不知道当你选择一个文件/目录时,Finder究竟发生了什么,然后从菜单中选择“压缩”。 Finder使用哪个可执行文件? – kiamlaluno 2009-12-19 06:59:03
它使用存档实用程序。 – 2009-12-19 07:16:22
退房zipzap,我快zip文件I/O库。
- 1. 从Filechannels创建Zip归档文件
- 2. 创建使用Go的存档Unicode文件名zip存档/ ZIP
- 3. cocoa基于文档的应用程序
- 4. 创建一个没有NIB文件的Cocoa应用程序
- 5. 创建Cocoa程序
- 6. 从zip创建文件 - android
- 7. 在C#中的多个内存文件中创建Zip归档文件
- 8. 从Cocoa应用程序访问Safari文档的DOM
- 9. Corupt ZIP文件servlet对多个文件归档的响应
- 10. 如何从我的文档目录创建.zip文件?
- 11. 从文件夹创建zip文件
- 12. 从多个文件创建zip文件
- 13. 创建iPhone归档应用程序时出现错误
- 14. 从Cocoa应用程序
- 15. 从Cocoa应用程序
- 16. 创建Word文档并从.NET应用程序添加图像
- 17. 从Android应用程序创建输出文档
- 18. 从pdf文档创建iOS应用程序
- 19. 如何从Silverlight 4应用程序创建Word文档?
- 20. 从外部应用程序创建莲花笔记文档
- 21. 从.net应用程序创建Word文档
- 22. 从zip档案创建和解压缩文件
- 23. 从PHPExcel Objects创建ZIP存档
- 24. 如何使用Python从远程Zip归档通过HTTP读取选定文件?
- 25. 创建zip文件时使用gulp-vinyl-zip创建TypeError
- 26. 使用adm-zip从JSON对象创建zip文件
- 27. 从文档目录中的文件夹创建ZIP文件 - 目标C(ARC)
- 28. 如何从应用程序文件创建一个apk文件?
- 29. 在Cocoa应用程序上禁用基于文档的功能
- 30. 从一个字符串中解压zip归档文件
这很好,但是反过来呢?也就是说,有一个zip文件,然后阅读它的内容,就好像它是一个目录? – adib 2015-12-31 04:47:41
我看起来,但找不到任何类似的方法去其他方式。我已经投票赞成这个答案,因为它太棒了。但是,在我的Mac应用程序中,我使用NSTask来调用/ usr/bin/zip和/ usr/bin/unzip。它很简单,提供了很多*记录的*选项来控制行为,在我的情况下,代码比这少。 – 2016-02-26 14:20:51