C language uses zlib to implement gzip compression and gzip decompression of text characters

Source: Internet
Author: User
Tags uncompress

Many of the methods found on the network will lose characters when extracting the string, here is the workaround:

Http://stackoverflow.com/questions/21186535/compressing-decompressing-char-array-using-zlib-some-characters-missing

Based on this, the author modified a bit, because it is a beginner, only according to the compiler does not error the principle of modified a bit, can run

Open vc++6.0 new Console program, configure the Zdll.lib, put Zlib1.dll in the right place
The main program main.cpp is as follows
#include <string.h>#include<stdio.h>#include<stdlib.h>#include"Zlib.h"intMain () {Const Char*istream ="some foo kanji"; ULong Srclen= strlen (IStream) +1;//+1 for the trailing ' + 'ULong Destlen = Compressbound (Srclen);//should estimate size//needed for the bufferUnsignedChar* Ostream = (unsignedChar*)malloc(Destlen); intres = compress (Ostream, &destlen, (ConstUnsignedChar*) IStream, Srclen); //Destlen is now the size of actuall buffer needed for compression//You don ' t want to uncompress whole buffer later, just the used part if(res = =z_buf_error) {printf ("Buffer was too small!\n"); return 1; } if(res = =z_mem_error) {printf ("Not enough memory for compression!\n"); return 2; } unsignedChar*i2stream =ostream; Char* O2stream = (Char*)malloc(Srclen); ULong DestLen2= Destlen;//Destlen is the actual size of the compressed buffer intDes = uncompress ((unsignedChar*) O2stream, &Srclen, I2stream, destLen2); printf ("%s\n", O2stream); return 0;}

Accessories download

C language uses zlib to implement gzip compression and gzip decompression of text characters

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.