利用zlib压缩和解压gzip文件

因为项目的原因要运行一个这样的小小的demo。
操作系统:ubuntu19.04(优麒麟)
①安装 zlib.h头文件。
参考这位大佬的博客
https://blog.csdn.net/wsclinux/article/details/50494501。
方法:sudo apt-get install zliblg-dev(注意,中间是1 而不是 L的小写)
开始执行的第一次,返回错误 E:(一大串英文),大概就是说没有找到软件包之类的。后来又看了一篇博文,说是zlib在常规源里是找不到的,只能通过一个网站来找,心塞塞。
后来又写了一个程序,想试试是否系统自带有这个zlib.h,加了一句#include <zlib.h> ,结果不能运行。
然后不知道咋办又执行了 一下 上面那个命令,,, 竟然成功了。(然后我又回滚到第一次写的那个命令,也成功了。。)
(大概就是下图,比这个长一点)
在这里插入图片描述
2 网上找demo(面向网络的编程)
找了好几个,
https://www.cnblogs.com/passedbylove/p/4547029.html 这个佛了,写了几个函数名字,函数定义都没有。。我下载了附件,也没找到定义在哪里,大概是win里已经实现了
https://blog.csdn.net/danis_wang/article/details/51452880 这个没有试。
这个大佬的是好用的(下链接)
https://blog.csdn.net/moxiaomomo/article/details/52385837?locationNum=14
我把malloc上面加了一个强制类型转换,完全ojbk。
提示重点: 在linux上用命令运行,要加-lz
运行效果见图:
错误的情况
在这里插入图片描述

#include <stdio.h>
#include<stdlib.h>
#include <string.h>
#include <zlib.h>
 
// gzCompress: do the compressing
int gzCompress(const char *src, int srcLen, char *dest, int destLen)
{
	z_stream c_stream;
	int err = 0;
	int windowBits = 15;
	int GZIP_ENCODING = 16;
 
	if(src && srcLen > 0)
	{
		c_stream.zalloc = (alloc_func)0;
		c_stream.zfree = (free_func)0;
		c_stream.opaque = (voidpf)0;
		if(deflateInit2(&c_stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 
                    windowBits | GZIP_ENCODING, 8, Z_DEFAULT_STRATEGY) != Z_OK) return -1;
		c_stream.next_in  = (Bytef *)src;
		c_stream.avail_in  = srcLen;
		c_stream.next_out = (Bytef *)dest;
		c_stream.avail_out  = destLen;
		while (c_stream.avail_in != 0 && c_stream.total_out < destLen) 
		{
			if(deflate(&c_stream, Z_NO_FLUSH) != Z_OK) return -1;
		}
        	if(c_stream.avail_in != 0) return c_stream.avail_in;
		for (;;) {
			if((err = deflate(&c_stream, Z_FINISH)) == Z_STREAM_END) break;
			if(err != Z_OK) return -1;
		}
		if(deflateEnd(&c_stream) != Z_OK) return -1;
		return c_stream.total_out;
	}
	return -1;
}
 
// gzDecompress: do the decompressing
int gzDecompress(const char *src, int srcLen, const char *dst, int dstLen){
	z_stream strm;
	strm.zalloc=NULL;
	strm.zfree=NULL;
	strm.opaque=NULL;
	 
	strm.avail_in = srcLen;
	strm.avail_out = dstLen;
	strm.next_in = (Bytef *)src;
	strm.next_out = (Bytef *)dst;
	 
	int err=-1, ret=-1;
	err = inflateInit2(&strm, MAX_WBITS+16);
	if (err == Z_OK){
	    err = inflate(&strm, Z_FINISH);
	    if (err == Z_STREAM_END){
	        ret = strm.total_out;
	    }
	    else{
	        inflateEnd(&strm);
	        return err;
	    }
	}
	else{
	    inflateEnd(&strm);
	    return err;
	}
	inflateEnd(&strm);
	return err;
}
 
int main()
{
	char* src = "this is okey";
	int size_src = strlen(src);
	char* compressed = (char *)malloc(size_src*2);
	memset(compressed, 0, size_src*2);
	printf("to compress src: %s\n", src);
	printf("to compress src size: %d\n", size_src);
 
	int gzSize = gzCompress(src, size_src, compressed, size_src*2);
	if (gzSize <= 0)
	{
		printf("compress error.\n");
		return -1;
	}
	printf("compressed: ");
	int i = 0;
	for (; i<gzSize; ++i)
	{
		printf("%02x ", compressed[i]);
	}
	printf("\ncompressed size: %d\n", gzSize);
 
	char* uncompressed = (char *)malloc(size_src*2);
	memset(uncompressed, 0, size_src*2);
	int ret = gzDecompress(compressed, gzSize, uncompressed, size_src*2);
	printf("uncompressed: %s\n", uncompressed);
	printf("uncompressed size: %d\n", strlen(uncompressed));
 
	free(compressed);
	free(uncompressed);
	return 0;
}
 

后记:gzip格式头什么的,我还没有研究。因为项目我也不是负责这一块,等我整完我的我在补。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值