当前位置: 移动技术网 > 移动技术>移动开发>IOS > IOS下载资源zip到本地然后读取

IOS下载资源zip到本地然后读取

2019年05月12日  | 移动技术网移动技术  | 我要评论

思路是

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:@"https://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:@"https://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"];
}

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网