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"];
}