简体   繁体   中英

How to uncompress a COMPRESSED BLOB in m MYSQL_RES using zlib.h without using UNCOMPRESS in mysql query

I store data using insert compress {hex data} into an innodb table BLOB column. I need to programmatically select the compressed data using the mysql c api and uncompress it at the application level. Can I do so, using zlib.h and the uncompress function or inflate/deflate method?

I cannot use uncompress in the mysql query as it is a large resuult set and the data is traveling over the network onto a different server.

For example:

MYSQL_RES *results = NULL;
MYSQL_ROW row;

results = exec_query();
while ( row = mysql_fetch_row(results) )
{
        // row[0]  = uncompressed size
        // row[1]  = compressed size
        // row[2]  = compressed blob

        // does MYSQL "INSERT COMPRESS( {data} ) INTO ... " use LZ77?
        // will this work?
        // uncompress ( Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
}

I don't know if you are doing it correctly and I am not able to understand what you want.

Basically BLOB is used for storing binary data like Images, Videos, etc.

So you store them byte by byte and retrieve the same.

Additionally here you wish to compress.

So, compress the byte and store. While retrieving uncompress it.

A Sample example:

select uncompress(compress('hello world')) as sample;

在此处输入图片说明

This tutorial here shows how to insert images into MySQL DB using MySQL C API.

There is an attemp using zlib in Perl https://metacpan.org/pod/MySQL::Compress which appears to simply strip the first 4 bytes (length information) from the mysql compressed string. Using this approach with Python zlib like so:

    b=bytearray(mysql_compressed_data)
    for i in range(4):
        del b[0]
    mysql_compressed_data=bytes(b)
    zlib.decompress( mysql_compressed_data , -15)

, however, produces

zlib.error: Error -3 while decompressing data: invalid stored block lengths

Other attempts I have seen can be found here: python-2-x-zlib-decompress-data-from-mysql and here: mysql-compress-vs-php-gzcompress - none of which appear to successfully solve the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM