windows下通过qt使用zlib将文件夹压缩成zip格式,并且通过windows解压工具能成功解压
时间: 2024-01-24 12:02:28 浏览: 145
可以使用Qt的zlib库来完成文件夹压缩成zip格式的功能。以下是一个简单的示例代码:
```cpp
#include <QtCore>
#include <zlib.h>
void compressFile(const QString& filePath, z_stream& stream, QFile& outputFile)
{
QFile inputFile(filePath);
if (!inputFile.open(QIODevice::ReadOnly)) {
qWarning() << "Failed to open input file:" << filePath;
return;
}
QByteArray inputBuffer;
inputBuffer.resize(stream.avail_in);
int flush = Z_NO_FLUSH;
do {
// 读取输入文件到输入缓冲区
int bytesRead = inputFile.read(inputBuffer.data(), inputBuffer.size());
if (bytesRead == -1) {
qWarning() << "Failed to read input file:" << filePath;
break;
}
// 将输入缓冲区中的数据压缩到输出缓冲区
stream.next_in = reinterpret_cast<Bytef*>(inputBuffer.data());
stream.avail_in = bytesRead;
do {
QByteArray outputBuffer;
outputBuffer.resize(stream.avail_out);
stream.next_out = reinterpret_cast<Bytef*>(outputBuffer.data());
stream.avail_out = outputBuffer.size();
int ret = deflate(&stream, flush);
if (ret == Z_STREAM_ERROR) {
qWarning() << "Failed to compress input file:" << filePath << "Error code:" << stream.msg;
return;
}
// 将压缩后的数据写入输出文件中
int bytesWritten = outputFile.write(outputBuffer.data(), outputBuffer.size());
if (bytesWritten == -1) {
qWarning() << "Failed to write output file:" << outputFile.fileName();
return;
}
} while (stream.avail_out == 0);
} while (flush != Z_FINISH);
inputFile.close();
}
void compressFolder(const QString& folderPath, z_stream& stream, QFile& outputFile)
{
QDirIterator it(folderPath, QDir::AllEntries | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
while (it.hasNext()) {
QString filePath = it.next();
if (QFileInfo(filePath).isDir()) {
continue;
}
QString relativeFilePath = QDir(folderPath).relativeFilePath(filePath);
// 将文件名写入zip文件中
QByteArray fileNameData = relativeFilePath.toUtf8();
int fileNameLength = fileNameData.size();
outputFile.write(reinterpret_cast<const char*>(&fileNameLength), sizeof(fileNameLength));
outputFile.write(fileNameData);
compressFile(filePath, stream, outputFile);
}
}
void compressZip(const QString& sourcePath, const QString& targetPath)
{
QFile outputFile(targetPath);
if (!outputFile.open(QIODevice::WriteOnly)) {
qWarning() << "Failed to open output file:" << targetPath;
return;
}
// 初始化zlib压缩上下文
z_stream stream;
stream.zalloc = Z_NULL;
stream.zfree = Z_NULL;
stream.opaque = Z_NULL;
if (deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -MAX_WBITS, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK) {
qWarning() << "Failed to initialize zlib context.";
outputFile.close();
return;
}
// 压缩文件夹
compressFolder(sourcePath, stream, outputFile);
// 结束zlib压缩上下文
deflateEnd(&stream);
outputFile.close();
}
int main(int argc, char* argv[])
{
QCoreApplication app(argc, argv);
QString sourcePath = "C:/path/to/folder";
QString targetPath = "C:/path/to/archive.zip";
compressZip(sourcePath, targetPath);
return app.exec();
}
```
这个示例代码中,我们使用了Qt的QFile和QDirIterator类来遍历文件夹,使用zlib库来进行压缩,最终生成一个zip格式的压缩文件。在压缩文件时,我们先将文件名写入zip文件中,然后再将文件内容进行压缩。这样,在解压缩时,我们可以按照文件名逐个解压缩文件内容。需要注意的是,这个示例代码只适用于Windows平台上的解压工具,因为Windows平台上的解压工具对文件名编码方式的处理与其他平台上的解压工具有所不同。如果需要在其他平台上使用,可能需要进行一些调整。
阅读全文