Skip to content

Commit 7ce0c7c

Browse files
committed
factoring zipInfo:setAttributesOfItemAtPath:
1 parent 708a2c7 commit 7ce0c7c

File tree

1 file changed

+52
-75
lines changed

1 file changed

+52
-75
lines changed

SSZipArchive/SSZipArchive.m

Lines changed: 52 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -715,58 +715,13 @@ - (BOOL)open
715715
return (NULL != _zip);
716716
}
717717

718-
719-
- (void)zipInfo:(zip_fileinfo *)zipInfo setDate:(NSDate *)date
720-
{
721-
NSCalendar *currentCalendar = SSZipArchive._gregorian;
722-
NSCalendarUnit flags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
723-
NSDateComponents *components = [currentCalendar components:flags fromDate:date];
724-
struct tm tmz_date;
725-
tmz_date.tm_sec = (unsigned int)components.second;
726-
tmz_date.tm_min = (unsigned int)components.minute;
727-
tmz_date.tm_hour = (unsigned int)components.hour;
728-
tmz_date.tm_mday = (unsigned int)components.day;
729-
// ISO/IEC 9899 struct tm is 0-indexed for January but NSDateComponents for gregorianCalendar is 1-indexed for January
730-
tmz_date.tm_mon = (unsigned int)components.month - 1;
731-
// ISO/IEC 9899 struct tm is 0-indexed for AD 1900 but NSDateComponents for gregorianCalendar is 1-indexed for AD 1
732-
tmz_date.tm_year = (unsigned int)components.year - 1900;
733-
zipInfo->dos_date = tm_to_dosdate(&tmz_date);
734-
}
735-
736718
- (BOOL)writeFolderAtPath:(NSString *)path withFolderName:(NSString *)folderName withPassword:(nullable NSString *)password
737719
{
738720
NSAssert((_zip != NULL), @"Attempting to write to an archive which was never opened");
739721

740722
zip_fileinfo zipInfo = {};
741723

742-
NSDictionary *attr = [[NSFileManager defaultManager] attributesOfItemAtPath:path error: nil];
743-
if (attr)
744-
{
745-
NSDate *fileDate = (NSDate *)attr[NSFileModificationDate];
746-
if (fileDate)
747-
{
748-
[self zipInfo:&zipInfo setDate: fileDate];
749-
}
750-
751-
// Write permissions into the external attributes, for details on this see here: http://unix.stackexchange.com/a/14727
752-
// Get the permissions value from the files attributes
753-
NSNumber *permissionsValue = (NSNumber *)attr[NSFilePosixPermissions];
754-
if (permissionsValue != nil) {
755-
// Get the short value for the permissions
756-
short permissionsShort = permissionsValue.shortValue;
757-
758-
// Convert this into an octal by adding 010000, 010000 being the flag for a regular file
759-
NSInteger permissionsOctal = 0100000 + permissionsShort;
760-
761-
// Convert this into a long value
762-
uLong permissionsLong = @(permissionsOctal).unsignedLongValue;
763-
764-
// Store this into the external file attributes once it has been shifted 16 places left to form part of the second from last byte
765-
766-
// Casted back to an unsigned int to match type of external_fa in minizip
767-
zipInfo.external_fa = (unsigned int)(permissionsLong << 16L);
768-
}
769-
}
724+
[SSZipArchive zipInfo:&zipInfo setAttributesOfItemAtPath:path];
770725

771726
int error = zipOpenNewFileInZip3(_zip,
772727
[folderName stringByAppendingString:@"/"].fileSystemRepresentation,
@@ -817,34 +772,7 @@ - (BOOL)writeFileAtPath:(NSString *)path withFileName:(nullable NSString *)fileN
817772

818773
zip_fileinfo zipInfo = {};
819774

820-
NSDictionary *attr = [[NSFileManager defaultManager] attributesOfItemAtPath:path error: nil];
821-
if (attr)
822-
{
823-
NSDate *fileDate = (NSDate *)attr[NSFileModificationDate];
824-
if (fileDate)
825-
{
826-
[self zipInfo:&zipInfo setDate: fileDate];
827-
}
828-
829-
// Write permissions into the external attributes, for details on this see here: http://unix.stackexchange.com/a/14727
830-
// Get the permissions value from the files attributes
831-
NSNumber *permissionsValue = (NSNumber *)attr[NSFilePosixPermissions];
832-
if (permissionsValue != nil) {
833-
// Get the short value for the permissions
834-
short permissionsShort = permissionsValue.shortValue;
835-
836-
// Convert this into an octal by adding 010000, 010000 being the flag for a regular file
837-
NSInteger permissionsOctal = 0100000 + permissionsShort;
838-
839-
// Convert this into a long value
840-
uLong permissionsLong = @(permissionsOctal).unsignedLongValue;
841-
842-
// Store this into the external file attributes once it has been shifted 16 places left to form part of the second from last byte
843-
844-
// Casted back to an unsigned int to match type of external_fa in minizip
845-
zipInfo.external_fa = (unsigned int)(permissionsLong << 16L);
846-
}
847-
}
775+
[SSZipArchive zipInfo:&zipInfo setAttributesOfItemAtPath:path];
848776

849777
void *buffer = malloc(CHUNK);
850778
if (buffer == NULL)
@@ -876,7 +804,7 @@ - (BOOL)writeData:(NSData *)data filename:(nullable NSString *)filename withPass
876804
return NO;
877805
}
878806
zip_fileinfo zipInfo = {};
879-
[self zipInfo:&zipInfo setDate:[NSDate date]];
807+
[SSZipArchive zipInfo:&zipInfo setDate:[NSDate date]];
880808

881809
int error = zipOpenNewFileInZip3(_zip, filename.fileSystemRepresentation, &zipInfo, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION, 0, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, password.UTF8String, 0);
882810

@@ -953,6 +881,55 @@ + (NSString *)_temporaryPathForDiscardableFile
953881
return discardableFilePath;
954882
}
955883

