VC++中使用Zlib的一个例子

首先到www.zlib.net 下载个ZLIB,解压缩后打开zlib-1.2.3/projects/visualc6/zlib.dsw

选择Win32 LIB Release 按F7编绎生成zlib.lib

工程中使用时把

D:/zlib-1.2.3/projects/visualc6/Win32_LIB_Release/zlib.lib

D:/zlib-1.2.3/zlib.h

D:/zlib-1.2.3/zconf.h

拷贝到所在工程目录下

然后

#include "zlib.h"
#pragma comment(lib,"zlib.lib")

void CZlibTestDlg::OnButton1()
{
const unsigned char strSrc[]="hello world!/n/
aaaaa bbbbb ccccc ddddd aaaaa bbbbb ccccc ddddd中文测试 中文测试/
aaaaa bbbbb ccccc ddddd aaaaa bbbbb ccccc ddddd中文测试 中文测试/
aaaaa bbbbb ccccc ddddd aaaaa bbbbb ccccc ddddd中文测试 中文测试/
aaaaa bbbbb ccccc ddddd aaaaa bbbbb ccccc ddddd中文测试 中文测试";

     unsigned char buff[1024]={0},strDst[1024]={0};
     unsigned long srcLen=sizeof(strSrc),bufLen=sizeof(buff),dstLen=sizeof(strDst);
CString strc,strc2,strc3;
     strc.Format("Src string:%s/nLength:%d/n===================/n",strSrc,srcLen);
     //压缩
      compress(buff,&bufLen,strSrc,srcLen);
      strc2.Format("/nAfter Compressed Length:%d/nCompressed String:%s/n==============/n",bufLen,buff);
        
     //解压缩
      uncompress(strDst,&dstLen,buff,bufLen);
      strc3.Format("/nAfter UnCompressed Length:%d/nUnCompressed String:%s/n",dstLen,strDst);
     
   AfxMessageBox(strc+strc2+strc3);
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要获取压缩包压缩包的名字,我们需要使用zlib库提供的函数来解压缩文件,并且读取其的目录信息。以下是一个简单的C语言程序,可以实现这个功能: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include "zlib.h" #define CHUNK_SIZE 16384 int main(int argc, char *argv[]) { gzFile file = NULL; char buf[CHUNK_SIZE]; int len = 0, pos = 0, i = 0; unsigned char header[10]; unsigned long filesize = 0, uncompressed_size = 0; if (argc < 2) { printf("Usage: %s <filename>\n", argv[0]); return 1; } file = gzopen(argv[1], "rb"); if (!file) { printf("Error: Failed to open file '%s'\n", argv[1]); return 1; } // read gzip header len = gzread(file, header, 10); if (len != 10 || memcmp(header, "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff", 10)) { printf("Error: %s is not a valid gzip file.\n", argv[1]); gzclose(file); return 1; } // read file name and extra fields while (1) { len = gzread(file, buf, CHUNK_SIZE); if (len == 0) { printf("Error: Failed to read file name and extra fields.\n"); gzclose(file); return 1; } for (i = 0; i < len; i++) { if (buf[i] == 0) { pos = i + 1; break; } } if (pos > 0) { break; } } // read file size memcpy(&filesize, buf + pos, sizeof(unsigned long)); pos += sizeof(unsigned long); // read uncompressed size memcpy(&uncompressed_size, buf + pos, sizeof(unsigned long)); pos += sizeof(unsigned long); // read file name printf("The compressed file name is: %s\n", buf + pos); gzclose(file); return 0; } ``` 这个程序打开了一个gzip文件,并且读取了其的文件名和额外的字段。它也解析了文件的大小和未压缩大小,但是这些信息对于我们需要的压缩包名字并没有用处。最终,程序输出了压缩包名字,并且关闭了文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值