2012-10-22 25 views
0

我在項目中使用了目標zip解壓縮並將文件存儲到文檔目錄。它的工作正常與iOS 5.0及以下版本。使用5.1模擬器很好地工作。但是在5.1設備上工作時,只有少數文件被解壓縮。文件夾中沒有其他顯示。下面是用於解壓縮和存儲的代碼。目標Zip不能與iOS5.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; 

} 

請幫忙! ! !

在此先感謝

+0

我使用[SSZipArchive](https://github.com/soffes/ssziparchive)。 它適用於我的項目! –

回答

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; 
} 
+0

感謝您的解決方案。建議:將此方法的內容封裝在try/catch塊中 - 因爲如果zip文件以某種方式損壞,Objective-Zip將拋出ZipException。 – lifjoy