iOS 解压zip rar

由于近期需要开发一款带有解压功能的小工具软件,调研了一下。有关配置zip解压和rar解压的教程,发现非常乱,而且没有什么章法。很多都是相互复制粘贴,在踩了许多坑之后。在此分享一个比较完善的步骤,共同进步。

第一步:

 如果是从别的应用程序通过系统的用其他应用打开或者复制文件到我们的App如此图:


第一步
第二步

所以我们需要如何让我们的App显示到系统的分享里面,我们需要配置info.plist


第三步

鼠标右键-> Open As -> Source Code(XML格式)  把下面这段配置文件粘贴进去然后再鼠标右键-> Open As -> Property List (list格式)

CFBundleDocumentTypes

CFBundleTypeIconFiles

58

80

120

CFBundleTypeName

com.myapp.common-data

LSHandlerRank

Default

LSItemContentTypes

com.microsoft.powerpoint.ppt

public.item

com.microsoft.word.doc

com.adobe.pdf

com.microsoft.excel.xls

public.image

public.content

public.composite-content

public.archive

public.audio

public.movie

public.text

public.data


代码App可以接收的数据格式

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 以上就是所有的步骤了。回见各位

你可能感兴趣的:(iOS 解压zip rar)