Skip to content

Commit 23426fd

Browse files
committed
Fixes for unzipping modification dates and add ARC support
* Add ARC support * Fixes: One of the bugs is that the file modification dates for files being unzipped were wrong. Apparently the code was taking the MS-DOS date and time and treating it as an NSTimeInterval since Jan 1, 1980. This is not correct and was resulting in modification dates for me in 2014. — Brant Sears * Fixes: The second bug was that the file modification dates for folders were showing up as the current date/time, but when I unzip the same archive using Apple's implementation of zip, the file modification dates for those folders were set sometime in the past. — Brant Sears
1 parent f78aa2c commit 23426fd

File tree

3 files changed

+55
-13
lines changed

3 files changed

+55
-13
lines changed

SSZipArchive.h

+14-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
//
88

99
#import <Foundation/Foundation.h>
10-
#import "SSZipArchiveDelegate.h"
10+
#include "minizip/unzip.h"
11+
12+
@protocol SSZipArchiveDelegate;
1113

1214
@interface SSZipArchive : NSObject
1315

@@ -28,3 +30,14 @@
2830
- (BOOL)close;
2931

3032
@end
33+
34+
35+
@protocol SSZipArchiveDelegate <NSObject>
36+
37+
@optional
38+
39+
- (void)zipArchiveWillUnzipArchiveAtPath:(NSString *)path globalInfo:(unz_global_info)globalInfo;
40+
- (void)zipArchiveWillUnzipFileAtIndex:(NSInteger)fileIndex totalFiles:(NSInteger)totalFiles archivePath:(NSString *)archivePath fileInfo:(unz_file_info)fileInfo;
41+
- (void)zipArchiveDidUnzipFileAtIndex:(NSInteger)fileIndex totalFiles:(NSInteger)totalFiles archivePath:(NSString *)archivePath fileInfo:(unz_file_info)fileInfo;
42+
43+
@end

SSZipArchive.m

+21-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#import "SSZipArchive.h"
1010
#include "minizip/zip.h"
11-
#include "minizip/unzip.h"
1211
#import "zlib.h"
1312
#import "zconf.h"
1413

@@ -67,13 +66,19 @@ + (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination o
6766
}
6867

6968
BOOL success = YES;
70-
int ret;
69+
int ret = 0;
7170
unsigned char buffer[4096] = {0};
7271
NSFileManager *fileManager = [NSFileManager defaultManager];
7372
NSMutableSet *directoriesModificationDates = [[NSMutableSet alloc] init];
7473
if([delegate respondsToSelector:@selector(zipArchiveWillUnzipFile:globalInfo:)])
7574
[delegate zipArchiveWillUnzipFile:path globalInfo:globalInfo];
7675

