壓縮文件上傳的場景
前段時(shí)間,工作中有一個(gè)這樣的需求,用戶選擇本機(jī)的多張圖片要以壓縮包(zip)格式的方式來進(jìn)行上傳服務(wù)器.
工作準(zhǔn)備
本文提供的方法使用ZipArchive
來實(shí)現(xiàn)
首先需要在本地創(chuàng)建壓縮包的臨時(shí)路徑
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *rootPathStr = ([paths count] > 0) ? paths[0] : nil;
/** 壓縮包名*/
NSString *uploadFileName = @"publicRequireFile.zip";
NSString *photoZip = [rootPathStr stringByAppendingPathComponent:uploadFileName];
第二步創(chuàng)建壓縮文件
ZipArchive *zip =[[ZipArchive alloc] init];
/** 創(chuàng)建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]
第五步實(shí)現(xiàn)上傳服務(wù)器,與圖片的上傳基本一致不在做過多的介紹