'Can't open compressed file

I'm trying to compress a text file with QT:

QFile inFile("d:\\build\\Directories\\Debug\\files\\developer.txt");
bool open_file_result = inFile.open(QIODevice::ReadOnly);
QByteArray ba = inFile.readAll();

QFile file("d:\\build\\Directories\\Debug\\files\\developer.gz");
bool open_zip_result = file.open(QIODevice::WriteOnly);
QDataStream out(&file);
out << qCompress(ba);

file.close();

open_file_result and open_zip_result are both true. I can also see the zip file (it also has a size, such as 50KB) but I can't open it. I'm getting the following error:

The archive is either in unknown format or damaged.

What am I doing wrong? If you have a better/another way to compress a text file, please tell me!



Solution 1:[1]

There is a difference between the Zip compression algorithm and the Zip container. You're confusing the two.

You need to do some research into the Zip container format that will help you locate and extract a zipped file within a Zip container. Once you've gotten that file, you can apply qUncompress to that file only.

Try looking at the QuaZip, which is a library that has been written for this purpose.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 RobbieE