iOS 使用 ZipArchive 压缩 zip 并上传

使用 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) {
            }];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。