qtcompressionzip

How to compress a few files to zip in Qt?


I need to zip a few files in Qt. I am trying Quazip. But the zip file contains files with 0kb size. Something goes wrong. Can somebody help me out here! The code for zipping is given here:

QString testZip = location + "/tempTheme/test.zip";
            QuaZip zip(testZip);
            zip.setFileNameCodec("IBM866");            
            if(!zip.open(QuaZip::mdCreate)) {
              qWarning("testCreate(): zip.open(): %d", zip.getZipError());
            }
            zip.setComment("Test comment");
            QFileInfoList files=QDir(location + "/tempTheme/").entryInfoList();
            QFile inFile;
            QuaZipFile outFile(&zip);
            char c;
            foreach(QFileInfo file, files) {
              if(!file.isFile()||file.fileName()=="test.zip") continue;
              inFile.setFileName(file.fileName());
              if(!inFile.open(QIODevice::ReadOnly)) {
                qWarning("testCreate(): inFile.open(): %s", inFile.errorString().toLocal8Bit().constData());
              }
              if(!outFile.open(QIODevice::WriteOnly, QuaZipNewInfo(inFile.fileName(), inFile.fileName()))) {
                qWarning("testCreate(): outFile.open(): %d", outFile.getZipError());
              }
              while(inFile.getChar(&c)&&outFile.putChar(c));
              if(outFile.getZipError()!=UNZ_OK) {
                qWarning("testCreate(): outFile.putChar(): %d", outFile.getZipError());
              }
              outFile.close();
              if(outFile.getZipError()!=UNZ_OK) {
                qWarning("testCreate(): outFile.close(): %d", outFile.getZipError());
              }
              inFile.close();
            }
            zip.close();
            if(zip.getZipError()!=0) {
              qWarning("testCreate(): zip.close(): %d", zip.getZipError());
             QMessageBox msgInfo;
             msgInfo.information(this, "blah", "done");
            }

Solution

  • I got the answer, I need to make changes as following,

    QString testZip = location + "/test.zip";
                QuaZip zip(testZip);
                zip.setFileNameCodec("IBM866");
                if(!zip.open(QuaZip::mdCreate)) {
                  qWarning("testCreate(): zip.open(): %d", zip.getZipError());
                }
                //zip.setComment("Test comment");
                QFileInfoList files=QDir(location + "/tempTheme/").entryInfoList();
                QFile inFile;
                QFile inFileTmp;
                QuaZipFile outFile(&zip);
                char c;
                foreach(QFileInfo file, files) {
                  if(!file.isFile()) continue;
                  inFileTmp.setFileName(file.fileName());
                  inFile.setFileName(file.filePath());
                  if(!inFile.open(QIODevice::ReadOnly)) {
                    qWarning("testCreate(): inFile.open(): %s", inFile.errorString().toLocal8Bit().constData());
                  }
                  if(!outFile.open(QIODevice::WriteOnly, QuaZipNewInfo(inFileTmp.fileName(), inFile.fileName()))) {
                    qWarning("testCreate(): outFile.open(): %d", outFile.getZipError());
                  }
                  while(inFile.getChar(&c)&&outFile.putChar(c));
                  if(outFile.getZipError()!=UNZ_OK) {
                    qWarning("testCreate(): outFile.putChar(): %d", outFile.getZipError());
                  }
                  outFile.close();
                  if(outFile.getZipError()!=UNZ_OK) {
                    qWarning("testCreate(): outFile.close(): %d", outFile.getZipError());
                  }
                  inFile.close();
                }
                zip.close();
                if(zip.getZipError()!=0) {
                  qWarning("testCreate(): zip.close(): %d", zip.getZipError());
                }