2012-10-22 3 views
0

파일을 문서 디렉토리에 압축 해제하고 저장하는 프로젝트에서 objective zip을 사용하고 있습니다. iOS 5.0 이하 버전에서 잘 작동합니다. 5.1 Simulator로 잘 작동합니다. 그러나 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

를 사용하여 사전에

감사 목적 지퍼를 사용하여 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

이 솔루션을 이용해 주셔서 감사합니다. 제안 : Zip 파일이 어떤 식 으로든 손상된 경우 Objective-Zip이 ZipException을 throw하기 때문에 try/catch 블록에이 메서드의 내장을 포함하십시오. – lifjoy

관련 문제