使用QuaZip對文件進行壓縮解壓操作

使用QuaZip對文件進行壓縮解壓操作

       八月份快要過去了,突然想到我還沒有寫一篇日誌呢,想一想這一個月我並沒有做開發以外的事情,三十天來還是嘗試並且解決了不少技術上的問題的。所以這次我打算將其中一些作爲日誌分享出來。

蔣彩陽原創文章,首發地址:http://blog.csdn.net/gamesdev/article/details/48136687。歡迎同行前來探討。

       前幾天正在討論使用打包工具的問題,待選的方案是7z和zip。於是拿了QLib7z、Qt7z還有QuaZip來進行測試,後面發現,使用QuaZip這個方案實在是很方便,於是就將使用QuaZip的過程記錄一下。大家可能看過別人有關介紹QuaZip的博客,我這篇沒有參考其它人的寫法,完全是來自官方測試項目。

下載

       QuaZip項目的地址來自sourceforge.net。大家可以點擊這個地址獲取下載鏈接

書寫pri文件

       下載完畢之後,我們決定採用其源碼的方式,而不是編譯成一個靜態的庫。因此,我們新建一個qmake項目,這個項目包含描述QuaZip項目構建情況的文件,它的內容是:

 

# QuaZip.pri
 
QUAZIP_INCLUDE_PATH= E:\QtReference\quazip-0.7.1
 
INCLUDEPATH+= $$QUAZIP_INCLUDE_PATH/quazip
DEFINES+= QUAZIP_BUILD
LIBS+= -lz
 
HEADERS+= \
        $$QUAZIP_INCLUDE_PATH/quazip/crypt.h \
        $$QUAZIP_INCLUDE_PATH/quazip/ioapi.h \
       $$QUAZIP_INCLUDE_PATH/quazip/JlCompress.h \
       $$QUAZIP_INCLUDE_PATH/quazip/quaadler32.h \
       $$QUAZIP_INCLUDE_PATH/quazip/quachecksum32.h \
        $$QUAZIP_INCLUDE_PATH/quazip/quacrc32.h\
       $$QUAZIP_INCLUDE_PATH/quazip/quagzipfile.h \
       $$QUAZIP_INCLUDE_PATH/quazip/quaziodevice.h \
       $$QUAZIP_INCLUDE_PATH/quazip/quazipdir.h \
       $$QUAZIP_INCLUDE_PATH/quazip/quazipfile.h \
        $$QUAZIP_INCLUDE_PATH/quazip/quazipfileinfo.h\
       $$QUAZIP_INCLUDE_PATH/quazip/quazip_global.h \
        $$QUAZIP_INCLUDE_PATH/quazip/quazip.h \
       $$QUAZIP_INCLUDE_PATH/quazip/quazipnewinfo.h \
        $$QUAZIP_INCLUDE_PATH/quazip/unzip.h \
        $$QUAZIP_INCLUDE_PATH/quazip/zip.h
 
SOURCES+= $$QUAZIP_INCLUDE_PATH/quazip/qioapi.cpp \
          $$QUAZIP_INCLUDE_PATH/quazip/JlCompress.cpp \
          $$QUAZIP_INCLUDE_PATH/quazip/quaadler32.cpp \
          $$QUAZIP_INCLUDE_PATH/quazip/quacrc32.cpp \
          $$QUAZIP_INCLUDE_PATH/quazip/quagzipfile.cpp \
          $$QUAZIP_INCLUDE_PATH/quazip/quaziodevice.cpp \
          $$QUAZIP_INCLUDE_PATH/quazip/quazip.cpp \
          $$QUAZIP_INCLUDE_PATH/quazip/quazipdir.cpp \
           $$QUAZIP_INCLUDE_PATH/quazip/quazipfile.cpp\
          $$QUAZIP_INCLUDE_PATH/quazip/quazipfileinfo.cpp \
          $$QUAZIP_INCLUDE_PATH/quazip/quazipnewinfo.cpp \
           $$QUAZIP_INCLUDE_PATH/quazip/unzip.c\
           $$QUAZIP_INCLUDE_PATH/quazip/zip.c

使用的時候只需要更改QUAZIP_INCLUDE_PATH變量的值就好了。然後再項目pro文件中添加這樣一行:

include( QuaZip.pri )

壓縮操作

       使用QuaZip也是非常非常的簡單。它的一個特點是將Zip內文件的讀寫操作封裝成QIODevice的一個子類,這樣可以使用我們常用的文件讀寫方法來對其操作了。

       下面是一個簡單的將一個字符串數組寫入壓縮文件的代碼:

    const QString& zipName( "E:/Archive.zip" );
    QuaZip zip( zipName );

    if ( !zip.open( QuaZip::mdCreate ) )
    {
        qDebug( "Could not create zip: %s", qPrintable( zipName ) );
        return;
    }

    QStringList data;
    data.append( "AAaaAAaa" );
    data.append( "BBbbBBbb" );
    data.append( "CCccCCcc" );
    data.append( "DDddDDdd" );

    foreach ( const QString& str, data )
    {
        const QString& innerName = str;

        QuaZipNewInfo newInfo( innerName );

        QuaZipFile file( &zip );
        bool ret = file.open( QIODevice::WriteOnly,
                              newInfo,      // QuaZipNewInfo結構體引用
                              Q_NULLPTR,    // 密碼
                              0,            // CRC值(默認值是0)
                              8 );          // 寫入方法(0爲文件夾,8爲普通文件)
        if ( !ret ) continue;

        // 開始寫入文件的數據了
        file.write( str.toUtf8( ) );
        file.close( );
    }

    zip.close( );

解壓操作

       解壓操作和壓縮操作相反,也是先構建QuaZip再構建QuaZipFile,當然也可以一步到位,直接使用QuaZipFile進行解壓。下面的代碼目的是從“Archive.zip”文件中解壓“聊天.txt”文本文件,解壓密碼是“63636361”。

<pre name="code" class="cpp">    const QString& zipName( "E:/Archive.zip" );
    const QString& fileName( "聊天.txt" );

    QuaZipFile file( zipName, fileName );
    if ( file.open( QIODevice::ReadOnly,    // 打開模式
                    Q_NULLPTR,              // 壓縮方法
                    Q_NULLPTR,              // 壓縮等級
                    false,                  // 是否是保留原始文件?
                    "63636361" ) )          // 壓縮密碼
    {
        ui->unzippedEdit->setPlainText( file.readAll( ) );
        file.close( );
    }
    else
    {
        ui->unzippedEdit->setPlainText( "無法打開:" + zipName + '/' + fileName );
    }

發佈了293 篇原創文章 · 獲贊 65 · 訪問量 136萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章