ZipArchive压缩文件上传

最近项目中要求能够上传图片和日志(可恶!你怎么不要求上传视频呢!),用的是老牌的ZipArchive,居然跳了几次坑,特来记录下来免得下次又忘记了=。=

下面这份就是全部的压缩代码了(上传的是最多五张图片):

    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    /** 新建文件夹路径*/
    NSString *imagePath = [path stringByAppendingPathComponent:@"feedBackImages"];
    NSFileManager *photoFileManager = [NSFileManager defaultManager];
    
    /** 判断该路径存不存在,若不存在就建立这个文件夹*/
    if (![photoFileManager fileExistsAtPath:imagePath]) {
        [photoFileManager createDirectoryAtPath:imagePath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    /** 检查文件夹有没有内容,若有则清空*/
    NSDirectoryEnumerator *dirEnum = [photoFileManager enumeratorAtPath:imagePath];
    NSString *fileName = @"";
    while (fileName = [dirEnum nextObject]) {
        [photoFileManager removeItemAtPath:[NSString stringWithFormat:@"%@/%@", imagePath, fileName] error:nil];
    }
    
    NSUInteger count = self.choosePictureView.imageArray.count - 1;
    NSUInteger length = 0;
    for (int i = 0; i < count; i++) {
        
        UIImage *photoImage = self.choosePictureView.imageArray[i];
        
        /** 压缩用户选择的图片*/
        NSData *dataImage = UIImageJPEGRepresentation(photoImage, 0.3);
        
        length += dataImage.length;
        
        if (length > 5 * pow(1024, 2)) {
            HintView *hintView = [[HintView alloc]initWith:LocatizedStirngForkey(@"上传图片过大")];
            [hintView showInView];
            
            return;
        }
        
        UIImage *choosePhoto = [UIImage imageWithData:dataImage];
        
        [self.choosePhotoArray addObject:choosePhoto];
        
        NSString *pictureNew = [NSString stringWithFormat:@"/picImage_%d.jpeg",i];
        
        /** 存入每张照片*/
        NSString *NewPhotoPath = [imagePath stringByAppendingString:pictureNew];
        [photoFileManager createFileAtPath:NewPhotoPath contents:dataImage attributes:nil];
    }
    
    ZipArchive *zip =[[ZipArchive alloc] init];
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *rootPathStr = ([paths count] > 0) ? paths[0] : nil;
    
    /** 图片压缩包名*/
    NSString *uploadFileName = @"feedbackPictureUpload.zip";
    NSString *photoZip = [rootPathStr stringByAppendingPathComponent:uploadFileName];
    
    
    /** 创建zip文件*/
    BOOL ret = [zip CreateZipFile2:photoZip];
    /** 遍历文件夹,将文件夹中的文件添加到压缩文件包中*/
    dirEnum = [photoFileManager enumeratorAtPath:imagePath];
    while ((fileName = [dirEnum nextObject]) != nil) {
        ret = [zip addFileToZip:[imagePath stringByAppendingPathComponent:fileName] newname:fileName];
    }

    /** 确认文件是否已经压缩完成*/
    if ([zip CloseZipFile2]) {
        [self feedbackPictureUpload:uploadFileName withFilePath:photoZip andFeedbackId:feedBackId];
    }

首先说一下我跳进去的坑吧:
1、第一个是最后我居然没有确认文件是不是已经压缩完成,就直接调用了上传的方法,服务器那边虽然能够收到压缩包,但是一直解压失败,提示文件损坏,这还是最后比较手机内生成的ZIP包和服务器那边收到的ZIP包大小的时候发现的= =!

最后提示![zip CloseZipFile2]!千万别忘了!

2、第二个就是文件的contentType了,貌似图片和别的contentType不一样,需要在AFNManager里加上multipart/form-data类型,服务器那边才收的到

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"multipart/form-data",@"text/plain", nil];

好了,就这么多了,其他的坑我倒是没遇到,等遇到了再来更新吧O.O

你可能感兴趣的:(ZipArchive压缩文件上传)