Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import <Foundation/Foundation.h>
- @protocol DownloadDelegate;
- @interface Download : NSObject {
- id <DownloadDelegate> delegate;
- NSString *_url;
- NSString *_path;
- NSString *_filename;
- NSString *_filepath;
- NSFileHandle *file;
- long long cLength;
- }
- @property(nonatomic,assign) id<DownloadDelegate> delegate;
- @property(nonatomic,retain) NSString *_url;
- @property(nonatomic,retain) NSString *_path;
- @property(nonatomic,retain) NSString *_filename;
- @property(nonatomic,retain) NSString *_filepath;
- @property(nonatomic,retain) NSFileHandle *file;
- -(void)downloadFromURL:(NSString *)url toFile:(NSString *)filename inPath:(NSString *)path;
- -(void)unzip;
- -(void)unzipFile:(NSString *)f toPath:(NSString *)p;
- -(void)deleteZip;
- @end
- @protocol DownloadDelegate<NSObject>
- @optional
- - (void)onProgress:(float)percent;
- - (void)download:(Download *)download didFinish:(BOOL)success;
- - (void)unzip:(Download *)download didFinish:(BOOL)success;
- @end
- #import "Download.h"
- #import "ZipArchive.h"
- #import "Globals.h"
- @implementation Download
- @synthesize delegate;
- @synthesize _url, _path, _filename, _filepath;
- @synthesize file;
- -(void)downloadFromURL:(NSString *)url toFile:(NSString *)filename inPath:(NSString *)path {
- NSString *u = [[NSString alloc] initWithString:url];
- self._url = u;
- [u release];
- NSString *p = [[NSString alloc] initWithString:path];
- self._path = p;
- [p release];
- NSString *fn = [[NSString alloc] initWithString:filename];
- self._filename = fn;
- [fn release];
- NSString *fp = [[NSString alloc] initWithString:[_path stringByAppendingPathComponent:_filename]];
- self._filepath = fp;
- [fp release];
- if(DEBUG) NSLog(@"download.start");
- [[NSURLCache sharedURLCache] setMemoryCapacity:0]; // prevent NSURLConnection from leaking
- [[NSURLCache sharedURLCache] setDiskCapacity:0]; // prevent NSURLConnection from leaking
- if(DEBUG) NSLog(@"Downloading %@ from %@ to %@", _filename, _url, _filepath);
- NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:_url]];
- if(request==nil){
- if(DEBUG) NSLog(@"download.request = nil");
- [delegate download:self didFinish:NO];
- return;
- }
- NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
- if(connection==nil){
- if(DEBUG) NSLog(@"download.connection = nil");
- [delegate download:self didFinish:NO];
- return;
- }
- [connection release];
- }
- - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response {
- if(DEBUG) NSLog(@"download.connectionDidReceiveResonse: %@", response);
- cLength = [response expectedContentLength];
- BOOL pathExists = [[NSFileManager defaultManager] fileExistsAtPath:_path];
- if(!pathExists){
- [[NSFileManager defaultManager] createDirectoryAtPath:_path attributes:nil];
- }
- [[NSFileManager defaultManager] createFileAtPath:_filepath contents:nil attributes:nil];
- file = [[NSFileHandle fileHandleForUpdatingAtPath:_filepath] retain];// file is in .h
- if (file){
- [file seekToEndOfFile];
- }
- }
- - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
- long long c = 1;
- if (file) {
- c = [file seekToEndOfFile];
- }
- [file writeData:data];
- NSNumber *resourceLength = [NSNumber numberWithUnsignedInteger:c];
- NSNumber *p = [NSNumber numberWithFloat:([resourceLength floatValue] / cLength )];
- [self.delegate onProgress:[p floatValue]];
- }
- - (void)connectionDidFinishLoading:(NSURLConnection*)connection {
- if(DEBUG) NSLog(@"download.connectionDidFinishLoading: %@", connection);
- [file closeFile];
- [delegate download:self didFinish:YES];
- [NSThread detachNewThreadSelector:@selector(unzip) toTarget:self withObject:nil];
- [file release];
- }
- - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
- [delegate download:self didFinish:NO];
- if(file) [file release];
- }
- -(void)unzip {
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- if(DEBUG) NSLog(@"Download.unzip");
- ZipArchive *zipArchive = [[ZipArchive alloc] init];
- if([zipArchive UnzipOpenFile:_filepath]) {
- if([zipArchive UnzipFileTo:_path overWrite:YES]) {
- if(DEBUG) NSLog(@"Archive successfully unzipped.");
- }
- }
- [zipArchive release];
- [self deleteZip];
- [delegate unzip:self didFinish:YES];
- [pool release];
- }
- -(void)unzipFile:(NSString *)f toPath:(NSString *)p {
- NSString *pt = [[NSString alloc] initWithString:p];
- self._path = pt;
- [pt release];
- NSString *fp = [[NSString alloc] initWithString:f];
- self._filepath = fp;
- [fp release];
- [NSThread detachNewThreadSelector:@selector(unzip) toTarget:self withObject:nil];
- }
- -(void)deleteZip {
- [[NSFileManager defaultManager] removeItemAtPath:_filepath error:nil];
- }
- #pragma mark memory management
- -(void)dealloc {
- file = nil;
- _url = nil;
- _path = nil;
- _filename = nil;
- _filepath = nil;
- if(DEBUG) NSLog(@"Download %@ deallocated.", self);
- [super dealloc];
- }
- @end
Add Comment
Please, Sign In to add comment