美文网首页
iOS 使用 ZipArchive 压缩 zip 并上传

iOS 使用 ZipArchive 压缩 zip 并上传

作者: LearningCoding | 来源:发表于2019-04-10 11:59 被阅读0次

使用 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) {
            }];

相关文章

网友评论

      本文标题:iOS 使用 ZipArchive 压缩 zip 并上传

      本文链接:https://www.haomeiwen.com/subject/njcniqtx.html