目标Zip无法使用iOS5.1设备,未正确解压缩

Abh*_*man 0 objective-c ios ios5

我在项目中使用目标zip来解压缩并将文件存储到文档目录.它适用于iOS 5.0及更低版本.使用5.1模拟器正常工作.但是在使用5.1设备时,只有少量文件被解压缩.文件夹中没有显示任何其他内容.下面是用于解压缩和存储的代码.

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;

}
Run Code Online (Sandbox Code Playgroud)

请帮忙 !!!

提前致谢

rma*_*ddy 5

使用具有以下方法使用Objective-zip解压缩zip文件.这在iOS 4.3到6.0(也可能更早和更晚)下工作正常.'filename'参数是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;
}
Run Code Online (Sandbox Code Playgroud)