开发者

How to compress a few files to zip in Qt?

开发者 https://www.devze.com 2023-02-04 15:07 出处:网络
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!

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


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.)


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消