Zipping and unzipping files using Qt 5 framework

Hey everyone!

Qt provides a QByteArray class that can be used to store raw bytes and traditional 8-bit terminated strings. Moreover, Qt also provides the QString class to store string data. QString is mainly used throughout the Qt API.

Through this post, we will learn how to zip and unzip files using Qt. We will create a sample text file containing a message as follows,

demo.txt

This is a demo to zip and unzip files using Qt framework 

Pre-requisites: Qt Creator, MinGW compiler for Windows (64 bit)

Step 1: Launch Qt creator and create a new Qt console application project named TestZippingExample. Choose the default Desktop kit.

Step 2: Zip and unzip files

In order to zip files, we will be using the qCompress method of the QByteArray class. The syntax of the method is as follows,

QByteArray qCompress ( const QByteArray & data, int compressionLevel = -1 )

As mentioned in the official documentation, qCompress method compresses the data byte array and returns the compressed data in a new byte array. The compressionLevel parameter specifies how much compression should be used. Valid values are between 0 and 9, with 9 corresponding to the greatest compression (i.e. smaller compressed data) at the cost of using a slower algorithm.

Similarly, the qUncompress method uncompresses the first n bytes of data and returns a new byte array with the uncompressed data.

Step 3: Adding Qt code

Open the main.cpp file and add the following code!

main.cpp

#include <QCoreApplication>
#include <QByteArray>
#include <QBitArray>
#include <QString>
#include <QDebug>
#include <QFile>

void Zip(QString filename , QString zipfilename);
void Unzip(QString zipfilename , QString filename);
int main(int argc, char *argv[]) {
    QCoreApplication a(argc, argv);
 
    //zipping
    Zip("D:\\programs\\demo.txt","D:\\programs\\demo.zip");
    qDebug() << "Done zipping";

    //unzipping
    Unzip("D:\\programs\\demo.zip","D:\\programs\\demo_unzipped.txt");
    qDebug() << "Done unzipping";

    return a.exec();
}

void Zip (QString filename , QString zipfilename){

    QFile infile(filename);
    QFile outfile(zipfilename);
    infile.open(QIODevice::ReadOnly);
    outfile.open(QIODevice::WriteOnly);
    QByteArray uncompressedData = infile.readAll();
    QByteArray compressedData = qCompress(uncompressedData,9);
    outfile.write(compressedData);
    infile.close();
    outfile.close();
}
void Unzip (QString zipfilename , QString filename){
    QFile infile(zipfilename);
    QFile outfile(filename);
    infile.open(QIODevice::ReadOnly);
    outfile.open(QIODevice::WriteOnly);
    QByteArray uncompressedData = infile.readAll();
    QByteArray compressedData = qUncompress(uncompressedData);
    outfile.write(compressedData);
    infile.close();
    outfile.close();
}

.pro file

QT += core
QT -= gui

TARGET = TestZippingExample
CONFIG   += console
CONFIG   -= app_bundle
TEMPLATE = app
SOURCES += main.cpp

Save all changes. Build and run the application. If no errors occur, then you should see the following output!

qt-zip-unzip-output