简体   繁体   中英

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM