C ++ lzma按部分压缩和解压缩大流

时间:2011-06-26 21:05:06

标签: c++ qt lzma

我需要动态执行lzma压缩和解压缩。我在Qt中通过qnetworkmanager收到一个大文件,我需要在下载数据流时解压缩它。

当我收到部分数据流时,我需要将其解压缩,附加到文件,然后在此过程中释放任何已用内存。这样做的最佳方式是什么?

现在我正在尝试使用xz-utils,纯c api,或许有人可以提出更好的方法吗?

以下代码基于this示例

UPD2:

extern "C" void *lz_alloc(void *opaque, size_t nmemb, size_t size)
{
void *p = NULL;
try{
    p = new char [size];
}
catch(std::bad_alloc &ba)
{
    p = NULL;
}
return p;
}

extern "C" void lz_free(void *opaque, void *ptr)
{
delete [] (char*)ptr;
}


QByteArray lzCompress(QByteArray data)
{
QByteArray arr;
lzma_check check = LZMA_CHECK_CRC64;
lzma_stream strm = LZMA_STREAM_INIT; /* alloc and init lzma_stream struct */
lzma_allocator al;
al.alloc = lz_alloc;
al.free = lz_free;
strm.allocator = &al;
byte *in_buf;
byte out_buf [OUT_BUF_MAX];
size_t in_len;  /* length of useful data in in_buf */
size_t out_len; /* length of useful data in out_buf */
lzma_ret ret_xz;

/* initialize xz encoder */
ret_xz = lzma_easy_encoder (&strm, 9 | LZMA_PRESET_EXTREME, check);
if (ret_xz != LZMA_OK) {
    return QByteArray();
}

in_len = data.size();
in_buf = (byte*)data.data();
strm.next_in = in_buf;
strm.avail_in = in_len;

do {
    strm.next_out = out_buf;
    strm.avail_out = OUT_BUF_MAX;
    ret_xz = lzma_code (&strm, LZMA_FINISH);

    out_len = OUT_BUF_MAX - strm.avail_out;
    arr.append((char*)out_buf, out_len);
    out_buf[0] = 0;
} while (strm.avail_out == 0);
lzma_end (&strm);
return arr;
}

我已经睡了几个小时,现在我更清楚地思考,修复了错误的代码,更新了它(它的行为就像qCompress一样)

UPD3:

解压缩代码(qUncompress like behavior)

QByteArray lzUncompress(QByteArray data)
{
lzma_stream strm = LZMA_STREAM_INIT; /* alloc and init lzma_stream struct */
const uint32_t flags = LZMA_TELL_UNSUPPORTED_CHECK | LZMA_CONCATENATED;
const uint64_t memory_limit = UINT64_MAX; /* no memory limit */
byte *in_buf;
uint8_t out_buf [OUT_BUF_MAX];
size_t in_len;  /* length of useful data in in_buf */
size_t out_len; /* length of useful data in out_buf */
lzma_ret ret_xz;
QByteArray arr;

ret_xz = lzma_stream_decoder (&strm, memory_limit, flags);
if (ret_xz != LZMA_OK) {
    return QByteArray();
}

in_len = data.size();
in_buf = (byte*)data.data();

strm.next_in = in_buf;
strm.avail_in = in_len;
do {
    strm.next_out = out_buf;
    strm.avail_out = OUT_BUF_MAX;
    ret_xz = lzma_code (&strm, LZMA_FINISH);

    out_len = OUT_BUF_MAX - strm.avail_out;
    arr.append((char*)out_buf, out_len);
    out_buf[0] = 0;
} while (strm.avail_out == 0);
lzma_end (&strm);
return arr;
}

UPD4:

基本流解压缩类,下面的代码只是解压缩从http服务器上下载的xz流,正是我需要的:

