你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS下載資源zip到本地然後讀取

IOS下載資源zip到本地然後讀取

編輯:IOS開發綜合

思路是

1.ios下載服務器上的zip資源包(圖片,聲音等經過zip壓縮的資源包)到本地

2.解壓zip到程序目錄

3.從程序目錄加載資源文件

 


一、下載zip資源


[cpp]
-(NSString*)DownloadTextFile:(NSString*)fileUrl   fileName:(NSString*)_fileName 

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask,YES);//使用C函數NSSearchPathForDirectoriesInDomains來獲得沙盒中目錄的全路徑。  
    NSString *ourDocumentPath =[documentPaths objectAtIndex:0]; 
    NSString *sandboxPath = NSHomeDirectory(); 
    NSString *documentPath = [sandboxPath  stringByAppendingPathComponent:@"TestDownImgZip.app"];//將Documents添加到sandbox路徑上//TestDownImgZip.app  
    NSString *FileName=[documentPath stringByAppendingPathComponent:_fileName];//fileName就是保存文件的文件名  
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    // Copy the database sql file from the resourcepath to the documentpath  
    if ([fileManager fileExistsAtPath:FileName]) 
    { 
        return FileName; 
    }else 
    { 
        NSURL *url = [NSURL URLWithString:fileUrl]; 
        NSData *data = [NSData dataWithContentsOfURL:url]; 
        [data writeToFile:FileName atomically:YES];//將NSData類型對象data寫入文件,文件名為FileName  
    } 
    return FileName; 

-(NSString*)DownloadTextFile:(NSString*)fileUrl   fileName:(NSString*)_fileName
{
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask,YES);//使用C函數NSSearchPathForDirectoriesInDomains來獲得沙盒中目錄的全路徑。
    NSString *ourDocumentPath =[documentPaths objectAtIndex:0];
    NSString *sandboxPath = NSHomeDirectory();
    NSString *documentPath = [sandboxPath  stringByAppendingPathComponent:@"TestDownImgZip.app"];//將Documents添加到sandbox路徑上//TestDownImgZip.app
    NSString *FileName=[documentPath stringByAppendingPathComponent:_fileName];//fileName就是保存文件的文件名
    NSFileManager *fileManager = [NSFileManager defaultManager];
    // Copy the database sql file from the resourcepath to the documentpath
    if ([fileManager fileExistsAtPath:FileName])
    {
        return FileName;
    }else
    {
        NSURL *url = [NSURL URLWithString:fileUrl];
        NSData *data = [NSData dataWithContentsOfURL:url];
        [data writeToFile:FileName atomically:YES];//將NSData類型對象data寫入文件,文件名為FileName
    }
    return FileName;
}

 

 

 

2.解壓zip包


[cpp]
- (void)OpenZip:(NSString*)zipPath  unzipto:(NSString*)_unzipto 

    ZipArchive* zip = [[ZipArchive alloc] init]; 
    if( [zip UnzipOpenFile:zipPath] ) 
    { 
        BOOL ret = [zip UnzipFileTo:_unzipto overWrite:YES]; 
        if( NO==ret ) 
        { 
            NSLog(@"error"); 
        } 
        [zip UnzipCloseFile]; 
    } 
    [zip release]; 
     

- (void)OpenZip:(NSString*)zipPath  unzipto:(NSString*)_unzipto
{
    ZipArchive* zip = [[ZipArchive alloc] init];
    if( [zip UnzipOpenFile:zipPath] )
    {
        BOOL ret = [zip UnzipFileTo:_unzipto overWrite:YES];
        if( NO==ret )
        {
            NSLog(@"error");
        }
        [zip UnzipCloseFile];
    }
    [zip release];
   
}

 

3.調去函數


[cpp]
- (IBAction)ShowImg:(id)sender { 
    NSString *filePath = [self DownloadTextFile:@"http://www.xtox.net/img.zip" fileName:@"img.zip"]; 
    NSLog(filePath); 
     
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask,YES);//使用C函數NSSearchPathForDirectoriesInDomains來獲得沙盒中目錄的全路徑。  
    NSString *ourDocumentPath =[documentPaths objectAtIndex:0]; 
    NSString *sandboxPath = NSHomeDirectory(); 
    NSString *documentPath = [sandboxPath  stringByAppendingPathComponent:@"TestDownImgZip.app"];//將Documents添加到sandbox路徑上//TestDownImgZip.app  
 
    [self OpenZip:filePath unzipto:documentPath]; 
    self.imgView.image = [UIImage imageNamed:@"img/1.png"]; 

- (IBAction)ShowImg:(id)sender {
    NSString *filePath = [self DownloadTextFile:@"http://www.xtox.net/img.zip" fileName:@"img.zip"];
    NSLog(filePath);
   
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask,YES);//使用C函數NSSearchPathForDirectoriesInDomains來獲得沙盒中目錄的全路徑。
    NSString *ourDocumentPath =[documentPaths objectAtIndex:0];
    NSString *sandboxPath = NSHomeDirectory();
    NSString *documentPath = [sandboxPath  stringByAppendingPathComponent:@"TestDownImgZip.app"];//將Documents添加到sandbox路徑上//TestDownImgZip.app

    [self OpenZip:filePath unzipto:documentPath];
    self.imgView.image = [UIImage imageNamed:@"img/1.png"];
}

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved