Permalink
Browse files

Merge pull request #2 from ANNotunzdY/master

Optimize "locateFileInZip"
  • Loading branch information...
2 parents 35aac64 + 2b1cbf4 commit 036235ac38ad0eb8126a8f6cf3bb537c94068fd8 @roustem roustem committed May 16, 2012
Showing with 34 additions and 3 deletions.
  1. +1 −0 Objective-Zip/ZipFile.h
  2. +33 −3 Objective-Zip/ZipFile.m
@@ -57,6 +57,7 @@ typedef enum {
@interface ZipFile : NSObject {
NSString *_fileName;
ZipFileMode _mode;
+ NSDictionary *contents;
@private
zipFile _zipFile;
@@ -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) {

0 comments on commit 036235a

Please sign in to comment.