美文网首页
使用quazip在内存中压缩文件

使用quazip在内存中压缩文件

作者: 坤kln | 来源:发表于2019-06-13 18:11 被阅读0次

quazip是一个QT下实现的压缩和解压库

下载地址:

https://github.com/stachenov/quazip/releases

quazip已经是带了内存中解压和压缩的功能,但是没有封装到JlCompress

在编译quazip时需要JlCompress类中的static bool compressFile(QuaZip* zip, QString fileName, QString fileDest);等几个private函数修改为protected,因为我是继承了JlCompress类,需要使用bool compressFile(QuaZip* zip, QString fileName, QString fileDest);函数。

下面是我实现的JlCompressEx类,只做了将文件压缩到内存中

头文件内容

#pragma once

#include "quazip/JlCompress.h"
#include "quazip/quazip.h"

#include <QBuffer>
#include <QByteArray>

class JlCompressEx : public JlCompress
{
public:
    // 文件压缩到内存buffer,zipIoDevice可以使用QBuffer zipBuffer;
    static bool CompressToBuffer(QString file, QIODevice& zipIoDevice);
    // 内存数据压缩到内存buffer
    static bool CompressToBuffer(QByteArray& sourceData, QString fileName, QIODevice& zipIoDevice);
    // 文件压缩到内存buffer
    static bool CompressToBuffer(QStringList files, QIODevice& zipIoDevice);

public:
    // 压缩数据
    static bool CompressBuffer(QuaZip& zip, QByteArray& sourceData, QString fileName);
};

cpp文件内容

#include "JlCompressEx.h"

bool JlCompressEx::CompressToBuffer(QString file, QIODevice& zipIoDevice)
{
    QuaZip zip(&zipIoDevice);
    //QDir().mkpath(QFileInfo(fileCompressed).absolutePath());
    if (!zip.open(QuaZip::mdCreate))
    {
        return false;
    }
    // 
    if (!compressFile(&zip, file, QFileInfo(file).fileName())) {
        return false;
    }

    // 
    zip.close();
    if (zip.getZipError() != UNZ_OK) {
        return false;
    }

    return true;
}

bool JlCompressEx::CompressToBuffer(QByteArray& sourceData, QString fileName, QIODevice& zipIoDevice)
{
    QuaZip zip(&zipIoDevice);
    //QDir().mkpath(QFileInfo(fileCompressed).absolutePath());
    if (!zip.open(QuaZip::mdCreate))
    {
        return false;
    }

    if (!CompressBuffer(zip, sourceData, fileName))
    {
        return false;
    }

    // 
    zip.close();
    if (zip.getZipError() != UNZ_OK) {
        return false;
    }

    return true;
}

bool JlCompressEx::CompressToBuffer(QStringList files, QIODevice& zipIoDevice)
{
    // 
    QuaZip zip(&zipIoDevice);
    //QDir().mkpath(QFileInfo(fileCompressed).absolutePath());
    if (!zip.open(QuaZip::mdCreate))
    {
        return false;
    }

    // 
    QFileInfo info;
    for (int index = 0; index < files.size(); ++index) {
        const QString & file(files.at(index));
        info.setFile(file);
        if (!info.exists() || !compressFile(&zip, file, info.fileName())) {
            return false;
        }
    }

    // 
    zip.close();
    if (zip.getZipError() != 0)
    {
        return false;
    }

    return true;
}

bool JlCompressEx::CompressBuffer(QuaZip& zip, QByteArray& sourceData, QString fileName)
{
    // 
    if (zip.getMode() != QuaZip::mdCreate &&
        zip.getMode() != QuaZip::mdAppend &&
        zip.getMode() != QuaZip::mdAdd)
    {
        return false;
    }
    // 
    QuaZipFile outFile(&zip);
    if (!outFile.open(QIODevice::WriteOnly, QuaZipNewInfo(fileName)))
    {
        return false;
    }
    // 
    if (outFile.write(sourceData) != sourceData.size())
    {
        return false;
    }
    if (outFile.getZipError() != UNZ_OK)
    {
        return false;
    }

    // 
    outFile.close();
    if (outFile.getZipError() != UNZ_OK)
    {
        return false;
    }

    return true;
}

使用测试

QBuffer zipBuffer;
JlCompressEx::CompressToBuffer(fileData, "data.json", zipBuffer);
QByteArray& bufferArray = zipBuffer.buffer();

相关文章

  • 使用quazip在内存中压缩文件

    quazip是一个QT下实现的压缩和解压库 下载地址: https://github.com/stachenov/...

  • PHP 实现文件压缩解压zip格式

    在php中,有时我们需要使用到压缩文件操作,压缩文件可以节省磁盘空间;且压缩文件更小,便于网络传输,效率高,下面我...

  • 使用Java API进行tar.gz文件及文件夹压缩解压缩

    在java(JDK)中我们可以使用ZipOutputStream去创建zip压缩文件,(参考我之前写的文章 使用j...

  • QuaZip源码编译及使用

    QuaZip是用C++、Qt对Zlib进行封装,用于压缩和解压文件的库。 源码下载地址:QuaZip源码官方下载 ...

  • 压缩解压命令

    注意:windows的压缩文件格式在linux中未必能打开,但是linux中的压缩文件在windows都可以打开。...

  • mac加密压缩

    mac中利用终端加密压缩文件/文件夹: 首先打开终端, 找到文件夹目录 cd 路径 : 使用命令压缩 压缩文件夹 ...

  • python 递归解压缩zip文件

    如何进行递归的对压缩文件进行解压 在使用zipfile库解压压缩文件的时候,有时候会遇到一种情况,就是一个压缩文件...

  • sse中内存对齐问题

    c++ sse中无论声明栈内存还是堆内存都需要声明内存对齐,在VC++中:堆内存分配使用_aligned_mall...

  • Linux命令学习----zgrep学习

    使用zgrep命令可以在压缩文件中调用grep按正则表达式来搜索 语法格式 zgrep [参数] [模式] [文件...

  • linux命令tar命令详解(压缩,解压)

    tar命令参数:-c 创建压缩文件-x 解压缩文件-t 查看压缩文件内容-z 使用Gzip方式压缩或解压-v 显示...

网友评论

      本文标题:使用quazip在内存中压缩文件

      本文链接:https://www.haomeiwen.com/subject/vdhefctx.html