Skip to content

Commit 1a848e4

Browse files
author
Hugo Hache
committed
Zip or zip file not found error is now correctly handled
1 parent daa2277 commit 1a848e4

File tree

1 file changed

+41
-39
lines changed

1 file changed

+41
-39
lines changed

ADZipURLProtocol/ADZipURLProtocol.m

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -58,53 +58,55 @@ - (id)initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLRespons
5858
}
5959
if (_zip == NULL) {
6060
NSLog(@"Could not find a zipfile named \"%@\" in \"%@\"", archiveName, searchPath);
61-
return nil;
62-
}
63-
// The following can be used to list every file in the _zip archive
64-
/*
65-
zip_int64_t numEntries = zip_get_num_entries(_zip, 0);
66-
for (int i = 0; i <numEntries; i++) {
67-
const char * name = zip_get_name(_zip, i, 0);
68-
NSLog(@"File at index %d is \"%s\"",i, name);
69-
}
70-
*/
61+
} else {
62+
// The following can be used to list every file in the _zip archive
63+
/*
64+
zip_int64_t numEntries = zip_get_num_entries(_zip, 0);
65+
for (int i = 0; i <numEntries; i++) {
66+
const char * name = zip_get_name(_zip, i, 0);
67+
NSLog(@"File at index %d is \"%s\"",i, name);
68+
}
69+
*/
7170

72-
NSString * fileName = request.URL.path;
73-
if ([fileName hasPrefix:@"/"]) {
74-
fileName = [fileName substringFromIndex:1]; // Remove the leading slash if present
75-
}
76-
_zipFile = zip_fopen(_zip, fileName.UTF8String, ZIP_FL_UNCHANGED);
77-
if (_zipFile == NULL) { // Let's do as if we were a webserver and try appending "index.html" if the URL referenced a folder
78-
NSLog(@"Could not find a file named \"%@\" in archive, trying to append index.html", fileName);
79-
fileName = [fileName stringByAppendingPathComponent:@"index.html"];
71+
NSString * fileName = request.URL.path;
72+
if ([fileName hasPrefix:@"/"]) {
73+
fileName = [fileName substringFromIndex:1]; // Remove the leading slash if present
74+
}
8075
_zipFile = zip_fopen(_zip, fileName.UTF8String, ZIP_FL_UNCHANGED);
81-
}
82-
if (_zipFile == NULL) {
83-
NSLog(@"Could not find a file named \"%@\" in archive", fileName);
84-
return nil;
85-
}
86-
_readBuffer = malloc(AD_ZIP_PROTOCOL_READ_BUFFER_LENGTH);
87-
if (_readBuffer == NULL) {
88-
NSLog(@"Couldn't allocate read buffer !");
89-
return nil;
76+
if (_zipFile == NULL) { // Let's do as if we were a webserver and try appending "index.html" if the URL referenced a folder
77+
NSLog(@"Could not find a file named \"%@\" in archive, trying to append index.html", fileName);
78+
fileName = [fileName stringByAppendingPathComponent:@"index.html"];
79+
_zipFile = zip_fopen(_zip, fileName.UTF8String, ZIP_FL_UNCHANGED);
80+
}
81+
if (_zipFile == NULL) {
82+
NSLog(@"Could not find a file named \"%@\" in archive", fileName);
83+
}
84+
_readBuffer = malloc(AD_ZIP_PROTOCOL_READ_BUFFER_LENGTH);
85+
if (_readBuffer == NULL) {
86+
NSLog(@"Couldn't allocate read buffer !");
87+
}
9088
}
9189
}
9290
return self;
9391
}
9492

9593
- (void)startLoading {
96-
[self.client URLProtocol:self
97-
didReceiveResponse:[[NSURLResponse alloc] initWithURL:_lastReqURL MIMEType:nil expectedContentLength:-1 textEncodingName:nil]
98-
cacheStoragePolicy:NSURLCacheStorageNotAllowed];
99-
int numberOfBytesRead;
100-
do {
101-
numberOfBytesRead = (int)zip_fread(_zipFile, _readBuffer, AD_ZIP_PROTOCOL_READ_BUFFER_LENGTH);
102-
if (numberOfBytesRead >= 0) {
103-
NSData * data = [NSData dataWithBytes:_readBuffer length:numberOfBytesRead];
104-
[self.client URLProtocol:self didLoadData:data];
105-
}
106-
} while (numberOfBytesRead > 0);
107-
[self.client URLProtocolDidFinishLoading:self];
94+
if (_zip && _zipFile && _readBuffer) {
95+
[self.client URLProtocol:self
96+
didReceiveResponse:[[NSURLResponse alloc] initWithURL:_lastReqURL MIMEType:nil expectedContentLength:-1 textEncodingName:nil]
97+
cacheStoragePolicy:NSURLCacheStorageNotAllowed];
98+
int numberOfBytesRead;
99+
do {
100+
numberOfBytesRead = (int)zip_fread(_zipFile, _readBuffer, AD_ZIP_PROTOCOL_READ_BUFFER_LENGTH);
101+
if (numberOfBytesRead >= 0) {
102+
NSData * data = [NSData dataWithBytes:_readBuffer length:numberOfBytesRead];
103+
[self.client URLProtocol:self didLoadData:data];
104+
}
105+
} while (numberOfBytesRead > 0);
106+
[self.client URLProtocolDidFinishLoading:self];
107+
} else {
108+
[self.client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSCocoaErrorDomain code:NSFileNoSuchFileError userInfo:nil]];
109+
}
108110
}
109111

110112
- (void)stopLoading {

0 commit comments

Comments
 (0)