是否存在與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