使用 ZipArchive
pod 'SSZipArchive'
#import <ZipArchive.h>
// 文件存儲的地址數(shù)組
NSArray *filesArr = @[path1, path2, path3];
// 壓縮包存儲到沙盒
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
// files 壓縮包名字,zipFilepath 壓縮文件地址
NSString *zipFilepath = [caches stringByAppendingPathComponent:@"files.zip"];
[SSZipArchive createZipFileAtPath:zipFilepath withFilesAtPaths:filesArr];
NSData *filesZipData = [NSData dataWithContentsOfFile:zipFilepath];
使用afn 上傳到服務(wù)器
NSData *filesZipData = [NSData dataWithContentsOfFile:zipFilepath];
AFHTTPSessionManager *sessionManager = [[AFHTTPSessionManager alloc] initWithBaseURL:nil];
[sessionManager
POST:@"https://path"
parameters:nil
constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
[formData appendPartWithFileData:filesZipData
name:@"fileParamName"
fileName:[NSString stringWithFormat:@"%@.%@",
@"fileParamName", type]
mimeType:[NSString stringWithFormat:@"groupFile/%@", type]];
} progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];