如何在Qt中将几个文件压缩为zip?

发布于 2024-10-11 06:22:52 字数 1847 浏览 2 评论 0原文

我需要在 Qt 中压缩一些文件。我正在尝试Quazip。但 zip 文件包含大小为 0kb 的文件。出了问题。有人可以帮我吗! 压缩代码如下:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

屌丝范 2024-10-18 06:22:52

如果这个项目只是一个玩具,一次一个字符可能没问题,但我无法想象一次向 zip 文件管理器添加一百万个字符会非常有效。如今,一兆字节的文件看起来非常小。因此,请在 QuaZip API 中寻找直接将文件添加到 zip 中的机制,或者至少一次添加大量数据缓冲区的机制。 (Qt 的缓冲节省了系统调用,但在大多数程序中,处理一个字符的 100 万个函数调用与处理 8k 缓冲区的 128 个函数调用相比,这一点会很明显。)

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

风柔一江水 2024-10-18 06:22:52

我得到了答案,我需要进行如下更改,

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

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());
            }
假扮的天使 2024-10-18 06:22:52

正如前面提到的,如果它是一个玩具应用程序,那么可以编写字符到字符。但实际上这确实是浪费资源。

我的解决方案是:

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

它使用 QFile 读取整个文件,然后使用 QuaZipFile 写出。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文