diff --git a/SSZipArchive/SSZipArchive.h b/SSZipArchive/SSZipArchive.h index 9546b77..a30dee8 100644 --- a/SSZipArchive/SSZipArchive.h +++ b/SSZipArchive/SSZipArchive.h @@ -25,12 +25,20 @@ // Zip + (BOOL)createZipFileAtPath:(NSString *)path withFilesAtPaths:(NSArray *)filenames; ++ (BOOL)createZipFileAtPath:(NSString *)path withFilesAtPaths:(NSArray *)filenames inFolder:(NSString*)folder; + (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath; ++ (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath inFolder:(NSString*)folder; + - (id)initWithPath:(NSString *)path; - (BOOL)open; - (BOOL)writeFile:(NSString *)path; - (BOOL)writeData:(NSData *)data filename:(NSString *)filename; + +// 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; - (BOOL)close; @end diff --git a/SSZipArchive/SSZipArchive.m b/SSZipArchive/SSZipArchive.m index 4c337da..6721757 100644 --- a/SSZipArchive/SSZipArchive.m +++ b/SSZipArchive/SSZipArchive.m @@ -320,8 +320,30 @@ return success; } ++ (BOOL)createZipFileAtPath:(NSString *)path withFilesAtPaths:(NSArray *)paths inFolder:(NSString *)folder { + BOOL success = NO; + SSZipArchive *zipArchive = [[SSZipArchive alloc] initWithPath:path]; + if ([zipArchive open]) { + for (NSString *path in paths) { + if (!folder) { + [zipArchive writeFile:path]; + } + else { + NSString *destination = [folder stringByAppendingPathComponent:path.lastPathComponent]; + [zipArchive writeFileAtPath:path withFileName:destination]; + } + } + success = [zipArchive close]; + } -+ (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath { +#if !__has_feature(objc_arc) + [zipArchive release]; +#endif + + return success; +} + ++ (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath inFolder:(NSString *)folder { BOOL success = NO; NSFileManager *fileManager = nil; @@ -336,6 +358,9 @@ while ((fileName = [dirEnumerator nextObject])) { BOOL isDir; NSString *fullFilePath = [directoryPath stringByAppendingPathComponent:fileName]; + if (folder) { + fileName = [folder stringByAppendingPathComponent: fileName]; + } [fileManager fileExistsAtPath:fullFilePath isDirectory:&isDir]; if (!isDir) { [zipArchive writeFileAtPath:fullFilePath withFileName:fileName]; @@ -352,6 +377,9 @@ return success; } ++ (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath { + return [SSZipArchive createZipFileAtPath:path withContentsOfDirectory:directoryPath inFolder:nil]; +} - (id)initWithPath:(NSString *)path { if ((self = [super init])) {