在C语言中用zlib压缩的数据,如何用php解压缩呢?

辉歌 2011-11-02 09:15:24

#include "StdAfx.h"
#include ".\zip.h"

CZip::CZip(void)
: m_pZlibStream(NULL)
, m_ulTotalZipLen(0)
, m_bCompress(true)
{
}

CZip::~CZip(void)
{
if(m_pZlibStream)
{
deflateEnd(m_pZlibStream);
delete m_pZlibStream;
m_pZlibStream = NULL;
}
}

unsigned long CZip::ZipData(const unsigned char* pUnZipData, unsigned long ulUnZipLen, unsigned char** ppZipData)
{
if(pUnZipData == NULL || ulUnZipLen == 0)
{
return 0;
}

unsigned long ulZipLen = 0;
do
{
if(m_bCompress == false)
{
break;
}
if(ulUnZipLen < ENCODE_ZLIB_MIN_COMP_SIZE)
{
break;
}
*ppZipData = new unsigned char[ulUnZipLen + 301];
if(*ppZipData == NULL)
{
break;
}
ulZipLen = ulUnZipLen + 300;
int Ret = Zip(pUnZipData,ulUnZipLen,*ppZipData + 1,ulZipLen);
if(Ret != Z_OK)
{
delete []*ppZipData;
ulZipLen = 0;
break;
}

*(unsigned char*)*ppZipData = ZIP_TYPE_ZLIB;
ulZipLen += 1;
return ulZipLen;

}while(0);

*ppZipData = new unsigned char[ulUnZipLen + 1];
if(*ppZipData)
{
*(unsigned char*)*ppZipData = ZIP_TYPE_NONE;
memcpy(*ppZipData + 1,pUnZipData,ulUnZipLen);
ulZipLen = ulUnZipLen + 1;
}
return ulZipLen;
}

int CZip::Zip(const unsigned char* pUnZipData, unsigned long ulUnZipLen, unsigned char* pZipData, unsigned long& ulZipLen)
{
int err = Z_DATA_ERROR;
if (m_pZlibStream == NULL)
{
m_pZlibStream = new z_stream;
// Initialise stream values
m_pZlibStream->zalloc = (alloc_func)0;
m_pZlibStream->zfree = (free_func)0;
m_pZlibStream->opaque = (voidpf)0;

// Initialise the z_stream
err = deflateInit(m_pZlibStream,9);
if (err != Z_OK)
{
return err;
}
m_ulTotalZipLen = 0;
}
// Use whatever input is provided
m_pZlibStream->next_in = const_cast<Bytef*>(pUnZipData);
m_pZlibStream->avail_in = ulUnZipLen;
m_pZlibStream->next_out = pZipData;
m_pZlibStream->avail_out = ulZipLen;
// Try to unzip the data
err = deflate(m_pZlibStream, Z_SYNC_FLUSH);
if ((err == Z_OK) && (m_pZlibStream->avail_in == 0))
{
// All available input has been processed, everything ok.
// Set the size to the amount unzipped in this call (including all recursive calls)
ulZipLen = (m_pZlibStream->total_out - m_ulTotalZipLen);
m_ulTotalZipLen = m_pZlibStream->total_out;
}

if (err != Z_OK)
ulZipLen = 0;
return err;
}

void CZip::EnableCompress(bool bEnable)
{
m_bCompress = bEnable;
}


源代码如上。使用接口为unsigned long CZip::ZipData(const unsigned char* pUnZipData, unsigned long ulUnZipLen, unsigned char** ppZipData)
现在想把这个函数压缩的数据在php里解压缩。如何处理呢?
...全文
108 点赞 收藏 回复
写回复
回复

还没有回复,快来抢沙发~

发动态
发帖子
基础编程
创建于2007-09-28

9734

社区成员

14.0w+

社区内容

从PHP安装配置,PHP入门,PHP基础到PHP应用
社区公告
暂无公告