defiant
defiant

Reputation: 3341

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");
            }

Upvotes: 2

Views: 4887

Answers (3)

domdetre
domdetre

Reputation: 51

As mentioned earlier, if it is a toy app its okay to write char-to-char. But in real it is really wasting resources.

My solution is:

(QuaZipFile) zipout->write( (QFile)inFile->readAll() );

It reads the entire file with QFile than writes out with QuaZipFile.

Upvotes: 0

sarnold
sarnold

Reputation: 104050

If this project is just a toy, character-at-a-time is probably fine, but I can't imagine adding one million characters one-at-a-time to a zip file manager would be very efficient. And a one megabyte file looks mighty small these days. So hunt around the QuaZip API for mechanisms to either add files directly to the zip, or at least large buffers of data at a time. (Qt's buffering saves system calls but one million function calls working on one character vs 128 function calls working with 8k buffers is going to be noticeable in most programs.)

Upvotes: 3

defiant
defiant

Reputation: 3341

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());
            }

Upvotes: 1

Related Questions