如何在Qt中将几个文件压缩为zip?
我需要在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果这个项目只是一个玩具,一次一个字符可能没问题,但我无法想象一次向 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.)
我得到了答案,我需要进行如下更改,
I got the answer, I need to make changes as following,
正如前面提到的,如果它是一个玩具应用程序,那么可以编写字符到字符。但实际上这确实是浪费资源。
我的解决方案是:
它使用 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:
It reads the entire file with QFile than writes out with QuaZipFile.