firemail
标题: qCompress/qUncompress [打印本页]
作者: Qter 时间: 2018-4-12 15:30
标题: qCompress/qUncompress
Qt中提供了qCompress/qUncompress来进行文件的压缩与解压,但存在两个问题:
- 无法很好地压缩/解压缩文件夹。
- 只能将一个文件压缩为某种格式,压缩后的文件无法通过其它解压工具打开(如:7zip,或WinRAR),只能通过qUncompress解压读取,也就是说qCompress生成的压缩文件不包含普通压缩文件的一些头信息,导致无法解压。
作者: Qter 时间: 2018-4-12 15:34
- void MainWindow::zipUnZip()
- {
- //zipping
- Zip("/home/qt/comm_record.txt","/home/qt/demo.dor");
- qDebug() << "Done zipping";
- //unzipping
- Unzip("/home/qt/demo.dor","/home/qt/demo_unzipped.txt");
- qDebug() << "Done unzipping";
- }
- 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();
- }
- #include <QtCore/QFile>
- #include <QByteArray>
- #include <QBitArray>
复制代码
欢迎光临 firemail (http://firemail.wang:8088/) |
Powered by Discuz! X3 |