zoukankan      html  css  js  c++  java
  • SSZipArchive的使用详解和遇到的问题

    https://blog.csdn.net/zhengang007/article/details/51019479

    2016年03月30日  版权声明:本文为博主原创文章,转载请注明作者和原文链接。 https://blog.csdn.net/zhengang007/article/details/51019479

    一、使用详解:

    我们在开发app的时候,有时会需要对文件进行压缩和解压的操作,这个时候我们就必须要用到一个第三方的开源库,SSZipArchive ,来对目标文件进行压缩和解压的操作。
    SSZipArchive下载链接,引入到工程时需要添加 libz.tbd 库,否则编译时通不过。
    压缩:

    //压缩
    - (void)createZipFile {
    //目的路径
    NSString *destinationPath = @"/Users/Administrator/Desktop/wzg.zip";//注意是这个是 zip 格式的后缀
    //源文件路径
    NSString *sourceFilePath = @"/Users/Administrator/Desktop/wzg.txt";
    //数组里可以放多个源文件,这些文件会被同一打包成压缩包,到 destinationPath 这个路径下。
    if ([SSZipArchive createZipFileAtPath:destinationPath withFilesAtPaths:@[sourceFilePath]]) {
    NSLog(@"压缩成功");
    }
    else {
    NSLog(@"压缩失败");
    }
    }
    解压:

    //解压
    - (void)unzipFile {
    //源文件路径
    NSString *sourceFilePath = @"/Users/Administrator/Desktop/wzg.zip";
    //目的文件路径
    NSString *destinationPath = @"/Users/wangzhengang/Desktop/";
    //把 sourceFilePath 这个路径下的zip包,解压到这个 destinationPath 路径下
    if ([SSZipArchive unzipFileAtPath:sourceFilePath toDestination:destinationPath delegate:self uniqueId:nil]){
    NSLog(@"解压成功");
    }
    else {
    NSLog(@"解压失败");
    }
    }

    代理方法:

    #pragma mark - SSZipArchiveDelegate
    - (void)zipArchiveWillUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo {
    NSLog(@"将要解压。");
    }

    - (void)zipArchiveDidUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo unzippedPath:(NSString *)unzippedPat uniqueId:(NSString *)uniqueId {
    NSLog(@"解压完成!");
    }

    二、SSZipArchive所有方法说明:

    @interface SSZipArchive : NSObject

    // Unzip 解压
    /**
    * @param path 源文件
    * @param destination 目的文件
    * @param uniqueId 标记,用于区别多个解压操作
    *
    * @return 返回 YES 表示成功,返回 NO 表示解压失败。
    */
    + (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination uniqueId:(NSString *)uniqueId;

    /**
    * @param path 源文件
    * @param destination 目的文件
    * @param overwrite YES 会覆盖 destination 路径下的同名文件,NO 则不会。
    * @param password 需要输入密码的才能解压的压缩包
    * @param error 返回解压时遇到的错误信息
    * @param uniqueId 标记,用于区别多个解压操作
    *
    * @return 返回 YES 表示成功,返回 NO 表示解压失败。
    */
    + (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination overwrite:(BOOL)overwrite password:(NSString *)password error:(NSError **)error uniqueId:(NSString *)uniqueId;

    /**
    * @param path 源文件
    * @param destination 目的文件
    * @param delegate 设置代理
    * @param uniqueId 标记,用于区别多个解压操作
    *
    * @return 返回 YES 表示成功,返回 NO 表示解压失败。
    */
    + (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination delegate:(id<SSZipArchiveDelegate>)delegate uniqueId:(NSString *)uniqueId;

    /**
    * @param path 源文件
    * @param destination 目的文件
    * @param overwrite YES 会覆盖 destination 路径下的同名文件,NO 则不会。
    * @param password 需要输入密码的才能解压的压缩包
    * @param error 返回解压时遇到的错误信息
    * @param delegate 设置代理
    * @param uniqueId 标记,用于区别多个解压操作
    *
    * @return 返回 YES 表示成功,返回 NO 表示解压失败。
    */
    + (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination overwrite:(BOOL)overwrite password:(NSString *)password error:(NSError **)error delegate:(id<SSZipArchiveDelegate>)delegate uniqueId:(NSString *)uniqueId;

    // Zip 压缩
    /**
    * @param path 目的路径(格式:~/xxx.zip 结尾的路径)
    * @param filenames 要压缩的文件路径
    *
    * @return 返回 YES 表示成功,返回 NO 表示压缩失败。
    */
    + (BOOL)createZipFileAtPath:(NSString *)path withFilesAtPaths:(NSArray *)filenames;

    /**
    * @param path 目的路径(格式:~/xxx.zip 结尾的路径)
    * @param filenames 要压缩的文件目录路径
    *
    * @return 返回 YES 表示成功,返回 NO 表示压缩失败。
    */
    + (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath;

    /**
    * 初始化压缩对象
    *
    * @param path 目的路径(格式:~/xxx.zip 结尾的路径)
    *
    * @return 初始化后的对像
    */
    - (id)initWithPath:(NSString *)path;

    /**
    * 打开压缩对象
    * @return 返回 YES 表示成功,返回 NO 表示失败。
    */
    - (BOOL)open;

    /**
    * 添加要压缩的文件的路径
    *
    * @param path 文件路径
    *
    * @return 返回 YES 表示成功,返回 NO 表示失败。
    */
    - (BOOL)writeFile:(NSString *)path;

    /**
    * 向此路径的文件里写入数据
    *
    * @param data 要写入的数据
    * @param filename 文件路径
    *
    * @return 返回 YES 表示成功,返回 NO 表示失败。
    */
    - (BOOL)writeData:(NSData *)data filename:(NSString *)filename;

    /**
    * 关闭压缩对象
    * @return 返回 YES 表示成功,返回 NO 表示失败。
    */
    - (BOOL)close;

    @end


    @protocol SSZipArchiveDelegate <NSObject>

    @optional

    //将要解压
    - (void)zipArchiveWillUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo;
    //解压完成
    - (void)zipArchiveDidUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo unzippedPath:(NSString *)unzippedPat uniqueId:(NSString *)uniqueId;
    //将要解压
    - (void)zipArchiveWillUnzipFileAtIndex:(NSInteger)fileIndex totalFiles:(NSInteger)totalFiles archivePath:(NSString *)archivePath fileInfo:(unz_file_info)fileInfo;
    //解压完成
    - (void)zipArchiveDidUnzipFileAtIndex:(NSInteger)fileIndex totalFiles:(NSInteger)totalFiles archivePath:(NSString *)archivePath fileInfo:(unz_file_info)fileInfo;

    @end

    三、遇到的问题:

    当对要压缩或者要解压的文件的文件名包含有中文文字时,这个时候会出现文件名乱码的问题,或者在目的路径下未能找到解压后的文件的问题。
    解决办法:

    在 SSZipArchive.m 文件中改一下对 文件路径的编码格式,即可。
    更改前:

    NSString *strPath = [NSString stringWithCString:filename encoding:NSUTF8StringEncoding];

    更改后:

    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);

    NSString *strPath = [NSString stringWithCString:filename encoding:NSUTF8StringEncoding];

    ---------------------
    作者:飞天舞桐
    来源:CSDN
    原文:https://blog.csdn.net/zhengang007/article/details/51019479
    版权声明:本文为博主原创文章,转载请附上博文链接!

    iOS SSZipArchive--压缩与解压缩

    https://www.jianshu.com/p/d80fb939d4c4

    2018.01.09 16:15* 字数 62 阅读 780评论 2

    配置SSZipArchive

    导入SSZipArchive后,先编译,会出现如下错误:


     
    异常.png

    解决方法:
    单击项目->Linked Frameworks and Libraries->点击左下角加号->输入libz.tbd->Add,再次编译即可。


     
    图示.png

    解压缩

    NSString *str = @"/Users/mazaiting/Desktop/tomcat.zip";

        

        [SSZipArchive unzipFileAtPath:str toDestination:@"/Users/mazaiting/Desktop/tomcat"];

    压缩

        [SSZipArchive createZipFileAtPath:@"/Users/mazaiting/Desktop/tomcat1.zip" withContentsOfDirectory:@"/Users/mazaiting/Desktop/tomcat"];

    作者:_凌浩雨

    链接:https://www.jianshu.com/p/d80fb939d4c4

    来源:简书

    简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。


  • 相关阅读:
    【Nginx】--Linux服务器中配置Nginx一个域名访问多个项目
    【node】-- express 热部署,修改不重新启动
    typora快捷键
    简单工厂模式
    软件设计七大原则
    Spring:事务的传播行为
    Spring:MVC执行流程
    Spring:beanfactory中循环依赖和命名重复
    Spring:MVC启动时的WebApplicationContext的关系
    Spring:如何实现注解的组合
  • 原文地址:https://www.cnblogs.com/sundaysgarden/p/10748661.html
Copyright © 2011-2022 走看看