Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ socket
hi all
 
i use socket to fetch the html page
 
and the request tag "Accept-Encoding:gzip" so i recieve a compress data
 
i try use zlib to uncompress but i failed when i call function inflate
 
i use the code follow:

#define CHUNK 16384
int inflate_read(char *source,int len,char **dest,int gzip) 
{ 
	int ret; 
	unsigned have; 
	z_stream strm; 
	unsigned char out[CHUNK] = {0}; 
	int totalsize = 0; 
 
	/* allocate inflate state */ 
	strm.zalloc = Z_NULL; 
	strm.zfree = Z_NULL; 
	strm.opaque = Z_NULL; 
	strm.avail_in = 0; 
	strm.next_in = Z_NULL; 
 
	if(gzip) 
		ret = inflateInit2(&strm, 47); 
	else 
		ret = inflateInit(&strm); 
 
	if (ret != Z_OK) 
		return ret; 
 
	strm.avail_in = len; 
	strm.next_in = (Bytef*)source; 
 
	/* run inflate() on input until output buffer not full */ 
	do { 
		strm.avail_out = CHUNK; 
		strm.next_out = out; 
		ret = inflate(&strm, Z_NO_FLUSH); 
		assert(ret != Z_STREAM_ERROR); /* state not clobbered */ 
		switch (ret) { 
		case Z_NEED_DICT: 
			ret = Z_DATA_ERROR; /* and fall through */ 
		case Z_DATA_ERROR: 
		case Z_MEM_ERROR: 
			inflateEnd(&strm); 
			return ret; 
		} 
		have = CHUNK - strm.avail_out; 
		totalsize += have; 
		*dest = (char*)realloc(*dest,totalsize); 
		memcpy(*dest + totalsize - have,out,have); 
	} while (strm.avail_out == 0); 
 
	/* clean up and return */ 
	(void)inflateEnd(&strm); 
	return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR; 
} 
 

someone can help me how to uncompress data and uncompress data with "Transfer-Encoding: chunked"
 
thank you
Posted 3-Jan-12 19:46pm
jeansea1.7K
Comments
Albert Holguin at 8-Jan-12 1:33am
   
This looks like C... are you sure it's tagged correctly? Also, posting a solution to your question moves your question out of the "unanswered" category, reducing your odds of getting help, so don't do that in the future.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

This code gets html page. Put html page's address to fetch instead of http://example.com. Below posted code is from http://curl.haxx.se/libcurl/c/simple.html[^]
 
#include <stdio.h>
#include 
 
int main(void)
{
  CURL *curl;
  CURLcode res;
 
  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
    res = curl_easy_perform(curl);
 
    /* always cleanup */ 
    curl_easy_cleanup(curl);
  }
  return 0;
}
Y</stdio.h>
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 310
1 OriginalGriff 285
2 Afzaal Ahmad Zeeshan 200
3 Sergey Alexandrovich Kryukov 195
4 BillWoodruff 180
0 OriginalGriff 6,499
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,193
3 Manas Bhardwaj 4,657
4 Maciej Los 4,120


Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 14 May 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100