Merge pull request #44 from iggycoder/master

zipping folders
This commit is contained in:
Sam Soffes 2013-03-09 09:40:39 -08:00
commit b4fa2e4739
3 changed files with 73 additions and 12 deletions

View File

@ -25,6 +25,7 @@
// Zip // Zip
+ (BOOL)createZipFileAtPath:(NSString *)path withFilesAtPaths:(NSArray *)filenames; + (BOOL)createZipFileAtPath:(NSString *)path withFilesAtPaths:(NSArray *)filenames;
+ (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath;
- (id)initWithPath:(NSString *)path; - (id)initWithPath:(NSString *)path;
- (BOOL)open; - (BOOL)open;

View File

@ -293,6 +293,35 @@
} }
+ (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath {
BOOL success = NO;
SSZipArchive *zipArchive = [[SSZipArchive alloc] initWithPath:path];
if ([zipArchive open]) {
// use a local filemanager (queue/thread compatibility)
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSDirectoryEnumerator *dirEnumerator = [fileManager enumeratorAtPath:directoryPath];
NSString *fileName;
while (fileName = [dirEnumerator nextObject]) {
BOOL isDir;
NSString *fullFilePath = [directoryPath stringByAppendingPathComponent:fileName];
[fileManager fileExistsAtPath:fullFilePath isDirectory:&isDir];
if (!isDir) {
[zipArchive writeFileAtPath:fullFilePath withFileName:fileName];
}
}
success = [zipArchive close];
}
#if !__has_feature(objc_arc)
[zipArchive release];
#endif
return success;
}
- (id)initWithPath:(NSString *)path { - (id)initWithPath:(NSString *)path {
if ((self = [super init])) { if ((self = [super init])) {
_path = [path copy]; _path = [path copy];
@ -329,27 +358,43 @@
} }
- (BOOL)writeFile:(NSString *)path { - (BOOL)writeFile:(NSString *)path
NSAssert((_zip != NULL), @"Attempting to write to an archive which was never opened"); {
return [self writeFileAtPath:path withFileName:nil];
}
// supports writing files with logical folder/directory structure
// *path* is the absolute path of the file that will be compressed
// *fileName* is the relative name of the file how it is stored within the zip e.g. /folder/subfolder/text1.txt
- (BOOL)writeFileAtPath:(NSString *)path withFileName:(NSString *)fileName {
NSAssert((_zip != NULL), @"Attempting to write to an archive which was never opened");
FILE *input = fopen([path UTF8String], "r"); FILE *input = fopen([path UTF8String], "r");
if (NULL == input) { if (NULL == input) {
return NO; return NO;
} }
zipOpenNewFileInZip(_zip, [[path lastPathComponent] UTF8String], NULL, NULL, 0, NULL, 0, NULL, Z_DEFLATED, const char *afileName;
Z_DEFAULT_COMPRESSION); if (!fileName) {
afileName = [path.lastPathComponent UTF8String];
}
else {
afileName = [fileName UTF8String];
}
zipOpenNewFileInZip(_zip, afileName, NULL, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION);
void *buffer = malloc(CHUNK); void *buffer = malloc(CHUNK);
unsigned int len = 0; unsigned int len = 0;
while (!feof(input)) {
while (!feof(input))
{
len = (unsigned int) fread(buffer, 1, CHUNK, input); len = (unsigned int) fread(buffer, 1, CHUNK, input);
zipWriteInFileInZip(_zip, buffer, len); zipWriteInFileInZip(_zip, buffer, len);
} }
zipCloseFileInZip(_zip); zipCloseFileInZip(_zip);
free(buffer); free(buffer);
fclose(input);
return YES; return YES;
} }

View File

@ -26,11 +26,15 @@
- (void)testZipping { - (void)testZipping {
NSString *outputPath = [self _cachesPath:@"Zipped"]; // use extracted files from [-testUnzipping]
NSString *inputPath = [self _cachesPath:@"Regular"];
NSArray *inputPaths = [NSArray arrayWithObjects: NSArray *inputPaths = [NSArray arrayWithObjects:
[outputPath stringByAppendingPathComponent:@"Readme.markdown"], [inputPath stringByAppendingPathComponent:@"Readme.markdown"],
[outputPath stringByAppendingPathComponent:@"LICENSE"], [inputPath stringByAppendingPathComponent:@"LICENSE"],
nil]; nil];
NSString *outputPath = [self _cachesPath:@"Zipped"];
NSString *archivePath = [outputPath stringByAppendingPathComponent:@"CreatedArchive.zip"]; NSString *archivePath = [outputPath stringByAppendingPathComponent:@"CreatedArchive.zip"];
[SSZipArchive createZipFileAtPath:archivePath withFilesAtPaths:inputPaths]; [SSZipArchive createZipFileAtPath:archivePath withFilesAtPaths:inputPaths];
@ -38,6 +42,17 @@
STAssertTrue([[NSFileManager defaultManager] fileExistsAtPath:archivePath], @"Archive created"); STAssertTrue([[NSFileManager defaultManager] fileExistsAtPath:archivePath], @"Archive created");
} }
- (void)testDirectoryZipping {
// use Unicode as folder (has a file in root and a file in subfolder)
NSString *inputPath = [self _cachesPath:@"Unicode"];
NSString *outputPath = [self _cachesPath:@"FolderZipped"];
NSString *archivePath = [outputPath stringByAppendingPathComponent:@"ArchiveWithFolders.zip"];
[SSZipArchive createZipFileAtPath:archivePath withContentsOfDirectory:inputPath];
STAssertTrue([[NSFileManager defaultManager] fileExistsAtPath:archivePath], @"Folder Archive created");
}
- (void)testUnzipping { - (void)testUnzipping {
NSString *zipPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"TestArchive" ofType:@"zip"]; NSString *zipPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"TestArchive" ofType:@"zip"];