76+
// Message delegate
77+
if ([delegate respondsToSelector:@selector(zipArchiveWillUnzipArchiveAtPath:globalInfo:)]) {
78+
[delegate zipArchiveWillUnzipArchiveAtPath:path globalInfo:globalInfo];
79+
}
80+
81+
NSMutableSet *directoriesModificationDates = [[NSMutableSet alloc] init];
7782
NSInteger currentFileNumber = 0;
7883
do {
7984
if ([password length] == 0) {
@@ -98,9 +103,11 @@ + (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination o
98103
break;
99104
}
100105

101-
if([delegate respondsToSelector:@selector(zipArchiveWillUnzipFileNumber:outOf:fromFile:fileInfo:)])
102-
[delegate zipArchiveWillUnzipFileNumber:currentFileNumber outOf:(NSInteger)globalInfo.number_entry
103-
fromFile:path fileInfo:fileInfo];
106+
// Message delegate
107+
if ([delegate respondsToSelector:@selector(zipArchiveWillUnzipFileAtIndex:totalFiles:archivePath:fileInfo:)]) {
108+
[delegate zipArchiveWillUnzipFileAtIndex:currentFileNumber totalFiles:(NSInteger)globalInfo.number_entry
109+
archivePath:path fileInfo:fileInfo];
110+
}
104111

105112
char *filename = (char *)malloc(fileInfo.size_filename + 1);
106113
unzGetCurrentFileInfo(zip, &fileInfo, filename, fileInfo.size_filename + 1, NULL, 0, NULL, 0);
@@ -132,10 +139,9 @@ + (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination o
132139
if (nil != err) {
133140
NSLog(@"[SSZipArchive] Error: %@", err.localizedDescription);
134141
}
135-
142+
136143
[directoriesModificationDates addObject: [NSDictionary dictionaryWithObjectsAndKeys:fullPath, @"path", modDate, @"modDate", nil]];
137-
138-
144+
139145
if ([fileManager fileExistsAtPath:fullPath] && !isDirectory && !overwrite) {
140146
unzCloseCurrentFile(zip);
141147
ret = unzGoToNextFile(zip);
@@ -173,6 +179,12 @@ + (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination o
173179
unzCloseCurrentFile( zip );
174180
ret = unzGoToNextFile( zip );
175181

182+
// Message delegate
183+
if ([delegate respondsToSelector:@selector(zipArchiveDidUnzipFileAtIndex:totalFiles:archivePath:fileInfo:)]) {
184+
[delegate zipArchiveDidUnzipFileAtIndex:currentFileNumber totalFiles:(NSInteger)globalInfo.number_entry
185+
archivePath:path fileInfo:fileInfo];
186+
}
187+
176188
currentFileNumber++;
177189
} while(ret == UNZ_OK && UNZ_OK != UNZ_END_OF_LIST_OF_FILE);
178190

@@ -325,7 +337,7 @@ + (NSDate *)_dateWithMSDOSFormat:(UInt32)msdosDateTime {
325337

326338
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
327339
NSDateComponents *components = [[NSDateComponents alloc] init];
328-
340+
329341
NSAssert(0xFFFFFFFF == (kYearMask | kMonthMask | kDayMask | kHourMask | kMinuteMask | kSecondMask), @"[SSZipArchive] MSDOS date masks don't add up");
330342

331343
[components setYear:1980 + ((msdosDateTime & kYearMask) >> 25)];

Tests/SSZipArchiveTests.m

+20-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#import "SSZipArchiveTests.h"
1010
#import "SSZipArchive.h"
1111

12-
@interface SSZipArchiveTests ()
12+
@interface SSZipArchiveTests () <SSZipArchiveDelegate>
1313
- (NSString *)_cachesPath;
1414
@end
1515

@@ -33,9 +33,9 @@ - (void)testUnzipping {
3333
NSString *zipPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"TestArchive" ofType:@"zip"];
3434
NSString *outputPath = [self _cachesPath];
3535

36-
[SSZipArchive unzipFileAtPath:zipPath toDestination:outputPath];
36+
[SSZipArchive unzipFileAtPath:zipPath toDestination:outputPath delegate:self];
3737

38-
NSFileManager *fileManager = [NSFileManager defaultManager];
38+
NSFileManager *fileManager = [NSFileManager defaultManager];
3939
NSString *testPath = [outputPath stringByAppendingPathComponent:@"Readme.markdown"];
4040
STAssertTrue([fileManager fileExistsAtPath:testPath], @"Readme unzipped");
4141

@@ -50,6 +50,23 @@ - (void)testUnzipping {
5050
}
5151

5252

53+
#pragma mark - Delegate
54+
55+
- (void)zipArchiveWillUnzipArchiveAtPath:(NSString *)path globalInfo:(unz_global_info)globalInfo {
56+
NSLog(@"zipArchiveWillUnzipArchiveAtPath: `%@` globalInfo:", path);
57+
}
58+
59+
60+
- (void)zipArchiveWillUnzipFileAtIndex:(NSInteger)fileIndex totalFiles:(NSInteger)totalFiles archivePath:(NSString *)archivePath fileInfo:(unz_file_info)fileInfo {
61+
NSLog(@"zipArchiveWillUnzipFileAtIndex: `%ld` totalFiles: `%ld` unzippedPath: `%@` fileInfo:", fileIndex, totalFiles, archivePath);
62+
}
63+
64+
65+
- (void)zipArchiveDidUnzipFileAtIndex:(NSInteger)fileIndex totalFiles:(NSInteger)totalFiles archivePath:(NSString *)archivePath fileInfo:(unz_file_info)fileInfo {
66+
NSLog(@"zipArchiveDidUnzipFileAtIndex: `%ld` totalFiles: `%ld` unzippedPath: `%@` fileInfo:", fileIndex, totalFiles, archivePath);
67+
}
68+
69+
5370
#pragma mark - Private
5471

5572
- (NSString *)_cachesPath {

0 commit comments

Comments
 (0)