support password zip

This commit is contained in:
Sen 2015-10-28 20:48:53 +08:00
parent a523114ea1
commit de2c1fe2f3
2 changed files with 24 additions and 23 deletions

View File

@ -35,16 +35,16 @@
progressHandler:(void (^)(NSString *entry, unz_file_info zipInfo, long entryNumber, long total))progressHandler progressHandler:(void (^)(NSString *entry, unz_file_info zipInfo, long entryNumber, long total))progressHandler
completionHandler:(void (^)(NSString *path, BOOL succeeded, NSError *error))completionHandler; completionHandler:(void (^)(NSString *path, BOOL succeeded, NSError *error))completionHandler;
// Zip // Zip, password could be nil
+ (BOOL)createZipFileAtPath:(NSString *)path withFilesAtPaths:(NSArray *)filenames; + (BOOL)createZipFileAtPath:(NSString *)path withFilesAtPaths:(NSArray *)paths withPassword:(NSString *)password;
+ (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath; + (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath withPassword:(NSString *)password;
+ (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath keepParentDirectory:(BOOL)keepParentDirectory; + (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath keepParentDirectory:(BOOL)keepParentDirectory withPassword:(NSString *)password;
- (instancetype)initWithPath:(NSString *)path; - (instancetype)initWithPath:(NSString *)path;
@property (NS_NONATOMIC_IOSONLY, readonly) BOOL open; @property (NS_NONATOMIC_IOSONLY, readonly) BOOL open;
- (BOOL)writeFile:(NSString *)path; - (BOOL)writeFile:(NSString *)path withPassword:(NSString *)password;
- (BOOL)writeFileAtPath:(NSString *)path withFileName:(NSString *)fileName; - (BOOL)writeFileAtPath:(NSString *)path withFileName:(NSString *)fileName withPassword:(NSString *)password;
- (BOOL)writeData:(NSData *)data filename:(NSString *)filename; - (BOOL)writeData:(NSData *)data filename:(NSString *)filename withPassword:(NSString *)password;
@property (NS_NONATOMIC_IOSONLY, readonly) BOOL close; @property (NS_NONATOMIC_IOSONLY, readonly) BOOL close;
@end @end

View File

@ -375,13 +375,13 @@
#pragma mark - Zipping #pragma mark - Zipping
+ (BOOL)createZipFileAtPath:(NSString *)path withFilesAtPaths:(NSArray *)paths + (BOOL)createZipFileAtPath:(NSString *)path withFilesAtPaths:(NSArray *)paths withPassword:(NSString *)password
{ {
BOOL success = NO; BOOL success = NO;
SSZipArchive *zipArchive = [[SSZipArchive alloc] initWithPath:path]; SSZipArchive *zipArchive = [[SSZipArchive alloc] initWithPath:path];
if ([zipArchive open]) { if ([zipArchive open]) {
for (NSString *filePath in paths) { for (NSString *filePath in paths) {
[zipArchive writeFile:filePath]; [zipArchive writeFile:filePath withPassword:password];
} }
success = [zipArchive close]; success = [zipArchive close];
} }
@ -393,12 +393,12 @@
return success; return success;
} }
+ (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath { + (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath withPassword:(NSString *)password{
return [self createZipFileAtPath:path withContentsOfDirectory:directoryPath keepParentDirectory:NO]; return [self createZipFileAtPath:path withContentsOfDirectory:directoryPath keepParentDirectory:NO withPassword:password];
} }
+ (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath keepParentDirectory:(BOOL)keepParentDirectory { + (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath keepParentDirectory:(BOOL)keepParentDirectory withPassword:(NSString *)password{
BOOL success = NO; BOOL success = NO;
NSFileManager *fileManager = nil; NSFileManager *fileManager = nil;
@ -418,7 +418,7 @@
{ {
fileName = [[directoryPath lastPathComponent] stringByAppendingPathComponent:fileName]; fileName = [[directoryPath lastPathComponent] stringByAppendingPathComponent:fileName];
} }
[zipArchive writeFileAtPath:fullFilePath withFileName:fileName]; [zipArchive writeFileAtPath:fullFilePath withFileName:fileName withPassword:password];
} }
else else
{ {
@ -426,7 +426,7 @@
{ {
NSString *tempName = [fullFilePath stringByAppendingPathComponent:@".DS_Store"]; NSString *tempName = [fullFilePath stringByAppendingPathComponent:@".DS_Store"];
[@"" writeToFile:tempName atomically:YES encoding:NSUTF8StringEncoding error:nil]; [@"" writeToFile:tempName atomically:YES encoding:NSUTF8StringEncoding error:nil];
[zipArchive writeFileAtPath:tempName withFileName:[fileName stringByAppendingPathComponent:@".DS_Store"]]; [zipArchive writeFileAtPath:tempName withFileName:[fileName stringByAppendingPathComponent:@".DS_Store"] withPassword:password];
[[NSFileManager defaultManager] removeItemAtPath:tempName error:nil]; [[NSFileManager defaultManager] removeItemAtPath:tempName error:nil];
} }
} }
@ -486,7 +486,7 @@
zipInfo->tmz_date.tm_year = (unsigned int)components.year; zipInfo->tmz_date.tm_year = (unsigned int)components.year;
} }
- (BOOL)writeFolderAtPath:(NSString *)path withFolderName:(NSString *)folderName - (BOOL)writeFolderAtPath:(NSString *)path withFolderName:(NSString *)folderName withPassword:(NSString *)password
{ {
NSAssert((_zip != NULL), @"Attempting to write to an archive which was never opened"); NSAssert((_zip != NULL), @"Attempting to write to an archive which was never opened");
@ -520,21 +520,22 @@
} }
unsigned int len = 0; unsigned int len = 0;
zipOpenNewFileInZip(_zip, [[folderName stringByAppendingString:@"/"] UTF8String], &zipInfo, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_NO_COMPRESSION); zipOpenNewFileInZip3(_zip, [[folderName stringByAppendingString:@"/"] UTF8String], &zipInfo, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_NO_COMPRESSION, 0, -MAX_WBITS, DEF_MEM_LEVEL,
Z_DEFAULT_STRATEGY, [password UTF8String], 0);
zipWriteInFileInZip(_zip, &len, 0); zipWriteInFileInZip(_zip, &len, 0);
zipCloseFileInZip(_zip); zipCloseFileInZip(_zip);
return YES; return YES;
} }
- (BOOL)writeFile:(NSString *)path - (BOOL)writeFile:(NSString *)path withPassword:(NSString *)password;
{ {
return [self writeFileAtPath:path withFileName:nil]; return [self writeFileAtPath:path withFileName:nil withPassword:password];
} }
// supports writing files with logical folder/directory structure // supports writing files with logical folder/directory structure
// *path* is the absolute path of the file that will be compressed // *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 // *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)writeFileAtPath:(NSString *)path withFileName:(NSString *)fileName withPassword:(NSString *)password
{ {
NSAssert((_zip != NULL), @"Attempting to write to an archive which was never opened"); NSAssert((_zip != NULL), @"Attempting to write to an archive which was never opened");
@ -580,7 +581,7 @@
} }
} }
zipOpenNewFileInZip(_zip, afileName, &zipInfo, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION); zipOpenNewFileInZip3(_zip, afileName, &zipInfo, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION, 0, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, [password UTF8String], 0);
void *buffer = malloc(CHUNK); void *buffer = malloc(CHUNK);
unsigned int len = 0; unsigned int len = 0;
@ -597,7 +598,7 @@
return YES; return YES;
} }
- (BOOL)writeData:(NSData *)data filename:(NSString *)filename - (BOOL)writeData:(NSData *)data filename:(NSString *)filename withPassword:(NSString *)password;
{ {
if (!_zip) { if (!_zip) {
return NO; return NO;
@ -608,7 +609,7 @@
zip_fileinfo zipInfo = {{0,0,0,0,0,0},0,0,0}; zip_fileinfo zipInfo = {{0,0,0,0,0,0},0,0,0};
[self zipInfo:&zipInfo setDate:[NSDate date]]; [self zipInfo:&zipInfo setDate:[NSDate date]];
zipOpenNewFileInZip(_zip, [filename UTF8String], &zipInfo, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION); zipOpenNewFileInZip3(_zip, [filename UTF8String], &zipInfo, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION, 0, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, [password UTF8String], 0);
zipWriteInFileInZip(_zip, data.bytes, (unsigned int)data.length); zipWriteInFileInZip(_zip, data.bytes, (unsigned int)data.length);