From 2b1cbf4fc1e21add53c689bb5f04f2fb73355765 Mon Sep 17 00:00:00 2001 From: Takayoshi Sato Date: Wed, 16 May 2012 12:22:00 +0900 Subject: [PATCH] Optimize locate file. --- Objective-Zip/ZipFile.h | 1 + Objective-Zip/ZipFile.m | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/Objective-Zip/ZipFile.h b/Objective-Zip/ZipFile.h index 79642fd..3d04da3 100644 --- a/Objective-Zip/ZipFile.h +++ b/Objective-Zip/ZipFile.h @@ -57,6 +57,7 @@ typedef enum { @interface ZipFile : NSObject { NSString *_fileName; ZipFileMode _mode; + NSDictionary *contents; @private zipFile _zipFile; diff --git a/Objective-Zip/ZipFile.m b/Objective-Zip/ZipFile.m index 8f285df..f0a37e9 100644 --- a/Objective-Zip/ZipFile.m +++ b/Objective-Zip/ZipFile.m @@ -39,7 +39,6 @@ #define FILE_IN_ZIP_MAX_NAME_LENGTH (256) - @implementation ZipFile @@ -55,6 +54,25 @@ - (id) initWithFileName:(NSString *)fileName mode:(ZipFileMode)mode { NSString *reason= [NSString stringWithFormat:@"Can't open '%@'", _fileName]; @throw [[[ZipException alloc] initWithReason:reason] autorelease]; } + + unzGoToFirstFile(_unzFile); + + NSMutableDictionary* dic = [NSMutableDictionary dictionary]; + + do { + FileInZipInfo* info = [self getCurrentFileInZipInfo]; + unz_file_pos pos; + int err = unzGetFilePos(_unzFile, &pos); + if (err == UNZ_OK) { + [dic setObject:[NSArray arrayWithObjects: + [NSNumber numberWithLong:pos.pos_in_zip_directory], + [NSNumber numberWithLong:pos.num_of_file], + nil] forKey:info.name]; + } + } while (unzGoToNextFile (_unzFile) != UNZ_END_OF_LIST_OF_FILE); + + contents = [dic retain]; + break; case ZipFileModeCreate: @@ -85,6 +103,7 @@ - (id) initWithFileName:(NSString *)fileName mode:(ZipFileMode)mode { - (void) dealloc { [_fileName release]; + [contents release]; [super dealloc]; } @@ -269,8 +288,19 @@ - (BOOL) locateFileInZip:(NSString *)fileNameInZip { @throw [[[ZipException alloc] initWithReason:reason] autorelease]; } - int err= unzLocateFile(_unzFile, [fileNameInZip cStringUsingEncoding:NSUTF8StringEncoding], 1); - if (err == UNZ_END_OF_LIST_OF_FILE) + //int err= unzLocateFile(_unzFile, [fileNameInZip cStringUsingEncoding:NSUTF8StringEncoding], 1); + + NSArray* info = [contents objectForKey:fileNameInZip]; + + if (!info) return NO; + + unz_file_pos pos; + pos.pos_in_zip_directory = [[info objectAtIndex:0] longValue]; + pos.num_of_file = [[info objectAtIndex:1] longValue]; + + int err = unzGoToFilePos(_unzFile, &pos); + + if (err == UNZ_END_OF_LIST_OF_FILE) return NO; if (err != UNZ_OK) {