bsdiff算法c语言实现,iOS 使用bsdiff进行资源文件增量更新(bsdiff / bspatch)
bsdiff介紹:
bsdiff是一種二級制差分工具,由bsdiff與bspatch組成, 將oldfile與newfile做二進制數據差分(bsdiff操作),得到更新的部分(patch文件),再與oldfile進行合成(bspatch操作)。比如icon增量更新為例,old圖片資源有5個icon,new圖片資源在之前的5個icon上加了10個新的icon,這是old與new進行bsdiff操作,會生成增量10個icon的patch文件,接下來再與old圖片資源進行bspatch操作,最后合成了最新的15個icon圖片資源。
bsdiff原理.png
下載bsdiff bzip
開始正題
新建一個工程,將下載好的bsdiff bzip導入工程中,導入文件內容:
image.png
編譯工程文件,會報一個函數重載的error
image.png
需要在panic方法前添加__attribute__((overloadable))
繼續編譯項目
回報如下的錯誤,原因是存在相同的main函數,我們按照錯誤提示,到每個方法中進行文件重命名即可。
image.png
此時編譯,成功!!!
因為bsdiff為C語言編寫,oc引用C需要新建一個pch文件,在pch引用bsdiff.c與bspatch.c的方法(該方法名是我將main方法改成此方法名)
int BsdiffUntils_bsdiff(int argc, char *argv[]);
int BsdiffUntils_bspatch(int argc,char * argv[]);
并在bsdiff.c與bspatch.c中include該pch文件。
因在本地進行測試,所有正常要在服務器做的bsdiff操作也需要在本地進行操作
我這里找了6張圖片,其中3張壓縮成zip,作為old文件, 所有的6張壓縮成zip,作為new文件。
調用bsdiff方法
#pragma Mark - Bsdiff
- (void)Bsdiff
{
const char *argv[4];
argv[0] = "bsdiff";
// oldPath
NSString *path1 = [NSString stringWithFormat:@"/%@/%@",[NSBundle mainBundle].bundlePath, @"old.zip"];
argv[1] = [path1 UTF8String];
// newPath
NSString *path2 = [NSString stringWithFormat:@"/%@/%@",[NSBundle mainBundle].bundlePath, @"new.zip"];
// argv[2] = argv[1] = [[NSString stringWithFormat:@"file://%@", path2] UTF8String];
argv[2] = [path2 UTF8String];
// patchPath
argv[3] = [[self createFileWithFileName:@"bsdiff_Test"] UTF8String];
int result = BsdiffUntils_bsdiff(4, argv);
}
參數1:為固定字符串
參數2:oldfile的文件路徑
參數3:newfile的文件路徑
參數4:合成patch的文件路徑
此時,運行項目,會在對應路徑下出現一個bsdiff_Test的文件,這個文件就是oldfile與newfile差分出來的文件,也就是需要更新的文件,但是這個文件不是直接就可以使用的,需要我們用bspatch方法將其合成zip文件
調用bspatch方法
#pragma Mark - Bspatch
- (void)Bspatch
{
const char *argv[4];
argv[0] = "bspatch";
// oldPath
NSString *path1 = [NSString stringWithFormat:@"/%@/%@",[NSBundle mainBundle].bundlePath, @"old.zip"];
argv[1] = [path1 UTF8String];
// newPath
argv[2] = [[self createFileWithFileName:@"Test_Result.zip"] UTF8String];
// patchPath
argv[3] = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"bsdiff_Test"] UTF8String];
int result = BsdiffUntils_bspatch(4, argv);
}
參數1:為固定字符串
參數2:oldfile的文件路徑
參數3:oldfile與patch合成成zip文件的目標路徑
參數4:上一步差分出來的patch文件路徑
運行項目,在對應路徑下,就會看到Test_Result.zip文件,此時解壓此壓縮包,就為完整的文件。
另外的測試:比如oldfile有5個圖片,newfile有10個圖片,其中old中有三個圖片被刪除了,這時oldfile與newfile進行bsdiff時,patch出來的就是7個文件,這些都可以自行測試,這里就不在此贅述了
總結
以上是生活随笔為你收集整理的bsdiff算法c语言实现,iOS 使用bsdiff进行资源文件增量更新(bsdiff / bspatch)的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: python:私有属性
 - 下一篇: 争分夺秒,努力进取