压缩文件上传的场景
前段时间,工作中有一个这样的需求,用户选择本机的多张图片要以压缩包(zip)格式的方式来进行上传服务器.
工作准备
本文提供的方法使用ZipArchive
来实现
首先需要在本地创建压缩包的临时路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *rootPathStr = ([paths count] > 0) ? paths[0] : nil;
/** 压缩包名*/
NSString *uploadFileName = @"publicRequireFile.zip";
NSString *photoZip = [rootPathStr stringByAppendingPathComponent:uploadFileName];
第二步创建压缩文件
ZipArchive *zip =[[ZipArchive alloc] init];
/** 创建zip文件*/
BOOL ret = [zip CreateZipFile2:photoZip];
第三步 /** 遍历文件夹,将文件夹中的文件添加到压缩文件包中*/
dirEnum = [photoFileManager enumeratorAtPath:imagePath];
while ((fileName = [dirEnum nextObject]) != nil) {
ret = [zip addFileToZip:[imagePath stringByAppendingPathComponent:fileName] newname:fileName];
}
第四步,也是最重要的一步判断文件是否压缩成功
[zip CloseZipFile2]
第五步实现上传服务器,与图片的上传基本一致不在做过多的介绍
网友评论