web服务交互中HTTP数据内容GZIP,ZLIB格式压缩与解压缩封装(共享)
//dll内部封装API格式
//gzip BOOL fnZlibDecompressPacket (__IN_PARAM unsigned char* gZlibDataBuffer, __IN_PARAM size_t gZLibDataLength, __OUT_PARAM char* pChDataBuffer, __OUT_PARAM DWORD *pdwLength); BOOL fnZlibCompressPacket (__IN_PARAM unsigned char* gZlibDataBuffer, __IN_PARAM size_t gZLibDataLength, __OUT_PARAM char * pchGzipOutData, __OUT_PARAM DWORD *pdwLength); //zlib BOOL fnGzipDecompressPacket (__IN_PARAM unsigned char* gzipDataBuffer, __IN_PARAM size_t gzipDataBuffer_Length, __OUT_PARAM char* pChDataBuffer, __OUT_PARAM DWORD *pdwLength); BOOL fnGzipCompressPacket (__IN_PARAM const char * gzipDataBuffer, __IN_PARAM int gzipDataBuffer_Length, __OUT_PARAM char * dstDataBuffer, __OUT_PARAM int *pdwLength);
//GZIP调用核心代码演示
typedef BOOL (*UncompressionGzipPacketToBuffer) (unsigned char* gZipDataBuffer, size_t gZipDataLength, char * pDataBuffer,DWORD *dwLength); typedef BOOL (*CompressionBufferPacketToGzip) ( unsigned char* chDataBuffer, size_t chDataLength, char* pGzipBuffer,DWORD *dwLength); HINSTANCE hInst=NULL; UncompressionGzipPacketToBuffer UngzipPacket; CompressionBufferPacketToGzip ComGzipBuffer; hInst=LoadLibrary(_T("GzipZlibdll.dll")); if (hInst==NULL) { return -1; } UngzipPacket=(UncompressionGzipPacketToBuffer)GetProcAddress(hInst,"fnGzipDecompressPacket"); ComGzipBuffer=(CompressionBufferPacketToGzip)GetProcAddress(hInst,"fnGzipCompressPacket"); if (UngzipPacket==NULL) { return -1; } if (ComGzipBuffer==NULL) { return -1; } BOOL bRet=UngzipPacket((unsigned char*)pChGzipBuff,dwReaded,pChDataBuff,&dwTransferSize); //注意此处dwGzipSize的必须有值,不能传递0考虑到压缩之后一定比原来数据小。传递dwTransferSize大小就可以 BOOL bReturn=ComGzipBuffer((unsigned char*)pChDataBuff,dwTransferSize,pChGzipBuff,&dwGzipSize); FreeLibrary(hInst);
//zlib同上演示样例。