objective-c
//拼接补丁打包目录
NSString *patchDirectory = [self patchDirectoryWithAppVersion:entity.appVersion patchVersion:entity.patchVersion];
[[NSFileManager defaultManager] removeItemAtPath:patchDirectory error:NULL];
[[NSFileManager defaultManager] createDirectoryAtPath:patchDirectory withIntermediateDirectories:YES attributes:nil error:NULL];
NSString *patchZipPath = [self patchZipPathWithAppVersion:entity.appVersion patchVersion:entity.patchVersion];
//将数据写到制定打包文件目录
[data writeToFile:patchZipPath atomically:YES];
//计算补丁的MD5值
NSString *hashCode = [FileHash md5HashOfFileAtPath:patchZipPath];
//比对文件MD5值与服务器提供的MD5值
if (hashCode && entity.hashCode && [hashCode isEqualToString:entity.hashCode]) {
//拼接补丁包解压地址
NSString *directory = [self jsDirectoryWithAppVersion:entity.appVersion patchVersion:entity.patchVersion];
[[NSFileManager defaultManager] createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:NULL];
ZipArchive *zip = [[ZipArchive alloc] init];
if ([zip UnzipOpenFile:patchZipPath]) {
if ([zip UnzipFileTo:directory overWrite:YES]) {
[self setAppVersion:entity.appVersion];
[self setPatchVersion:entity.patchVersion];
if ([self checkNeedPatch]) {
[JPEngine startEngine];
//获取目录中所有的JavaScript文件
NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directory error:nil];
[array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) {
NSString *path = [directory stringByAppendingPathComponent:(NSString *) obj];
//如果文件是.js文件 则将文件注入打补丁
if ([path hasSuffix:@".js"]) {
[JPEngine evaluateScriptWithPath:path];
}
}];
}
}