Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize "locateFileInZip" #2

Merged
merged 1 commit into from May 16, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Objective-Zip/ZipFile.h
Expand Up @@ -57,6 +57,7 @@ typedef enum {
@interface ZipFile : NSObject {
NSString *_fileName;
ZipFileMode _mode;
NSDictionary *contents;

@private
zipFile _zipFile;
Expand Down
36 changes: 33 additions & 3 deletions Objective-Zip/ZipFile.m
Expand Up @@ -39,7 +39,6 @@

#define FILE_IN_ZIP_MAX_NAME_LENGTH (256)


@implementation ZipFile


Expand All @@ -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:
Expand Down Expand Up @@ -85,6 +103,7 @@ - (id) initWithFileName:(NSString *)fileName mode:(ZipFileMode)mode {

- (void) dealloc {
[_fileName release];
[contents release];
[super dealloc];
}

Expand Down Expand Up @@ -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) {
Expand Down