iOS - 壓縮與解壓縮

-(void)zip{
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docspath = [paths objectAtIndex:0];
    NSString *eawPath = [[NSBundle mainBundle] pathForResource:@"ClientRes" ofType:@"zip"];
    NSLog(@"eawPath ===%@ ",eawPath);
    NSString *eawRPath = [docspath stringByAppendingPathComponent:@"ClientRes.zip"];
    dispatch_queue_t q = dispatch_queue_create("cn.itcast.gcddemo",DISPATCH_QUEUE_CONCURRENT);
    // 拷貝壓縮包到沙盒
    dispatch_sync(q, ^{
        [[NSFileManager defaultManager] copyItemAtPath:eawPath toPath:eawRPath error:NULL];
    });
    // 解壓壓縮包
    dispatch_sync(q, ^{
        [self OpenZip:eawRPath unzipto:docspath];
    });
    // 刪除壓縮包
    dispatch_sync(q, ^{
        [self deletDocumentsFile:eawRPath];
    });
    PostToUnityOne(StartGame, nil);
}

// 解壓zip包

- (void)OpenZip:(NSString*)zipPath unzipto:(NSString*)_unzipto
{
BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:zipPath];
    if (!blHave) {
        NSLog(@"no  have");
        return ;
    }else {
        ZipArchive* zip = [[ZipArchive alloc] init];
        if( [zip UnzipOpenFile:zipPath] )
        {
            BOOL ret = [zip UnzipFileTo:_unzipto overWrite:YES];
            if( NO==ret )
            {
                NSLog(@"error");
            }
            [zip UnzipCloseFile];
        }
    }
}

// 刪除沙盒文件

-(void)deletDocumentsFile:(NSString *)filePath{
    //文件名
    BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:filePath];
    if (!blHave) {
        NSLog(@"no  have");
        return ;
    }else {
        NSLog(@" have");
        BOOL blDele= [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
        if (blDele) {
            NSLog(@"dele success");
        }else {
            NSLog(@"dele fail");
        }
    }
}

// 刪除緩存

-(void)deleteCaches{
    // 刪除緩存
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachesDir = [paths objectAtIndex:0];
    NSArray *files = [[NSFileManager defaultManager] subpathsAtPath: cachesDir];
    [files enumerateObjectsUsingBlock:^(NSString   * obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *pathS=[cachesDir stringByAppendingPathComponent:obj];
        [self deletDocumentsFile:pathS];
    }]
}

ZipArchive庫:https://pan.baidu.com/s/1bpaSFPt

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • ¥關(guān)閉¥ 【雷霆戰(zhàn)機】 〖http://pan.baidu.com/s/1kVstszX〗 《解壓源碼后直接用AI...
    小菜c閱讀 9,832評論 0 19
  • ¥開啟¥ 【雷霆戰(zhàn)機】 〖http://pan.baidu.com/s/1kVstszX〗 《解壓源碼后直接用AI...
    小菜c閱讀 3,678評論 1 10
  • ¥開啟¥ 【雷霆戰(zhàn)機】 〖http://pan.baidu.com/s/1kVstszX〗 《解壓源碼后直接用AI...
    小菜c閱讀 4,007評論 0 5
  • 假如時間真的可以煮雨。 哪里有空隙去霸占孤獨。 最起碼來說,你一定在向著太陽的方向奔跑。 積極,開朗。 有時候,被...
    徐灝_0158閱讀 231評論 0 0
  • 我愿待你眉眼如初,歲月如故
    曜璃閱讀 138評論 0 0

友情鏈接更多精彩內(nèi)容