Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6073603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:17:20+00:00 2026-05-23T10:17:20+00:00

I need to do lzma compression and decompression on the fly. I’m receiving a

  • 0

I need to do lzma compression and decompression on the fly. I’m receiving a large file via qnetworkmanager in Qt and I need to decompress it as the data stream is downloading.

When I receive part of the data stream I need to decompress it, append to file and than free any used memory during the process. What is the best way to do this?

Now I’m trying with xz-utils, pure c api, maybe someone can suggest a better way?

following code based on this example

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

i have sleeped few hours, and now i thinking more clearly, fixed my wrong code, updated it(it’s behave just as qCompress works)

UPD3:

decompression code (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:

basic stream decompression class, following code just decompress xz stream downloaded from http server on the fly, exactly what i need:

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

and realisation:

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

all this based on example from first link, you need to replace commented code parts with your code to do something with uncompressed data.

i would like to see any suggestions to this code

you also need to connect “compressed_file_request_finished” slot to finished signal of qnetworkmanager to finish uncompressed data.

UPD5:

fixed lzCompress and lzUncompress, looks like working fine now, not sure about using LZMA_FULL_FLUSH in handle_new_data, as i read this is what i need, but still not sure, now i adapting existing code to use this…

UPD6:

you also need something like this:

/* 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

in visible range for this code to work.

UPD7:

updated code, all tested and working, i have found that liblzma not completely thread-safe, i tried to make multi-threaded compression of filelist. and it crashing very often.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:17:20+00:00Added an answer on May 23, 2026 at 10:17 am

    On this page, you will find the lzma SDK which provides source codes in different languages and some binaries: http://www.7-zip.org/sdk.html

    You have two solutions:

    • Use the C++ source code to decompress the incoming flow
    • Use the decoder binaries as an external tool in your app
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need to calculate optimum ulimit and fs.file-max values according to my own server needs.
Need a function that takes a character as a parameter and returns true if
Need a way to allow sorting except for last item with in a list.
Need to an expression that returns only things with an I followed by either
Need to locate the following pattern: The letter I followed by a space then
need ask you about some help. I have web app running in Net 2.0.
Need a function like: function isGoogleURL(url) { ... } that returns true iff URL
I need to know about Epoll On linux System. Could you recommend manual or
I need to copy hundreds of gigs of random files around on my computer
I need to send hundreds of newsletters, but would like to check first if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.