IOS-关于第三方解压库ZipArchive解压后中文乱码问题
来源:互联网 发布:python 爬虫 伯乐 编辑:程序博客网 时间:2024/04/30 00:05
-(BOOL) UnzipFileTo:(NSString*) path overWrite:(BOOL) overwrite{BOOL success = YES; int index = 0; int progress = -1;int ret = unzGoToFirstFile( _unzFile );unsigned charbuffer[4096] = {0};if( ret!=UNZ_OK ){[self OutputErrorMessage:@"Failed"];} const char* password = [_password cStringUsingEncoding:NSASCIIStringEncoding];do{ @autoreleasepool { if( [_password length]==0 ) ret = unzOpenCurrentFile( _unzFile ); else ret = unzOpenCurrentFilePassword( _unzFile, password ); if( ret!=UNZ_OK ) { [self OutputErrorMessage:@"Error occurs"]; success = NO; break; } // reading data and write to file int read ; unz_file_infofileInfo ={0}; ret = unzGetCurrentFileInfo(_unzFile, &fileInfo, NULL, 0, NULL, 0, NULL, 0); if( ret!=UNZ_OK ) { [self OutputErrorMessage:@"Error occurs while getting file info"]; success = NO; unzCloseCurrentFile( _unzFile ); break; } char* filename = (char*) malloc( fileInfo.size_filename +1 ); unzGetCurrentFileInfo(_unzFile, &fileInfo, filename, fileInfo.size_filename + 1, NULL, 0, NULL, 0); filename[fileInfo.size_filename] = '\0'; // check if it contains directory// NSString * strPath = [NSString stringWithCString:filename encoding:self.stringEncoding]; 把这句话注释掉换成以下两句 NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000); NSString * strPath = [NSString stringWithCString:filename encoding:enc]; BOOL isDirectory = NO; if( filename[fileInfo.size_filename-1]=='/' || filename[fileInfo.size_filename-1]=='\\') isDirectory = YES; free( filename ); if( [strPath rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"/\\"]].location!=NSNotFound ) {// contains a path strPath = [strPath stringByReplacingOccurrencesOfString:@"\\" withString:@"/"]; } NSString* fullPath = [path stringByAppendingPathComponent:strPath]; if( isDirectory ) [_fileManager createDirectoryAtPath:fullPath withIntermediateDirectories:YES attributes:nil error:nil]; else [_fileManager createDirectoryAtPath:[fullPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil]; FILE* fp = NULL; do { read = unzReadCurrentFile(_unzFile, buffer, 4096); if (read >= 0) { if (fp == NULL) { if( [_fileManager fileExistsAtPath:fullPath] && !isDirectory && !overwrite ) { if( ![self OverWrite:fullPath] ) { // don't process any more of the file, but continue break; } } if (!isDirectory) { fp = fopen( (const char*)[fullPath UTF8String], "wb"); if (fp == NULL) { [self OutputErrorMessage:@"Failed to open output file for writing"]; break; } } } fwrite(buffer, read, 1, fp ); } else // if (read < 0) { ret = read; // result will be an error code success = NO; [self OutputErrorMessage:@"Failed to read zip file"]; } } while (read > 0); if (fp) { fclose( fp ); // add the full path of this file to the output array [(NSMutableArray*)_unzippedFiles addObject:fullPath]; // set the orignal datetime property if( fileInfo.tmu_date.tm_year!=0 ) { NSDateComponents* components = [[NSDateComponents alloc] init]; components.second = fileInfo.tmu_date.tm_sec; components.minute = fileInfo.tmu_date.tm_min; components.hour = fileInfo.tmu_date.tm_hour; components.day = fileInfo.tmu_date.tm_mday; components.month = fileInfo.tmu_date.tm_mon; components.year = fileInfo.tmu_date.tm_year; NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDate* orgDate = [[gregorianCalendar dateFromComponents:components] retain]; [components release]; [gregorianCalendar release]; NSDictionary* attr = [NSDictionary dictionaryWithObject:orgDate forKey:NSFileModificationDate]; //[_fileManager fileAttributesAtPath:fullPath traverseLink:YES]; if( attr ) { //[attr setValue:orgDate forKey:NSFileCreationDate]; if( ![_fileManager setAttributes:attr ofItemAtPath:fullPath error:nil] ) { // cann't set attributes NSLog(@"Failed to set attributes"); } } [orgDate release]; orgDate = nil; } } if (ret == UNZ_OK) { ret = unzCloseCurrentFile( _unzFile ); if (ret != UNZ_OK) { [self OutputErrorMessage:@"file was unzipped but failed crc check"]; success = NO; } } if (ret == UNZ_OK) { ret = unzGoToNextFile( _unzFile ); } if (_progressBlock && _numFiles) { index++; int p = index*100/_numFiles; progress = p; _progressBlock(progress, index, _numFiles); } }} while (ret==UNZ_OK && ret!=UNZ_END_OF_LIST_OF_FILE);return success;}
0 0
- IOS-关于第三方解压库ZipArchive解压后中文乱码问题
- 关于ZipArchive 解压中文编码乱码的问题
- iOS关于rar解压第三方库Unrar4iOS使用总结
- IOS 使用ZipArchive解压文件
- Ubuntu 解压中文乱码问题
- 关于Quazip解压中文乱码问题的解决
- iOS用ZipArchive解压zip包
- IOS使用ZipArchive压缩解压文件
- ubuntu rar文件解压中文乱码问题
- ubuntu解压zip文件中文乱码问题
- linux解压zip中文乱码问题
- ubuntu 解压中文压缩文件乱码问题
- linux unzip解压中文乱码问题
- 中文乱码 解压
- Android解压中文乱码
- ZipArchive 解压zip
- ZipArchive 压缩与解压
- c# 第三方解压sharpziplib
- OpenCV仿射变换+投射变换+单应性矩阵
- DataGridView的列和详细内容的对齐
- 【老了】原来我不是派来拯救世界的
- Android中@id和@+id的区别
- VELT-0.1.3开发:参数整理
- IOS-关于第三方解压库ZipArchive解压后中文乱码问题
- hbase 学习梳理
- 如何制作网格粒子发射器
- 纵专谆壮爪妆资综装族装着紫篆拽钻篆坐
- 爪驻庄祝酌酌着族赚邹壮仔自酌缀注锥状
- 换种思路去理解设计模式(中)
- csdn 的第一天
- 使用myeclipse将Java项目打成jar包遇到classnotfound的问题解决办法
- jQuery-插件