2013-04-11 85 views

回答

1

聲明這個變量在您的.h文件中

NSMutableData *responseData; 

寫這樣的代碼在您的viewDidLoad方法 此方法將開始從給定的URL

NSURL *serverURL = [NSURL URLWithString:@"your file URL here"]; 
NSURLRequest *request = [NSURLRequest requestWithURL:serverURL]; 
NSURLConnection *cn = [NSURLConnection connectionWithRequest:request delegate:self]; 
[cn start]; 

下載文件實現的.m此委託方法文件

#pragma mark - NSURLConnection Delegate Methods 
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    NSLog(@"%s",__FUNCTION__); 
    responseData = nil; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    NSLog(@"%s",__FUNCTION__); 
    responseData = [[NSMutableData alloc] initWithCapacity:0]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    NSLog(@"%s",__FUNCTION__); 
    [responseData appendData:data]; 
} 


- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSLog(@"%s",__FUNCTION__); 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *docDirPath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil; 
    NSString *filePath = [docDirPath stringByAppendingPathComponent:@"DownloadedZip.zip"]; 

    // Save file to Document Directory 
    [responseData writeToFile:filePath atomically:YES]; 
    responseData = nil; 
} 

將項目下載到unArchive z ip文件here,並在文件目錄中保存文件後放入存檔代碼

+0

謝謝你的詳細解答。有效。 :) – NaXir 2013-04-12 04:34:59

+0

歡迎@NasirMahmood – 2013-04-12 04:42:11

0

首先將文件下載到您的設備。

NSString *URLstring = @"http://your.download.URL"; 
NSURL *url = [NSURL URLWithString:URLstring]; 
NSData *urlData = [NSData dataWithContentsOfURL:url]; 

然後將其寫入設備。如果路徑是你的設備上你想要保存的位置以及要保存它的文件名,請記住添加文件擴展名(.zip)。

[urlData writeToFile:thePath atomically:YES]; 

然後,你可以使用類似ziparchiveminizip來解壓。那裏有幾個開源解決方案。這兩個在過去爲我工作。容易使用。

之後,它足夠簡單,可以用解壓縮的數據做任何你想做的事情。然後做一些清理並從手機中刪除zip文件。