3

ファイルを抽出するためにobjectivezipというライブラリを使用しています。

関連するコード:

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];


ZipFile *unzipFile= [[ZipFile alloc] initWithFileName:requestFinishedPresentationPath mode:ZipFileModeUnzip];
NSArray *infos= [unzipFile listFileInZipInfos];


for (FileInZipInfo *info in infos) {
    NSLog(@"- %@ %@ %d (%d)", info.name, info.date, info.size, info.level);

    [unzipFile locateFileInZip:info.name];

    // Expand the file in memory
    ZipReadStream *read= [unzipFile readCurrentFileInZip];

    NSMutableData *data = [[NSMutableData alloc] initWithLength:256];
    int bytesRead = [read readDataWithBuffer:data];



    [data writeToFile:[documentsDir stringByAppendingPathComponent:info.name] atomically:NO];
    NSLog([documentsDir stringByAppendingPathComponent:info.name]);
    [read finishedReading];


}

[unzipFile close];

最初のnslogは次を出力します。

- _some-path_/modules/reference-popup/ref-popup.js 2012-08-21 08:49:36 +0000 109 (-1)

2番目のnslogは、次を出力します。

/Users/_USER_/Library/Application Support/iPhone Simulator/5.1/Applications/F2649DB7-7806-4C49-8DF9-BD939B2A9D5A/Documents/_some-path_/modules/slide-popup/slide-popup.css

基本的に、読み取りオブジェクトとデータオブジェクトは相互に通信していないと思います...ブラウザで適切なディレクトリに移動すると、ディレクトリであるはずの2つのファイルが表示されます(これらは最も上位にあります-レベルのディレクトリ)が、ブラウザはそれらが1kbのファイルとして書き出されたと言っています。コード内のバッファに従って、ファイルとその256バイトのファイルを表示してみます。

代わりに何をすべきですか?

zipを展開するには(すべてのファイルを適切にディレクトリに入れます)?

4

1 に答える 1

10

readDataWithBufferを呼び出すと、バッファーがいっぱいになり(その長さまで)、ストリームが一時停止します。実際にはループに入れて、bytesReadが0のときに停止する必要があります。これは、使用可能なメモリよりも大きいファイルを拡張できるようにするためです(たとえば、ビデオを考えてみてください)。

また、zipファイルにはディレクトリ構造がないことを考慮してください。ディレクトリはzipファイル名の一部として埋め込まれているだけです。正しいパスを再作成するのはあなた次第です。通常、いくつかのベースディレクトリがあり、zipファイル名を追加して、結果の完全なパスを使用してファイルを作成します。

Objective-Zip wikiの最後に、サンプルループが存在してコメントされているMemoryManagementというタイトルのセクションがあります。より完成された例は次のとおりです。

    // Open zip descriptor
    ZipFile *zip= [[ZipFile alloc] initWithFileName:zipPath mode:ZipFileModeUnzip];

    NSMutableData *buffer= [[NSMutableData alloc] initWithLength:BUFFER_SIZE];

    // Loop on file list
    NSArray *zipContentList= [zip listFileInZipInfos];
    for (FileInZipInfo *fileInZipInfo in zipContentList) {

        // Check if it's a directory
        if ([fileInZipInfo.name hasSuffix:@"/"]) {
            NSString *dirPath= [documentsPath stringByAppendingPathComponent:fileInZipInfo.name];
            [[NSFileManager defaultManager] createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:NULL];
            continue;
        }

        // Create file
        NSString *filePath= [documentsPath stringByAppendingPathComponent:fileInZipInfo.name];
        [[NSFileManager defaultManager] createFileAtPath:filePath contents:[NSData data] attributes:nil];
        NSFileHandle *file= [NSFileHandle fileHandleForWritingAtPath:filePath];

        // Seek file in zip 
        [zip locateFileInZip:fileInZipInfo.name];
        ZipReadStream *readStream= [zip readCurrentFileInZip];

        // Reset buffer
        [buffer setLength:BUFFER_SIZE];

        // Loop on read stream
        int totalBytesRead= 0;
        do {
            int bytesRead= [readStream readDataWithBuffer:buffer];
            if (bytesRead > 0) {

                // Write data
                [buffer setLength:bytesRead];
                [file writeData:buffer];

                totalBytesRead += bytesRead;

            } else
                break;

        } while (YES);

        // Close file
        [file closeFile];
        [readStream finishedReading];
    }

    // Close zip and release buffer
    [buffer release];
    [zip close];
    [zip release];

お役に立てれば。

于 2012-09-29T21:45:55.087 に答える