zlib解压数据,应该怎么解压?

aCracker 2011-11-01 09:04:06
我有一个二进制文件,40字节以后的数据需要用zlib进行解压。我把这个文件readfile到一个buffer中,然后调用zlib中的解压函数进行解压,结果解压出来是空的。


CString str=_T(""); // 全部内容
CStdioFile file; // 文件对象

if(!file.Open("D:\\1.W3G",CFile::modeRead | CFile::typeBinary,NULL))
{
//CString strTemp;
//strTemp.Format(_T("Open file error:%d"),GetLastError());
//MessageBox(strTemp);
// return false;
}
int len = file.GetLength()+1;

unsigned char szStr[1024] = {0};
file.Read(szStr,file.GetLength());


file.Flush();
file.Close();



CString strc,strc2,strc3;
const unsigned char strSrc[] = "English test\n中文测试";
unsigned char buff[1024] = {0},strDst[1024] = {0};

memcpy(buff,szStr+50,400);
//解压缩
uncompress(strDst, &dstLen, buff, bufLen);
strc3.Format("\nAfter UnCompressed Length:%d\nUnCompressed String:%s\n", dstLen, strDst);


...全文
628 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
aCracker 2011-11-01
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 yuucyf 的回复:]
//Sample Code

C/C++ code

#include <cstring>

#include <cstdlib>

#include <iostream>

#include "zlib.h"



using namespace std;



int main()

{

int err;

Byte com……
[/Quote]

字符串的我弄了可以,但是这是一个文件,魔兽录像。
要不你留一个邮箱,我发给你一个。
http://hi.baidu.com/kayak/blog/item/1a31b31927b99d4243a9ad8d.html
yuucyf 2011-11-01
  • 打赏
  • 举报
回复
//Sample Code

#include <cstring>

#include <cstdlib>

#include <iostream>

#include "zlib.h"



using namespace std;



int main()

{

int err;

Byte compr[200], uncompr[200]; // big enough

uLong comprLen, uncomprLen;

const char* hello = "12345678901234567890123456789012345678901234567890";



uLong len = strlen(hello) + 1;

comprLen = sizeof(compr) / sizeof(compr[0]);



err = compress(compr, &comprLen, (const Bytef*)hello, len);



if (err != Z_OK) {

cerr << "compess error: " << err << '/n';

exit(1);

}

cout << "orignal size: " << len

<< " , compressed size : " << comprLen << '/n';



strcpy((char*)uncompr, "garbage");



err = uncompress(uncompr, &uncomprLen, compr, comprLen);



if (err != Z_OK) {

cerr << "uncompess error: " << err << '/n';

exit(1);

}

cout << "orignal size: " << len

<< " , uncompressed size : " << uncomprLen << '/n';



if (strcmp((char*)uncompr, hello)) {

cerr << "BAD uncompress!!!/n";

exit(1);

} else {

cout << "uncompress() succeed: /n" << (char *)uncompr;

}

}


yuucyf 2011-11-01
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 acracker 的回复:]
返回的是 Z_DATA_ERROR
[/Quote]
你确认解压缩的数据是之前用compress压缩过的数据吗?
aCracker 2011-11-01
  • 打赏
  • 举报
回复
返回的是 Z_DATA_ERROR
aCracker 2011-11-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 yuucyf 的回复:]
int uncompress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);
uncompress 函数将 source 缓冲区的内容解压缩到 dest 缓冲区。sourceLen 是 source 缓冲区的大小(以字节计)。注意函数的第二个参数 destLen 是传址调用。当调用函数时,destLen 表……
[/Quote]
谢谢,你的回复。
    if(!file.Open("D:\\1.W3G",CFile::modeRead | CFile::typeBinary,NULL))      
{

}
int len = file.GetLength()+1;

unsigned char szStr[1024] = {0};
file.Read(szStr,file.GetLength());

file.Flush();
file.Close();


unsigned char buff[1024] = {0},strDst[1024*16] = {0};
unsigned long srcLen = sizeof(szStr),bufLen = sizeof(buff),dstLen = sizeof(strDst);
memcpy(buff,szStr+76,420);
int x = uncompress(strDst, &dstLen, buff, bufLen);

if(!file.Open("D:\\2.W3G",CFile::modeWrite | CFile::typeBinary,NULL))
{

}
file.Write(strDst,dstLen);
yuucyf 2011-11-01
  • 打赏
  • 举报
回复
int uncompress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);
uncompress 函数将 source 缓冲区的内容解压缩到 dest 缓冲区。sourceLen 是 source 缓冲区的大小(以字节计)。注意函数的第二个参数 destLen 是传址调用。当调用函数时,destLen 表示 dest 缓冲区的大小, dest 缓冲区要足以容下解压后的数据。在进行解压缩时,需要提前知道被压缩的数据解压出来会有多大。这就要求在进行压缩之前,保存原始数据的大小(也就是解压后的数据的大小)。这不是 zlib 函数库的功能,需要我们做额外的工作。当函数退出后, destLen 是解压出来的数据的实际大小。
uncompress 若成功,则返回 Z_OK ;若没有足够内存,则返回 Z_MEM_ERROR;若输出缓冲区不够大,则返回 Z_BUF_ERROR。若输入数据有误,则返回 Z_DATA_ERROR。

楼主你看你的使用:
uncompress(strDst, &dstLen, buff, bufLen);
我看不到bufLen,dstLen的值,还有你要判断返回值是否是解压成功还是失败的。

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