Gzip解压代码片段


talk is cheap, show me the code

代码片段

1f8b080000000000000335ce410b82401086e173fe8a65efb69217a5b54b6010
145106b6375d27d772d5dc49b45f5f66ddde0f8687e10a75b9b208e10a926c8c
4f6ac08428c4c686c7b3e802baae2b840aed6868801239ad8022f4c8466049a4
4a5a03189ca3d0f628615f92fd4d9ed6d930c68c173a27a6950165f7c2987953
e5bf6ba17b255c3c40e939a93e5e4f9b72d885be49ddcb2b7644276ffb8588b7
fe179e3ccea6efad37d3b4718bc6000000

详细可以参考:https://blog.csdn.net/vevenlcf/article/details/100172107

python代码

import binascii
a=0x1f8b0800000000000003bd55bd4ec330109ec95398ecc1b0315c83502bbaa3a20e88c1244789709dc8710bdd9010125b6165468247e8c0e350e031701c9a3a81022a842cbee47e3edff79d2eb075d6e76488328d62d17037d6d65d822288c348f41aee5e67c7db74b77c074671c8f4c740f90e2120510da4f09263c952f46175bfd9daee6cef4f1fcfa7e39bd7f3cb83031f683948a7551fe0e8a300aa8f921738133dede9695766959c459115423e7798080806a98afb9e924ca49c29ddddac0ea851827e80408d512e02760204b15028d4bcc31087c8e304a5e9b070533bcb2e0874f13df24eec68fd2527da3bc5436fc676d1f10703b2300b9aa4ac8f0d57c901bab36ee7497082a3792765ad3257e526f344dbcaf9f19d154d3f0c191f58f2b72c72f2009a4798972cc364e5647d0df715cc2e8b784a9ab144d241d6d74ad40856084e5892f0283093b41c24d08a5c8b28a8cafa31ae2ce5f364f27c352e049ddedd3f3dde9a01fd1359db7aa848a16dc6bc3842a997042ec7c34fc7a9dd6ad60bd05c1aa13629a7970f2fd717b549d9edd6cd6937963c3c8d426b62fe6d603ec1fefdb47e233550f367ac2c6c6b95428a6789bf0ed49c8ef39e902ff737d08d60ed7e070000

b = hex(a)
b = b[2:]
print(b)

c = binascii.a2b_hex(b)
print(c)

import gzip

txt=gzip.decompress(c)
print(len(txt),txt)
with open("3.txt", "wb+") as f:
    f.write(txt)

c 语言代码

#include <stdio.h>
#include <stdlib.h>
#include <zlib.h>

static int OWNUncompress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
{
    z_stream stream;
    int err;
 
    stream.next_in = (const Bytef*)source;
    stream.avail_in = (uInt)sourceLen;
    /* Check for source > 64K on 16-bit machine: */
    if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
 
    stream.next_out = dest;
    stream.avail_out = (uInt)*destLen;
    if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
 
    stream.zalloc = (alloc_func)0;
    stream.zfree = (free_func)0;
    err = inflateInit2_(&stream, MAX_WBITS + 16, ZLIB_VERSION, (int)sizeof(z_stream));
    if (err != Z_OK) return err;
 
    err = inflate(&stream, Z_FINISH);
    if (err != Z_STREAM_END) {
        inflateEnd(&stream);
        if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
            return Z_DATA_ERROR;
        return err;
    }
    *destLen = stream.total_out;
 
    err = inflateEnd(&stream);
    return err;
}

int main()
{  
   char buf[1000]={};
   FILE* fp=fopen("1.txt", "rb+");
   fread(buf, 1, 1000, fp);
   fclose(fp); 

   int buf_len= 1000;
   char *tmpContent = malloc(1000 * sizeof(Bytef));
   uLongf tmpContentSizeTmp = 1000;
                     
   if (!tmpContent
             || Z_OK != OWNUncompress((Bytef*)tmpContent, &tmpContentSizeTmp, \
                                                buf, 
                                                buf_len))
   {
		printf("MyUncompress failed or content is NULL\n");
		return ;
   }
   FILE* fp2=fopen("2.txt", "wb+");
   fwrite(tmpContent, tmpContentSizeTmp, 1, fp2);
   fclose(fp2);
   printf("%d\n", tmpContentSizeTmp);
}



 Toc
 Tags