[英]Objective Zip not working with iOS5.1 devices, not properly unzipping
I am using objective zip in a project to unzip and store files to documents directory. 我在项目中使用目标zip解压缩并将文件存储到文档目录。 Its working fine with iOS 5.0 and below versions. 它可以在iOS 5.0及以下版本上正常工作。 Working fine with 5.1 Simulator. 使用5.1 Simulator可以正常工作。 But when working on 5.1 device, only a few files are unzipped. 但是在5.1设备上工作时,仅解压缩了几个文件。 Nothing else is shown in the folder. 文件夹中没有其他内容。 below is the code used for unzipping and storage. 以下是用于解压缩和存储的代码。
for (NSString *file in fileArray) {
// Get the file info/name, prepare the target name/path
ZipReadStream *readStream = [unzipFile readCurrentFileInZip];
FileInZipInfo *fileInfo = [unzipFile getCurrentFileInZipInfo];
fileNameString = [NSString stringWithFormat:@"%@",[fileInfo name]];
NSLog(@"fileNameString %@",fileNameString);
NSString *DirName = [targetFolder stringByAppendingPathComponent:moduleName];
NSLog(@"targetFolder %@",targetFolder);
NSLog(@"DirName %@",DirName);
NSLog(@"fileNameString %@",fileNameString);
if (![fileManager fileExistsAtPath:DirName isDirectory:nil]) {
[fileManager createDirectoryAtPath:DirName attributes:nil];
NSLog(@"created directory %@", DirName);
}
NSLog(@"fileNameString %@",fileNameString);
NSString *unzipFilePath = [DirName stringByAppendingPathComponent:fileNameString];
NSLog(@"unzipFilePath-- %@",unzipFilePath);
// Create a file handle for writing the unzipped file contents
if (![fileManager fileExistsAtPath:unzipFilePath]) {
NSString *dir = unzipFilePath;//[unzipFilePath stringByDeletingLastPathComponent];
if ([[fileNameString pathExtension] isEqualToString:@""]) {
[fileManager createDirectoryAtPath:dir attributes:nil];
NSLog(@"created directory %@", dir);
}
[fileManager createFileAtPath:unzipFilePath contents:nil attributes:nil];
}
fileHandle = [NSFileHandle fileHandleForWritingAtPath:unzipFilePath];
// Read-then-write buffered loop to conserve memory
do {
// Reset buffer length
[unzipBuffer setLength:BUFFER_SIZE];
// Expand next chunk of bytes
int bytesRead = [readStream readDataWithBuffer:unzipBuffer];
if (bytesRead > 0) {
// Write what we have read
[unzipBuffer setLength:bytesRead];
[fileHandle writeData:unzipBuffer];
} else
break;
} while (YES);
[readStream finishedReading];
// NOTE: Disable iCloud backup for unzipped file if applicable here!
/*...*/
//[fileHandle writeData:unZipped];
[fileHandle closeFile];
[unzipFile goToNextFileInZip];
}
[unzipFile close]; // Be sure to also manage your memory manually if not using ARC!
// Also delete the zip file here to conserve disk space if applicable!
[recievedData release];
NSLog(@"Delete -- %@", documentsDirectory);
[fileManager removeItemAtPath:documentsDirectory error:nil];
return YES;
}
Please help ! 请帮忙 ! ! ! ! !
thanks in Advance 提前致谢
Use have the following method for unzipping a zip file using Objective-zip. 使用以下方法使用Objective-zip解压缩zip文件。 This works fine under iOS 4.3 through 6.0 (and probably earlier and later too). 在iOS 4.3到6.0之间(以及更早或更晚的版本)下,此方法也可以正常工作。 The 'filename' parameter is the full path to the zip file. “文件名”参数是zip文件的完整路径。
- (BOOL)unzipPath:(NSString *)filename toDirectory:(NSString *)directory error:(NSError **)error {
if (error) {
*error = nil;
}
ZipFile *unzipFile = [[ZipFile alloc] initWithFileName:filename mode:ZipFileModeUnzip];
int cnt = [unzipFile numFilesInZip];
[unzipFile goToFirstFileInZip];
for (int i = 0; i < cnt; i++) {
FileInZipInfo *info = [unzipFile getCurrentFileInZipInfo];
NSString *name = info.name;
if (![name hasSuffix:@"/"]) {
NSString *filePath = [directory stringByAppendingPathComponent:name];
NSString *basePath = [filePath stringByDeletingLastPathComponent];
if (![[NSFileManager defaultManager] createDirectoryAtPath:basePath withIntermediateDirectories:YES attributes:nil error:error]) {
[unzipFile close];
return NO;
}
[[NSData data] writeToFile:filePath options:0 error:nil];
NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:filePath];
ZipReadStream *read = [unzipFile readCurrentFileInZip];
NSUInteger count;
NSMutableData *data = [NSMutableData dataWithLength:2048];
while ((count = [read readDataWithBuffer:data])) {
data.length = count;
[handle writeData:data];
data.length = 2048;
}
[read finishedReading];
[handle closeFile];
}
[unzipFile goToNextFileInZip];
}
[unzipFile close];
return YES;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.