1.测试压缩与解压缩的demo
2. 代码如下
test.cpp
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <errno.h>
-
#include <zlib.h>
-
#define dbmsg(fmt, args ...) printf("%s:%s[%d]: "fmt"\n", __FILE__,__FUNCTION__, __LINE__,##args)
-
-
/* Compress gzip data */
-
int gzcompress(Bytef *data, uLong ndata,
-
Bytef *zdata, uLong *nzdata)
-
{
-
z_stream c_stream;
-
int err = 0;
-
-
if(data && ndata > 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,
-
-MAX_WBITS, 8, Z_DEFAULT_STRATEGY) != Z_OK) return -1;
-
c_stream.next_in = data;
-
c_stream.avail_in = ndata;
-
c_stream.next_out = zdata;
-
c_stream.avail_out = *nzdata;
-
while (c_stream.avail_in != 0 && c_stream.total_out < *nzdata)
-
{
-
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;
-
*nzdata = c_stream.total_out;
-
return 0;
-
}
-
return -1;
-
}
-
-
/* Uncompress gzip data */
-
int gzdecompress(Byte *zdata, uLong nzdata,
-
Byte *data, uLong *ndata)
-
{
-
int err = 0;
-
z_stream d_stream = {0}; /* decompression stream */
-
static char dummy_head[2] =
-
{
-
0x8 + 0x7 * 0x10,
-
(((0x8 + 0x7 * 0x10) * 0x100 + 30) / 31 * 31) & 0xFF,
-
};
-
d_stream.zalloc = (alloc_func)0;
-
d_stream.zfree = (free_func)0;
-
d_stream.opaque = (voidpf)0;
-
d_stream.next_in = zdata;
-
d_stream.avail_in = 0;
-
d_stream.next_out = data;
-
if(inflateInit2(&d_stream, -MAX_WBITS) != Z_OK) return -1;
-
//if(inflateInit2(&d_stream, 47) != Z_OK) return -1;
-
while (d_stream.total_out < *ndata && d_stream.total_in < nzdata) {
-
d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
-
if((err = inflate(&d_stream, Z_NO_FLUSH)) == Z_STREAM_END) break;
-
if(err != Z_OK )
-
{
-
if(err == Z_DATA_ERROR)
-
{
-
d_stream.next_in = (Bytef*) dummy_head;
-
d_stream.avail_in = sizeof(dummy_head);
-
if((err = inflate(&d_stream, Z_NO_FLUSH)) != Z_OK)
-
{
-
return -1;
-
}
-
}
-
else return -1;
-
}
-
}
-
if(inflateEnd(&d_stream) != Z_OK) return -1;
-
*ndata = d_stream.total_out;
-
return 0;
-
}
-
-
int file_read(char** pData)
-
{
-
char *pchBuf = NULL;
-
int nLen = 0;
-
FILE *pF = fopen("siminfo_ok.xml", "r"); //打开文件
-
if(NULL== pF)
-
{
-
dbmsg("file open error=%s",strerror(errno));
-
return -2;
-
}
-
-
fseek(pF, 0, SEEK_END); //文件指针移到文件尾
-
nLen = ftell(pF); //得到当前指针位置, 即是文件的长度
-
rewind(pF); //文件指针恢复到文件头位置
-
-
//动态申请空间, 为保存字符串结尾标志\0, 多申请一个字符的空间
-
pchBuf = (char*) malloc(sizeof(char)*nLen+1);
-
if(!pchBuf)
-
{
-
perror("内存不够!\n");
-
exit(0);
-
}
-
-
//读取文件内容//读取的长度和源文件长度有可能有出入,这里自动调整 nLen
-
nLen = fread(pchBuf, sizeof(char), nLen, pF);
-
-
pchBuf[nLen] = '\0'; //添加字符串结尾标志
-
-
//printf("%s\n", pchBuf); //把读取的内容输出到屏幕看看
-
-
fclose(pF); //关闭文件
-
*pData = pchBuf;
-
// free(pchBuf); //释放空间
-
-
return nLen;
-
}
-
-
#define BUF_SIZE 65535
-
int main()
-
{
-
//char *data = "kjdalkfjdflkjdlkfjdklfjdlkfjlkdjflkdjflddajfkdjfkdfaskf;ldsfk;ldakf;ldskfl;dskf;ld";
-
//uLong ndata = strlen(data);
-
char * data;
-
uLong ndata = file_read(&data);
-
Bytef zdata[BUF_SIZE];
-
uLong nzdata = BUF_SIZE;
-
Bytef odata[BUF_SIZE];
-
uLong nodata = BUF_SIZE;
-
-
memset(zdata, 0, BUF_SIZE);
-
-
if(gzcompress((Bytef *)data, ndata, zdata, &nzdata) == 0)
-
{
-
fprintf(stdout, "nzdata:%d %s\n", nzdata, zdata);
-
#if 1
-
printf("orgin_len=%d, compressed_len=%d\n", ndata, nzdata);
-
for(int i=0; i<nzdata; i++)
-
{
-
//printf("%d=0x%x ", i,(unsigned char)base_info[i]);
-
printf("0x%x ", zdata[i]);
-
if((i+1)%10==0)
-
printf("\n");
-
}
-
printf("\n");
-
#endif
-
memset(odata, 0, BUF_SIZE);
-
//if(gzdecompress(zdata, ndata, odata, &nodata) == 0) //ok
-
if(gzdecompress(zdata, ndata, odata, &nodata) == 0) //ok
-
{
-
//fprintf(stdout, "%d %s\n", nodata, odata);
-
}
-
}
-
}
Makefile
-
####################################################
-
# Generic makefile - 万能Makefile
-
# for compiling and linking C++ projects on Linux
-
# Author: George Foot Modified:Jackie Lee
-
####################################################
-
### Customising
-
#
-
# Adjust the following if necessary; EXECUTABLE is the target
-
# executable's filename, and LIBS is a list of libraries to link in
-
# (e.g. alleg, stdcx, iostr, etc). You can override these on make's
-
# command line of course, if you prefer to do it that way.
-
#
-
#
-
EXECUTABLE := test
-
INCLUDES:=../../include ./
-
SRCDIR:=
-
#
-
# # Now alter any implicit rules' variables if you like, e.g.:
-
-
CC:=g++
-
CFLAGS := -g -Wall -O3
-
CPPFLAGS := $(CFLAGS)
-
CPPFLAGS += $(addprefix -I,$(INCLUDES))
-
CPPFLAGS += -MMD
-
LDFLAGS = -Wl,-Bstatic -L../lib/ -lz -Wl,-Bdynamic -pthread -Wl,-Bdynamic
-
#
-
# # The next bit checks to see whether rm is in your djgpp bin
-
# # directory; if not it uses del instead, but this can cause (harmless)
-
# # `File not found' error messages. If you are not using DOS at all,
-
# # set the variable to something which will unquestioningly remove
-
# # files.
-
#
-
-
RM-F := rm -f
-
-
-
# # You shouldn't need to change anything below this point.
-
#
-
SRCS := $(wildcard *.cpp) $(wildcard $(addsuffix /*.cpp, $(SRCDIR)))
-
OBJS := $(patsubst %.cpp,%.o,$(SRCS))
-
DEPS := $(patsubst %.o,%.d,$(OBJS))
-
MISSING_DEPS := $(filter-out $(wildcard $(DEPS)),$(DEPS))
-
MISSING_DEPS_SOURCES := $(wildcard $(patsubst %.d,%.cpp,$(MISSING_DEPS)))
-
-
-
.PHONY : all deps objs clean veryclean rebuild info
-
-
all: $(EXECUTABLE)
-
-
deps : $(DEPS)
-
-
objs : $(OBJS)
-
-
clean :
-
@$(RM-F) *.o
-
@$(RM-F) *.d
-
@$(RM-F) $(EXECUTABLE)
-
-
rebuild: veryclean all
-
ifneq ($(MISSING_DEPS),)
-
$(MISSING_DEPS) :
-
@$(RM-F) $(patsubst %.d,%.o,$@)
-
endif
-
-include $(DEPS)
-
$(EXECUTABLE) : $(OBJS)
-
$(CC) -o $(EXECUTABLE) $(OBJS) $(LDFLAGS)
-
-
info:
-
@echo $(SRCS)
-
@echo $(OBJS)
-
@echo $(DEPS)
-
@echo $(MISSING_DEPS)
-
@echo $(MISSING_DEPS_SOURCES)
3. 代码下载
gzip.rar(下载后改名为gzip.tar.gz)
阅读(3566) | 评论(0) | 转发(0) |