firemail

标题: qCompress/qUncompress [打印本页]

作者: Qter    时间: 2018-4-12 15:30
标题: qCompress/qUncompress
Qt中提供了qCompress/qUncompress来进行文件的压缩与解压,但存在两个问题:


作者: Qter    时间: 2018-4-12 15:34
  1. void MainWindow::zipUnZip()
  2. {
  3.     //zipping
  4.    Zip("/home/qt/comm_record.txt","/home/qt/demo.dor");
  5.    qDebug() << "Done zipping";

  6.    //unzipping
  7.    Unzip("/home/qt/demo.dor","/home/qt/demo_unzipped.txt");
  8.    qDebug() << "Done unzipping";
  9. }


  10. void Zip (QString filename , QString zipfilename){

  11.     QFile infile(filename);
  12.     QFile outfile(zipfilename);
  13.     infile.open(QIODevice::ReadOnly);
  14.     outfile.open(QIODevice::WriteOnly);
  15.     QByteArray uncompressedData = infile.readAll();
  16.     QByteArray compressedData = qCompress(uncompressedData,9);
  17.     outfile.write(compressedData);
  18.     infile.close();
  19.     outfile.close();
  20. }

  21. void Unzip (QString zipfilename , QString filename){
  22.     QFile infile(zipfilename);
  23.     QFile outfile(filename);
  24.     infile.open(QIODevice::ReadOnly);
  25.     outfile.open(QIODevice::WriteOnly);
  26.     QByteArray uncompressedData = infile.readAll();
  27.     QByteArray compressedData = qUncompress(uncompressedData);
  28.     outfile.write(compressedData);
  29.     infile.close();
  30.     outfile.close();
  31. }

  32. #include <QtCore/QFile>
  33. #include <QByteArray>
  34. #include <QBitArray>
复制代码





欢迎光临 firemail (http://firemail.wang:8088/) Powered by Discuz! X3