当前位置 博文首页 > 李长孺的专栏:脚本 Qt tar.gz 解压缩 获取进度

    李长孺的专栏:脚本 Qt tar.gz 解压缩 获取进度

    作者:[db:作者] 时间:2021-08-17 09:47

    解压缩并显示进度

    方法1

    ?	pv ../root.tar.gz |tar -zxf -
    

    ? 参考:https://blog.csdn.net/weixin_33711641/article/details/94330472?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2allfirst_rank_v2~rank_v25-8-94330472.nonecase&utm_term=tar%E8%A7%A3%E5%8E%8B%E7%9C%8B%E8%BF%9B%E5%BA%A6

    方法2

    #!/bin/bash
    
    #total_size=`xz --robot -l $1 | grep 'totals' | awk '{print $5}'`
    total_size=`gzip -l $1 |awk 'END{print $2}'`
    
    echo "total_size:$total_size"
    
    block_size=`expr $total_size / 51200`
    block_size=`expr $block_size + 1`
    #let block_size=total_size/51200
    #let block_size=block_size+1
    echo "block_size:$block_size"
    tar --blocking-factor=$block_size --checkpoint=1 --checkpoint-action='ttyout= %u    \r' -zxf $1 -C $2
    
    #显示效果:
    root@lalala:~# ./unpackage.sh ./root.tar.gz temp/
    total_size:471726080
    block_size:921
    unpack 54%
    

    参考 : https://blog.csdn.net/u012385733/article/details/88840126

    参考:http://www.gnu.org/software/tar/manual/html_section/tar_26.html

    for Qt

    #解压脚本
    #!/bin/bash
    
    total_size=`gzip -l $1 |awk 'END{print $2}'`
    
    #echo "total_size:$total_size"
    
    block_size=`expr $total_size / 512`
    block_size=`expr $block_size / $3`
    block_size=`expr $block_size + 1`
    #let block_size=total_size/51200
    #let block_size=block_size+1
    #echo "block_size:$block_size"
    tar --blocking-factor=$block_size --checkpoint=1  --checkpoint-action='echo=%u'  -zxf $1 -C $2   2>&1
    
    //Qt获取tar解压进度 
    //伪代码 简单改改就好
    get_tar_progress_block("/bin/bash",QStringList()<<"-c"<<"/usr/nova/bin/unpackage.sh /advanced/root.tar.gz  /  100", &pval, 100);
    //pval 为进度条进度值,100为压缩进度多少等分
    
    int xxx::get_tar_progress_block(QString cmd,QStringList opt, int *pval, int portions){
            QProcess *proc = new QProcess;
            QByteArray result_tmp;
            QString result;
            int cmdEnd = 0;
            int temp = 0;
    
            proc->start(cmd,opt);
            if (!proc->waitForStarted()){
                    return -1;
            }
            while(!cmdEnd){
                    if( proc->waitForReadyRead() ){
                            result_tmp.append(proc->readAll());
                            result = result_tmp;
                            if(result == "100")
                                    cmdEnd = 1;
                        
                            if(result.right(1) == "\n"){
                                    QRegularExpression rx("(\\d+)");
                                    QRegularExpressionMatchIterator i = rx.globalMatch(result);
                                    while (i.hasNext()) {
                                            QRegularExpressionMatch match = i.next();
                                            QString word = match.captured(1);
                                            temp = word.toInt();
                                            emit emit_progressbar_val(SYSINIT_PROGRESSBAR_ID,(*pval+temp));//给进度条进度值
                                            if(temp == portions){
                                                    cmdEnd = 1;
                                                    *pval += temp;
                                            }
                                    }
                                    result_tmp.clear();
                                    result.clear();
                            }
            }
            if (!proc->waitForFinished()){
                    return -1;
            }
            return 0;
    
    }
    
    cs