美文网首页
iOS 压缩文件夹及其子目录文件

iOS 压缩文件夹及其子目录文件

作者: qqqqnnnndddd | 来源:发表于2021-03-17 13:49 被阅读0次

    添加Ziparchive框架  pod 'ZipArchive', '~> 1.3.0'

    link binrary with Libraies添加库的framework文件

    引入头文件 #import <ZipArchive/ZipArchive.h>

    /**

      *  根据路径将文件压缩为zip到指定路径

      *

      *  @param sourcePath 压缩文件夹路径

      *  @param destZipFile存放路径(保存重命名)路径.zip结尾

      */

    -(void) doZipAtPath:(NSString*)sourcePath to:(NSString*)destZipFile

    {

        NSFileManager *fileManager = [NSFileManager defaultManager];

        ZipArchive * zipArchive = [ZipArchive new];

        [zipArchive CreateZipFile2:destZipFile];

        NSArray *subPaths = [fileManager subpathsAtPath:sourcePath];// 关键是subpathsAtPath方法

        for(NSString *subPath in subPaths){

            NSString *fullPath = [sourcePath stringByAppendingPathComponent:subPath];

            BOOL isDir;

            if([fileManager fileExistsAtPath:fullPath isDirectory:&isDir] && !isDir)// 只处理文件

            {

                [zipArchive addFileToZip:fullPath newname:subPath];

            }

        }

          [zipArchive CloseZipFile2];

    }

    转自:https://blog.csdn.net/jinsulei123/article/details/26086839

    相关文章

      网友评论

          本文标题:iOS 压缩文件夹及其子目录文件

          本文链接:https://www.haomeiwen.com/subject/fiiscltx.html