Support for adding folders and subfolders to ZIP packages.

This commit is contained in:
Bohdan Hernandez 2014-02-09 02:48:44 +00:00
parent d3c40f0e95
commit de0ffe7478
3 changed files with 52 additions and 6 deletions

View File

@ -306,7 +306,6 @@
return success;
}
+ (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath {
BOOL success = NO;
@ -314,18 +313,27 @@
SSZipArchive *zipArchive = [[SSZipArchive alloc] initWithPath:path];
if ([zipArchive open]) {
// use a local filemanager (queue/thread compatibility)
fileManager = [[NSFileManager alloc] init];
NSDirectoryEnumerator *dirEnumerator = [fileManager enumeratorAtPath:directoryPath];
NSString *directoryName = [directoryPath lastPathComponent];
NSString *fileName;
[zipArchive writeFolderAtPath:directoryPath withFolderName:directoryName];
while ((fileName = [dirEnumerator nextObject])) {
BOOL isDir;
NSString *fullFilePath = [directoryPath stringByAppendingPathComponent:fileName];
[fileManager fileExistsAtPath:fullFilePath isDirectory:&isDir];
if (!isDir) {
[zipArchive writeFileAtPath:fullFilePath withFileName:fileName];
[zipArchive writeFileAtPath:fullFilePath withFileName:[directoryName stringByAppendingPathComponent:fileName]];
}
else
{
[zipArchive writeFolderAtPath:fullFilePath withFolderName:[directoryName stringByAppendingPathComponent:fileName]];
}
}
success = [zipArchive close];
}
@ -374,6 +382,45 @@
zipInfo->tmz_date.tm_year = (unsigned int)components.year;
}
- (BOOL)writeFolderAtPath:(NSString *)path withFolderName:(NSString *)folderName
{
NSAssert((_zip != NULL), @"Attempting to write to an archive which was never opened");
zip_fileinfo zipInfo = {{0}};
NSDictionary *attr = [[NSFileManager defaultManager] attributesOfItemAtPath:path error: nil];
if( attr )
{
NSDate *fileDate = (NSDate *)[attr objectForKey:NSFileModificationDate];
if( fileDate )
{
[self zipInfo:&zipInfo setDate: fileDate ];
}
// Write permissions into the external attributes, for details on this see here: http://unix.stackexchange.com/a/14727
// Get the permissions value from the files attributes
NSNumber *permissionsValue = (NSNumber *)[attr objectForKey:NSFilePosixPermissions];
if (permissionsValue) {
// Get the short value for the permissions
short permissionsShort = permissionsValue.shortValue;
// Convert this into an octal by adding 010000, 010000 being the flag for a regular file
NSInteger permissionsOctal = 0100000 + permissionsShort;
// Convert this into a long value
uLong permissionsLong = @(permissionsOctal).unsignedLongValue;
// Store this into the external file attributes once it has been shifted 16 places left to form part of the second from last byte
zipInfo.external_fa = permissionsLong << 16L;
}
}
unsigned int len = 0;
zipOpenNewFileInZip(_zip, [[folderName stringByAppendingString:@"/"] UTF8String], &zipInfo, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_NO_COMPRESSION);
zipWriteInFileInZip(_zip, &len, 0);
zipCloseFileInZip(_zip);
return YES;
}
- (BOOL)writeFile:(NSString *)path
{

View File

@ -192,9 +192,11 @@ static long ZCALLBACK fseek64_file_func (voidpf opaque, voidpf stream, ZPOS64_T
}
ret = 0;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wconversion"
if(fseeko64((FILE *)stream, offset, fseek_origin) != 0)
ret = -1;
#pragma clang diagnostic pop
return ret;
}

View File

@ -1554,10 +1554,7 @@ extern int ZEXPORT unzOpenCurrentFile3 (unzFile file, int* method,
pfile_in_zip_read_info->compression_method = s->cur_file_info.compression_method;
pfile_in_zip_read_info->filestream=s->filestream;
pfile_in_zip_read_info->z_filefunc=s->z_filefunc;
#ifndef __clang_analyzer__
pfile_in_zip_read_info->byte_before_the_zipfile=s->byte_before_the_zipfile;
#endif
pfile_in_zip_read_info->stream.total_out = 0;
if ((s->cur_file_info.compression_method==Z_BZIP2ED) && (!raw))