@@ -58,53 +58,55 @@ - (id)initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLRespons
58
58
}
59
59
if (_zip == NULL ) {
60
60
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
+ */
71
70
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
+ }
80
75
_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
+ }
90
88
}
91
89
}
92
90
return self;
93
91
}
94
92
95
93
- (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
+ }
108
110
}
109
111
110
112
- (void )stopLoading {
0 commit comments