使用 ZipArchive
pod 'SSZipArchive'
#import <ZipArchive.h>
// 文件存储的地址数组
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 上传到服务器
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) {
}];
网友评论