Guest User

Untitled

a guest
Oct 29th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1.  
  2. #import <Foundation/Foundation.h>
  3.  
  4. @protocol DownloadDelegate;
  5.  
  6. @interface Download : NSObject {
  7.  
  8. id <DownloadDelegate> delegate;
  9.  
  10. NSString *_url;
  11. NSString *_path;
  12. NSString *_filename;
  13. NSString *_filepath;
  14.  
  15. NSFileHandle *file;
  16.  
  17. long long cLength;
  18.  
  19. }
  20.  
  21. @property(nonatomic,assign) id<DownloadDelegate> delegate;
  22.  
  23. @property(nonatomic,retain) NSString *_url;
  24. @property(nonatomic,retain) NSString *_path;
  25. @property(nonatomic,retain) NSString *_filename;
  26. @property(nonatomic,retain) NSString *_filepath;
  27.  
  28. @property(nonatomic,retain) NSFileHandle *file;
  29.  
  30. -(void)downloadFromURL:(NSString *)url toFile:(NSString *)filename inPath:(NSString *)path;
  31. -(void)unzip;
  32. -(void)unzipFile:(NSString *)f toPath:(NSString *)p;
  33. -(void)deleteZip;
  34.  
  35. @end
  36.  
  37. @protocol DownloadDelegate<NSObject>
  38. @optional
  39.  
  40. - (void)onProgress:(float)percent;
  41. - (void)download:(Download *)download didFinish:(BOOL)success;
  42. - (void)unzip:(Download *)download didFinish:(BOOL)success;
  43.  
  44. @end
  45.  
  46.  
  47.  
  48. #import "Download.h"
  49. #import "ZipArchive.h"
  50. #import "Globals.h"
  51.  
  52.  
  53. @implementation Download
  54.  
  55. @synthesize delegate;
  56. @synthesize _url, _path, _filename, _filepath;
  57. @synthesize file;
  58.  
  59. -(void)downloadFromURL:(NSString *)url toFile:(NSString *)filename inPath:(NSString *)path {
  60.  
  61. NSString *u = [[NSString alloc] initWithString:url];
  62. self._url = u;
  63. [u release];
  64.  
  65. NSString *p = [[NSString alloc] initWithString:path];
  66. self._path = p;
  67. [p release];
  68.  
  69. NSString *fn = [[NSString alloc] initWithString:filename];
  70. self._filename = fn;
  71. [fn release];
  72.  
  73. NSString *fp = [[NSString alloc] initWithString:[_path stringByAppendingPathComponent:_filename]];
  74. self._filepath = fp;
  75. [fp release];
  76.  
  77. if(DEBUG) NSLog(@"download.start");
  78.  
  79. [[NSURLCache sharedURLCache] setMemoryCapacity:0]; // prevent NSURLConnection from leaking
  80. [[NSURLCache sharedURLCache] setDiskCapacity:0]; // prevent NSURLConnection from leaking
  81.  
  82. if(DEBUG) NSLog(@"Downloading %@ from %@ to %@", _filename, _url, _filepath);
  83.  
  84. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:_url]];
  85.  
  86. if(request==nil){
  87. if(DEBUG) NSLog(@"download.request = nil");
  88. [delegate download:self didFinish:NO];
  89. return;
  90. }
  91.  
  92. NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
  93.  
  94. if(connection==nil){
  95. if(DEBUG) NSLog(@"download.connection = nil");
  96. [delegate download:self didFinish:NO];
  97. return;
  98. }
  99.  
  100. [connection release];
  101.  
  102.  
  103. }
  104. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response {
  105.  
  106. if(DEBUG) NSLog(@"download.connectionDidReceiveResonse: %@", response);
  107.  
  108. cLength = [response expectedContentLength];
  109.  
  110. BOOL pathExists = [[NSFileManager defaultManager] fileExistsAtPath:_path];
  111.  
  112. if(!pathExists){
  113. [[NSFileManager defaultManager] createDirectoryAtPath:_path attributes:nil];
  114. }
  115.  
  116. [[NSFileManager defaultManager] createFileAtPath:_filepath contents:nil attributes:nil];
  117. file = [[NSFileHandle fileHandleForUpdatingAtPath:_filepath] retain];// file is in .h
  118.  
  119. if (file){
  120. [file seekToEndOfFile];
  121. }
  122. }
  123.  
  124. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
  125.  
  126. long long c = 1;
  127.  
  128. if (file) {
  129.  
  130. c = [file seekToEndOfFile];
  131.  
  132. }
  133.  
  134. [file writeData:data];
  135.  
  136. NSNumber *resourceLength = [NSNumber numberWithUnsignedInteger:c];
  137. NSNumber *p = [NSNumber numberWithFloat:([resourceLength floatValue] / cLength )];
  138. [self.delegate onProgress:[p floatValue]];
  139.  
  140. }
  141.  
  142. - (void)connectionDidFinishLoading:(NSURLConnection*)connection {
  143.  
  144. if(DEBUG) NSLog(@"download.connectionDidFinishLoading: %@", connection);
  145.  
  146. [file closeFile];
  147.  
  148. [delegate download:self didFinish:YES];
  149.  
  150. [NSThread detachNewThreadSelector:@selector(unzip) toTarget:self withObject:nil];
  151.  
  152. [file release];
  153.  
  154.  
  155. }
  156.  
  157. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
  158.  
  159. [delegate download:self didFinish:NO];
  160.  
  161. if(file) [file release];
  162.  
  163. }
  164.  
  165. -(void)unzip {
  166.  
  167. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  168.  
  169. if(DEBUG) NSLog(@"Download.unzip");
  170.  
  171. ZipArchive *zipArchive = [[ZipArchive alloc] init];
  172. if([zipArchive UnzipOpenFile:_filepath]) {
  173. if([zipArchive UnzipFileTo:_path overWrite:YES]) {
  174. if(DEBUG) NSLog(@"Archive successfully unzipped.");
  175. }
  176. }
  177. [zipArchive release];
  178.  
  179. [self deleteZip];
  180.  
  181. [delegate unzip:self didFinish:YES];
  182.  
  183. [pool release];
  184.  
  185. }
  186.  
  187. -(void)unzipFile:(NSString *)f toPath:(NSString *)p {
  188.  
  189. NSString *pt = [[NSString alloc] initWithString:p];
  190. self._path = pt;
  191. [pt release];
  192.  
  193. NSString *fp = [[NSString alloc] initWithString:f];
  194. self._filepath = fp;
  195. [fp release];
  196.  
  197. [NSThread detachNewThreadSelector:@selector(unzip) toTarget:self withObject:nil];
  198. }
  199.  
  200. -(void)deleteZip {
  201. [[NSFileManager defaultManager] removeItemAtPath:_filepath error:nil];
  202. }
  203.  
  204. #pragma mark memory management
  205.  
  206. -(void)dealloc {
  207.  
  208. file = nil;
  209. _url = nil;
  210. _path = nil;
  211. _filename = nil;
  212. _filepath = nil;
  213.  
  214. if(DEBUG) NSLog(@"Download %@ deallocated.", self);
  215.  
  216. [super dealloc];
  217.  
  218. }
  219.  
  220.  
  221. @end
Add Comment
Please, Sign In to add comment