由于近期需要開發一款帶有解壓功能的小工具軟件,調研了一下。有關配置zip解壓和rar解壓的教程,發現非常亂,而且沒有什么章法。很多都是相互復制粘貼,在踩了許多坑之后。在此分享一個比較完善的步驟,共同進步。
第一步:
?如果是從別的應用程序通過系統的用其他應用打開或者復制文件到我們的App如此圖:
所以我們需要如何讓我們的App顯示到系統的分享里面,我們需要配置info.plist
鼠標右鍵-> Open As -> Source Code(XML格式)? 把下面這段配置文件粘貼進去然后再鼠標右鍵-> Open As -> Property List (list格式)
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeIconFiles</key>
<array>
<string>58</string>
<string>80</string>
<string>120</string>
</array>
<key>CFBundleTypeName</key>
<string>com.myapp.common-data</string>
<key>LSHandlerRank</key>
<string>Default</string>
<key>LSItemContentTypes</key>
<array>
<string>com.microsoft.powerpoint.ppt</string>
<string>public.item</string>
<string>com.microsoft.word.doc</string>
<string>com.adobe.pdf</string>
<string>com.microsoft.excel.xls</string>
<string>public.image</string>
<string>public.content</string>
<string>public.composite-content</string>
<string>public.archive</string>
<string>public.audio</string>
<string>public.movie</string>
<string>public.text</string>
<string>public.data</string>
</array>
</dict>
</array>
data代表接收的數據格式,不管先梭哈再說。想要仔細了解的同學,可以自行google
DocumentTypeName 第一條就是蘋果官方介紹文檔
LSHandlerRank是代表優先級,就是顯示我們的App的順序? 這里我設置的是默認default 最高級是owner
LSItemContentTypes? 代表接收的內容格式
CFBundleTypeIconFiles
代表App顯示的icon 這個icon需要放在項目主目錄也就是Bundle Main
到這里就可以通過文件共享的方式找到并且復制文件到我們的App了
解決這個問題了,我們就開始進入正題了。配置zip 和rar的第三方庫(有大哥可以自己寫的就可以跳過了)
集成zip庫
https://github.com/ZipArchive/ZipArchive??? zip庫
1. 將SSZipArchive和minizip文件夾添加到項目中。
2. 將libz和libiconv庫添加到項目中。
3. 將Security框架添加到項目中。
4. 添加以下GCC_PREPROCESSOR_DEFINITIONS :?HAVE_INTTYPES_H HAVE_PKCRYPT HAVE_STDINT_H HAVE_WZAES HAVE_ZLIB MZ_ZIP_NO_SIGNING $(inherited).
3. 因為ZipArchive 是C集成的庫所以需要更改c文件的編譯方式 選中所有.c的文件 然后找到右邊的Xcode工具欄的identity and Type? 更改type 為 Objective-Source
4. 如果需要解壓的文件帶有中文字符,會出現解壓后中文變成亂碼的問題 需要在SSZipArchive.m文件里的
_filenameStringWithCString? 函數更改原作者的字符串編碼方式改為GB中文編碼? 由kCFStringEncodingDOSLatinUS? 編碼改為 kCFStringEncodingGB_18030_2000
這個問題坑了筆者蠻長時間的亂碼問題。
?集成解壓RAR
https://github.com/abbeycode/UnrarKit???
上面是github地址? 下載下來后直接打開pods的workspeace 然后找到frameworks里的unrarkit? -> show in finder - > copy到工程 然后再工程的Embedded Binaries? 完成啦
以上就集成好了zip 和rar 功能了
因為剛好這個項目是用swift寫的所以例如:在QQ打開我們的App之后文件怎么拿到呢,
通過AppDelegate.Swift里的
//接收來自其他app分享的文件
? ? override func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
? ? ? ? UserDefaults.standard.set(url, forKey: "ArchiveURL")
? ? ? ? UserDefaults.standard.synchronize()
? ? ? ? let data = try! Data.init(contentsOf: url)
? ? ? ? if data.count > 0 {
? ? ? ? ? ? NotificationCenter.default.post(name: Notification.Name("unArchive"), object: nil)
? ? ? ? }
? ? ? ? return true
? ? }
系統會自動幫我們復制到 docments 下的 inbox文件夾
這個url就是我們沙盒中的文件地址了可以直接獲取到Data了
生活嘛總要帶點
/// 解壓
- (BOOL)unArchiveZipWithZipPath:(NSString *)zipPath unArchivePath:(NSString *)unArchivePath {
??? BOOL isSucess = NO;
??? isSucess =? [SSZipArchive unzipFileAtPath:zipPath toDestination:unArchivePath];
??? if (isSucess) {
??????? //NSLog(@"解壓成功");
??????? isSucess = YES;
??? }else {
??????? //NSLog(@"解壓失敗");
??????? isSucess = NO;
??? }
??? return isSucess;
}
///解壓
- (BOOL)unArchiveRARWithRARPath:(NSString *)rarPath unArchivePath:(NSString *)unArchivePath {
??? BOOL isSucess = NO;
??? URKArchive * archive = [[URKArchive alloc]initWithPath:rarPath error:nil];
??? NSError *error = nil;
??? isSucess = [archive extractFilesTo:unArchivePath overwrite:NO error:&error];
??? if (error) {
???????
??????? isSucess = NO;
??? }
??? return isSucess;
}
///壓縮
- (BOOL)archiveWithPath:(NSString *)url fileName:(NSString *)fileName forPath:(NSString *)forPath{
??? if ([fileName containsString:@"."]) {
?????? fileName =? [fileName componentsSeparatedByString:@"."].firstObject;
??? }
??? forPath = [forPath stringByAppendingString:[NSString stringWithFormat:@"/%@.zip",fileName]];
??? // 壓縮后的路徑和文件名在前??? 需要壓縮的路徑再后
??? return [self archiveZipAtZipPath:forPath forPath:url];
}
///壓縮
- (BOOL)archiveZipAtZipPath:(NSString *)zipPath forPath:(NSString *)forPath {
???? BOOL isSucess = NO;
???
???? isSucess = [SSZipArchive createZipFileAtPath:zipPath withFilesAtPaths:@[forPath]];
??? if (isSucess) {
??????? //NSLog(@"壓縮成功");
??????? isSucess = YES;
??? }else {
??????? //NSLog(@"壓縮失敗");
??????? isSucess = NO;
??? }
??? return isSucess;
}
OK 以上就是所有的步驟了。回見各位