PDA

View Full Version : Qt file decompression



CodeHunt
15th April 2012, 05:56
How to decompress a gzip file created by Qt/zlib ?
I wrote a chunk of code to handle the decompression, it creates the output file but nothing is written to it. Can someone help me get the original file as is before compression ?

ChrisW67
15th April 2012, 10:43
Qt qCompress does not produce, and qUncompress does not uncompress, gzip files.
If you have used zlib directly to produce a compressed file then you use the opposite zlib functions to decompress it.

How about you show us a small, compilable program that creates this file and then fails to decompress it, then we might have some idea what you actually done.

wysota
15th April 2012, 12:14
If you know the size of the original data, you can decompress a zlib compressed file using qCompress by prepending the data buffer with the expected size as a 32-bit unsigned big-endian integer.

CodeHunt
25th April 2012, 14:20
QFile inFile("x.x");
inFile.Open(QIODevice::ReadOnly);
QByteArray ba=inFile.readAll();

QFile file("xx.abc");
file.open(QIODevice::WriteOnly);
QDataStream out(&file);
out<<qCompress(ba);

Decompress:

QFile inFile("C:\\xx.abc");
inFile.open(QIODevice::ReadOnly);
QByteArray ba=inFile.readAll();

QFile file("C:\\y.abc");
file.open(QIODevice::WriteOnly);
QDataStream out(&file);
out<<qUncompress(ba);


I am using zlib header for this

ChrisW67
25th April 2012, 23:18
Out of order...


I am using zlib header for this

You are not using any zlib function directly here. qCompress and qUnCompress using the zlib library but do not produce the same output as a zlib stream. Ultimately it is irrelevant because your problem lies elsewhere.





QFile inFile("x.x");
inFile.Open(QIODevice::ReadOnly);
QByteArray ba=inFile.readAll();

QFile file("xx.abc");
file.open(QIODevice::WriteOnly);
QDataStream out(&file);
out<<qCompress(ba);

// Decompress:

QFile inFile("C:\\xx.abc");
inFile.open(QIODevice::ReadOnly);
QByteArray ba=inFile.readAll();

QFile file("C:\\y.abc");
file.open(QIODevice::WriteOnly);
QDataStream out(&file);
out<<qUncompress(ba);


You are misusing QDataStream. If you write a file with QDataStream then you need to read it with QDataStream; you do not do this. Secondly, QDataStream is a serialisation mechanism for data structures, not an arbitrary binary writer. You should use QIODevice::write() and QIODevice::read() or readAll() directly for this application.



#include <QtCore>

int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);

QFile origFile("xx.txt");
if (origFile.open(QIODevice::ReadOnly)) {
QByteArray ba = origFile.readAll();
origFile.close();

QFile file("xx.abc");
if (file.open(QIODevice::WriteOnly)) {
qint64 count = file.write(qCompress(ba));
qDebug() << "Wrote" << count << "compressed bytes";
file.close();
}
}

//Decompress:

QFile inFile("xx.abc");
if (inFile.open(QIODevice::ReadOnly)) {
QByteArray ba = inFile.readAll();

QFile file("yy.txt");
if (file.open(QIODevice::WriteOnly)) {
qint64 count = file.write(qUncompress(ba));
qDebug() << "Wrote" << count << "uncompressed bytes";
file.close();
}
}

return 0;
}