Download the resource zip to your local device for IOS and then read it.

Source: Internet
Author: User
Tags ziparchive

The idea is

1. ios downloads the zip Resource Package (images, sounds, and other zip-compressed resource packages) from the server to the local device.

2. decompress the zip file to the program directory.

3. Load resource files from the program directory

 


1. Download zip Resources


[Cpp]
-(NSString *) DownloadTextFile :( NSString *) fileUrl fileName :( NSString *) _ fileName
{
NSArray * documentPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); // use the C function NSSearchPathForDirectoriesInDomains to obtain the full path of directories in the sandbox.
NSString * ourDocumentPath = [documentPaths objectAtIndex: 0];
NSString * sandboxPath = NSHomeDirectory ();
NSString * documentPath = [sandboxPath stringByAppendingPathComponent: @ "TestDownImgZip. app"]; // Add Documents to the sandbox path // TestDownImgZip. app
NSString * FileName = [documentPath stringByAppendingPathComponent: _ fileName]; // fileName indicates the name of the file to be saved.
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]; // write NSData-type object data to a file named FileName
}
Return FileName;
}

-(NSString *) DownloadTextFile :( NSString *) fileUrl fileName :( NSString *) _ fileName
{
NSArray * documentPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); // use the C function NSSearchPathForDirectoriesInDomains to obtain the full path of directories in the sandbox.
NSString * ourDocumentPath = [documentPaths objectAtIndex: 0];
NSString * sandboxPath = NSHomeDirectory ();
NSString * documentPath = [sandboxPath stringByAppendingPathComponent: @ "TestDownImgZip. app"]; // Add Documents to the sandbox path // TestDownImgZip. app
NSString * FileName = [documentPath stringByAppendingPathComponent: _ fileName]; // fileName indicates the name of the file to be saved.
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]; // write NSData-type object data to a file named FileName
}
Return FileName;
}

 

 

 

2. decompress the zip package


[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. Call a function


[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); // use the C function NSSearchPathForDirectoriesInDomains to obtain the full path of directories in the sandbox.
NSString * ourDocumentPath = [documentPaths objectAtIndex: 0];
NSString * sandboxPath = NSHomeDirectory ();
NSString * documentPath = [sandboxPath stringByAppendingPathComponent: @ "TestDownImgZip. app"]; // Add Documents to the sandbox path // 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); // use the C function NSSearchPathForDirectoriesInDomains to obtain the full path of directories in the sandbox.
NSString * ourDocumentPath = [documentPaths objectAtIndex: 0];
NSString * sandboxPath = NSHomeDirectory ();
NSString * documentPath = [sandboxPath stringByAppendingPathComponent: @ "TestDownImgZip. app"]; // Add Documents to the sandbox path // TestDownImgZip. app

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.