884+
+ (void)zipInfo:(zip_fileinfo *)zipInfo setAttributesOfItemAtPath:(NSString *)path
885+
{
886+
NSDictionary *attr = [[NSFileManager defaultManager] attributesOfItemAtPath:path error: nil];
887+
if (attr)
888+
{
889+
NSDate *fileDate = (NSDate *)attr[NSFileModificationDate];
890+
if (fileDate)
891+
{
892+
[self zipInfo:zipInfo setDate:fileDate];
893+
}
894+
895+
// Write permissions into the external attributes, for details on this see here: http://unix.stackexchange.com/a/14727
896+
// Get the permissions value from the files attributes
897+
NSNumber *permissionsValue = (NSNumber *)attr[NSFilePosixPermissions];
898+
if (permissionsValue != nil) {
899+
// Get the short value for the permissions
900+
short permissionsShort = permissionsValue.shortValue;
901+
902+
// Convert this into an octal by adding 010000, 010000 being the flag for a regular file
903+
NSInteger permissionsOctal = 0100000 + permissionsShort;
904+
905+
// Convert this into a long value
906+
uLong permissionsLong = @(permissionsOctal).unsignedLongValue;
907+
908+
// Store this into the external file attributes once it has been shifted 16 places left to form part of the second from last byte
909+
910+
// Casted back to an unsigned int to match type of external_fa in minizip
911+
zipInfo->external_fa = (unsigned int)(permissionsLong << 16L);
912+
}
913+
}
914+
}
915+
916+
+ (void)zipInfo:(zip_fileinfo *)zipInfo setDate:(NSDate *)date
917+
{
918+
NSCalendar *currentCalendar = SSZipArchive._gregorian;
919+
NSCalendarUnit flags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
920+
NSDateComponents *components = [currentCalendar components:flags fromDate:date];
921+
struct tm tmz_date;
922+
tmz_date.tm_sec = (unsigned int)components.second;
923+
tmz_date.tm_min = (unsigned int)components.minute;
924+
tmz_date.tm_hour = (unsigned int)components.hour;
925+
tmz_date.tm_mday = (unsigned int)components.day;
926+
// ISO/IEC 9899 struct tm is 0-indexed for January but NSDateComponents for gregorianCalendar is 1-indexed for January
927+
tmz_date.tm_mon = (unsigned int)components.month - 1;
928+
// ISO/IEC 9899 struct tm is 0-indexed for AD 1900 but NSDateComponents for gregorianCalendar is 1-indexed for AD 1
929+
tmz_date.tm_year = (unsigned int)components.year - 1900;
930+
zipInfo->dos_date = tm_to_dosdate(&tmz_date);
931+
}
932+
956933
+ (NSCalendar *)_gregorian
957934
{
958935
static NSCalendar *gregorian;

0 commit comments

Comments
 (0)