class lz_stream_decompressor : public QObject
{
Q_OBJECT
public:
lz_stream_decompressor(QNetworkReply *r, QNetworkAccessManager *q, const QString &str, unsigned long sz): flags(LZMA_TELL_UNSUPPORTED_CHECK | LZMA_CONCATENATED), memory_limit(UINT64_MAX), state(0), total_upd_size(sz)
{
    repl = r;
    qnm = q;
    path = str;
    strm.next_in = NULL;
    strm.avail_in = 0;
    strm.total_in = 0;
    strm.next_out = NULL;
    strm.avail_out = 0;
    strm.total_out = 0;
    strm.allocator = NULL;
    strm.internal = NULL;
    strm.reserved_ptr1 = NULL;
    strm.reserved_ptr2 = NULL;
    strm.reserved_ptr3 = NULL;
    strm.reserved_ptr4 = NULL;
    strm.reserved_int1 = 0;
    strm.reserved_int2 = 0;
    strm.reserved_int3 = 0;
    strm.reserved_int4 = 0;
    strm.reserved_enum1 = LZMA_RESERVED_ENUM;
    strm.reserved_enum2 = LZMA_RESERVED_ENUM;
    ret_xz = lzma_stream_decoder (&strm, memory_limit, flags);
    if (ret_xz != LZMA_OK)
    {
        state = -1;
        repl->abort();
    }
    else
    {
        connect(repl, SIGNAL(downloadProgress(qint64,qint64)), SLOT(handle_new_data(qint64,qint64)));
        connect(q, SIGNAL(finished(QNetworkReply*)), SLOT(compressed_file_request_finished(QNetworkReply*)));
        QFile(path).rename(path + ".tmp");
    }
}
~lz_stream_decompressor()
{
/*        if(repl)
        delete repl; */
    lzma_end (&strm);
}
const short get_state()
{
    return state;
}
signals:
void finished();

public slots:
void handle_new_data(qint64 bytesReceived, qint64 bytesTotal);
void compressed_file_request_finished(QNetworkReply*);
private:
QNetworkReply *repl;
QNetworkAccessManager *qnm;
lzma_stream strm;
const uint32_t flags;
const uint64_t memory_limit; /* no memory limit */
short state;
byte *in_buf;
byte out_buf [OUT_BUF_MAX];
size_t in_len;  /* length of useful data in in_buf */
size_t out_len; /* length of useful data in out_buf */
lzma_ret ret_xz;
QString path;
unsigned long &total_upd_size;
};

并实现:

void lz_stream_decompressor::handle_new_data(qint64 bytesReceived, qint64 bytesTotal)
{
if(repl->error() != QNetworkReply::NoError)
{//TODO: handle error here
    QFile(path).remove();
    QFile(path + ".tmp").rename(path);
    return;
}
total_upd_size -= repl->bytesAvailable();
QByteArray data = repl->readAll();
in_len = data.size();
in_buf = (byte*)data.data();
strm.next_in = in_buf;
strm.avail_in = in_len;

do {
    strm.next_out = out_buf;
    strm.avail_out = OUT_BUF_MAX;
    ret_xz = lzma_code (&strm, LZMA_RUN);
    out_len = OUT_BUF_MAX - strm.avail_out;
    QFile file(path);
    if(file.open(QIODevice::WriteOnly | QIODevice::Append))
    {
        file.write(QByteArray((char*)out_buf, (int)out_len));
        file.close();
    }
    out_buf[0] = 0;
} while (strm.avail_out == 0);
}

void lz_stream_decompressor::compressed_file_request_finished(QNetworkReply* repl)
{
if(repl->error() != QNetworkReply::NoError)
{//TODO: handle error here
    QFile(path).remove();
    QFile(path + ".tmp").rename(path);
    emit finished();
    return;
}
total_upd_size -= repl->bytesAvailable();
QByteArray data = repl->readAll();
in_len = data.size();
in_buf = (byte*)data.data();
strm.next_in = in_buf;
strm.avail_in = in_len;

do {
    strm.next_out = out_buf;
    strm.avail_out = OUT_BUF_MAX;
    ret_xz = lzma_code (&strm, LZMA_FINISH);
    out_len = OUT_BUF_MAX - strm.avail_out;
    QFile file(path);
    if(file.open(QIODevice::WriteOnly | QIODevice::Append))
    {
        file.write(QByteArray((char*)out_buf, (int)out_len));
        file.close();
    }
    out_buf[0] = 0;
} while (strm.avail_out == 0);
repl->deleteLater();
QFile(path + ".tmp").remove();
emit finished();
}

所有这一切都基于第一个链接的示例,您需要用您的代码替换已注释的代码部分,以便对未压缩的数据执行某些操作。

我希望看到对此代码的任何建议

您还需要将“compressed_file_request_finished”插槽连接到qnetworkmanager的完成信号,以完成未压缩的数据。

UPD5:

修复lzCompress和lzUncompress,看起来工作正常,不确定在handle_new_data中使用LZMA_FULL_FLUSH,因为我读到这是我需要的,但仍然不确定,现在我调整现有代码使用这个...

UPD6:

你还需要这样的东西:

/* read/write buffer sizes */
#define IN_BUF_MAX  409600
#define OUT_BUF_MAX 409600
/* analogous to xz CLI options: -0 to -9 */
#define COMPRESSION_LEVEL 7

/* boolean setting, analogous to xz CLI option: -e */
#define COMPRESSION_EXTREME true

在可见范围内,此代码可以正常工作。

UPD7:

更新了代码,所有测试和工作,我发现liblzma不是完全线程安全的,我试图对文件列表进行多线程压缩。它经常崩溃。

1 个答案:

答案 0 :(得分:7)

在此页面上,您将找到lzma SDK,它提供不同语言和一些二进制文件的源代码:http://www.7-zip.org/sdk.html

您有两种解决方案:

  • 使用C ++源代码解压缩传入流
  • 将解码器二进制文件用作应用程序中的外部工具