2010-07-22 07:05:38 +08:00
|
|
|
//
|
2010-08-20 04:27:48 +08:00
|
|
|
// SSZipArchive.m
|
|
|
|
// SSZipArchive
|
2010-07-22 07:05:38 +08:00
|
|
|
//
|
|
|
|
// Created by Sam Soffes on 7/21/10.
|
2010-08-20 04:27:48 +08:00
|
|
|
// Copyright Sam Soffes 2010. All rights reserved.
|
2010-07-22 07:05:38 +08:00
|
|
|
//
|
|
|
|
|
2010-08-20 04:27:48 +08:00
|
|
|
#import "SSZipArchive.h"
|
2010-07-22 07:05:38 +08:00
|
|
|
#include "minizip/zip.h"
|
|
|
|
#include "minizip/unzip.h"
|
|
|
|
#import "zlib.h"
|
|
|
|
#import "zconf.h"
|
|
|
|
|
2010-08-20 04:27:48 +08:00
|
|
|
@interface SSZipArchive (PrivateMethods)
|
2010-07-22 07:05:38 +08:00
|
|
|
+ (NSDate *)_dateFor1980;
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
2010-08-20 04:27:48 +08:00
|
|
|
@implementation SSZipArchive
|
2010-07-22 07:05:38 +08:00
|
|
|
|
|
|
|
+ (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination {
|
2010-07-23 23:41:46 +08:00
|
|
|
return [self unzipFileAtPath:path toDestination:destination overwrite:YES password:nil error:nil];
|
2010-07-22 07:05:38 +08:00
|
|
|
}
|
|
|
|
|
2010-07-23 23:41:46 +08:00
|
|
|
+ (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination overwrite:(BOOL)overwrite password:(NSString *)password error:(NSError **)error {
|
2010-07-22 07:05:38 +08:00
|
|
|
// Begin opening
|
|
|
|
zipFile zip = unzOpen((const char*)[path UTF8String]);
|
|
|
|
if (zip == NULL) {
|
2010-07-23 23:41:46 +08:00
|
|
|
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"failed to open zip file" forKey:NSLocalizedDescriptionKey];
|
2010-07-27 04:18:32 +08:00
|
|
|
if (error) {
|
2010-08-20 04:27:48 +08:00
|
|
|
*error = [NSError errorWithDomain:@"SSZipArchiveErrorDomain" code:-1 userInfo:userInfo];
|
2010-07-27 04:18:32 +08:00
|
|
|
}
|
2010-07-22 07:05:38 +08:00
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2011-07-22 06:39:03 +08:00
|
|
|
unz_global_info globalInfo = {0ul, 0ul};
|
2010-07-23 23:41:46 +08:00
|
|
|
unzGetGlobalInfo(zip, &globalInfo);
|
2010-07-22 07:05:38 +08:00
|
|
|
|
|
|
|
// Begin unzipping
|
|
|
|
if (unzGoToFirstFile(zip) != UNZ_OK) {
|
2010-07-23 23:41:46 +08:00
|
|
|
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"failed to open first file in zip file" forKey:NSLocalizedDescriptionKey];
|
2010-07-27 04:18:32 +08:00
|
|
|
if (error) {
|
2010-08-20 04:27:48 +08:00
|
|
|
*error = [NSError errorWithDomain:@"SSZipArchiveErrorDomain" code:-2 userInfo:userInfo];
|
2010-07-27 04:18:32 +08:00
|
|
|
}
|
2010-07-22 07:05:38 +08:00
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL success = YES;
|
|
|
|
int ret;
|
|
|
|
unsigned char buffer[4096] = {0};
|
|
|
|
NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
|
|
NSDate *nineteenEighty = [self _dateFor1980];
|
|
|
|
|
|
|
|
do {
|
|
|
|
if ([password length] == 0) {
|
|
|
|
ret = unzOpenCurrentFile(zip);
|
|
|
|
} else {
|
|
|
|
ret = unzOpenCurrentFilePassword(zip, [password cStringUsingEncoding:NSASCIIStringEncoding]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret != UNZ_OK) {
|
|
|
|
success = NO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reading data and write to file
|
2011-07-22 06:39:03 +08:00
|
|
|
unz_file_info fileInfo;
|
|
|
|
memset(&fileInfo, 0, sizeof(unz_file_info));
|
|
|
|
|
2010-07-22 07:05:38 +08:00
|
|
|
ret = unzGetCurrentFileInfo(zip, &fileInfo, NULL, 0, NULL, 0, NULL, 0);
|
|
|
|
if (ret != UNZ_OK) {
|
|
|
|
success = NO;
|
|
|
|
unzCloseCurrentFile(zip);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *filename = (char *)malloc(fileInfo.size_filename + 1);
|
|
|
|
unzGetCurrentFileInfo(zip, &fileInfo, filename, fileInfo.size_filename + 1, NULL, 0, NULL, 0);
|
|
|
|
filename[fileInfo.size_filename] = '\0';
|
|
|
|
|
|
|
|
// Check if it contains directory
|
|
|
|
NSString *strPath = [NSString stringWithCString:filename encoding:NSUTF8StringEncoding];
|
|
|
|
BOOL isDirectory = NO;
|
|
|
|
if (filename[fileInfo.size_filename-1] == '/' || filename[fileInfo.size_filename-1] == '\\') {
|
|
|
|
isDirectory = YES;
|
|
|
|
}
|
|
|
|
free(filename);
|
|
|
|
|
|
|
|
// Contains a path
|
|
|
|
if ([strPath rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"/\\"]].location != NSNotFound) {
|
|
|
|
strPath = [strPath stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
|
|
|
|
}
|
|
|
|
|
|
|
|
NSString* fullPath = [destination stringByAppendingPathComponent:strPath];
|
|
|
|
|
|
|
|
if (isDirectory) {
|
|
|
|
[fileManager createDirectoryAtPath:fullPath withIntermediateDirectories:YES attributes:nil error:nil];
|
|
|
|
} else {
|
|
|
|
[fileManager createDirectoryAtPath:[fullPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([fileManager fileExistsAtPath:fullPath] && !isDirectory && !overwrite) {
|
|
|
|
unzCloseCurrentFile(zip);
|
|
|
|
ret = unzGoToNextFile(zip);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *fp = fopen((const char*)[fullPath UTF8String], "wb");
|
|
|
|
while (fp) {
|
2011-07-22 12:52:14 +08:00
|
|
|
int readBytes = unzReadCurrentFile(zip, buffer, 4096);
|
2010-07-22 07:05:38 +08:00
|
|
|
|
2011-07-22 12:52:14 +08:00
|
|
|
if (readBytes > 0) {
|
|
|
|
fwrite(buffer, readBytes, 1, fp );
|
2010-07-22 07:05:38 +08:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fp) {
|
|
|
|
fclose(fp);
|
|
|
|
|
2011-05-28 07:34:05 +08:00
|
|
|
// Set the original datetime property
|
2010-07-22 07:05:38 +08:00
|
|
|
if (fileInfo.dosDate != 0) {
|
|
|
|
NSDate *orgDate = [[NSDate alloc] initWithTimeInterval:(NSTimeInterval)fileInfo.dosDate sinceDate:nineteenEighty];
|
|
|
|
NSDictionary *attr = [NSDictionary dictionaryWithObject:orgDate forKey:NSFileModificationDate];
|
|
|
|
|
|
|
|
if (attr) {
|
|
|
|
if ([fileManager setAttributes:attr ofItemAtPath:fullPath error:nil] == NO) {
|
|
|
|
// Can't set attributes
|
|
|
|
NSLog(@"Failed to set attributes");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
[orgDate release];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unzCloseCurrentFile( zip );
|
|
|
|
ret = unzGoToNextFile( zip );
|
|
|
|
} while(ret == UNZ_OK && UNZ_OK != UNZ_END_OF_LIST_OF_FILE);
|
|
|
|
|
|
|
|
// Close
|
|
|
|
unzClose(zip);
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+ (NSDate *)_dateFor1980 {
|
|
|
|
NSDateComponents *comps = [[NSDateComponents alloc] init];
|
|
|
|
[comps setDay:1];
|
|
|
|
[comps setMonth:1];
|
|
|
|
[comps setYear:1980];
|
|
|
|
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
|
|
|
|
NSDate *date = [gregorian dateFromComponents:comps];
|
|
|
|
|
|
|
|
[comps release];
|
|
|
|
[gregorian release];
|
|
|
|
return date;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|