Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2100593
  • 博文数量: 438
  • 博客积分: 3871
  • 博客等级: 中校
  • 技术积分: 6075
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-10 00:11
个人简介

邮箱: wangcong02345@163.com

文章分类

全部博文(438)

文章存档

2017年(15)

2016年(119)

2015年(91)

2014年(62)

2013年(56)

2012年(79)

2011年(16)

分类: LINUX

2017-02-28 11:22:25

1.测试压缩与解压缩的demo

2. 代码如下
test.cpp
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #include <zlib.h>
  6. #define dbmsg(fmt, args ...) printf("%s:%s[%d]: "fmt"\n", __FILE__,__FUNCTION__, __LINE__,##args)

  7. /* Compress gzip data */
  8. int gzcompress(Bytef *data, uLong ndata,
  9.     Bytef *zdata, uLong *nzdata)
  10. {
  11.     z_stream c_stream;
  12.     int err = 0;

  13.     if(data && ndata > 0)
  14.     {
  15.         c_stream.zalloc = (alloc_func)0;
  16.         c_stream.zfree = (free_func)0;
  17.         c_stream.opaque = (voidpf)0;
  18.         if(deflateInit2(&c_stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
  19.                     -MAX_WBITS, 8, Z_DEFAULT_STRATEGY) != Z_OK) return -1;
  20.         c_stream.next_in = data;
  21.         c_stream.avail_in = ndata;
  22.         c_stream.next_out = zdata;
  23.         c_stream.avail_out = *nzdata;
  24.         while (c_stream.avail_in != 0 && c_stream.total_out < *nzdata)
  25.         {
  26.             if(deflate(&c_stream, Z_NO_FLUSH) != Z_OK) return -1;
  27.         }
  28.         if(c_stream.avail_in != 0) return c_stream.avail_in;
  29.         for (;;) {
  30.             if((err = deflate(&c_stream, Z_FINISH)) == Z_STREAM_END) break;
  31.             if(err != Z_OK) return -1;
  32.         }
  33.         if(deflateEnd(&c_stream) != Z_OK) return -1;
  34.         *nzdata = c_stream.total_out;
  35.         return 0;
  36.     }
  37.     return -1;
  38. }

  39. /* Uncompress gzip data */
  40. int gzdecompress(Byte *zdata, uLong nzdata,
  41.         Byte *data, uLong *ndata)
  42. {
  43.     int err = 0;
  44.     z_stream d_stream = {0}; /* decompression stream */
  45.     static char dummy_head[2] =
  46.     {
  47.         0x8 + 0x7 * 0x10,
  48.         (((0x8 + 0x7 * 0x10) * 0x100 + 30) / 31 * 31) & 0xFF,
  49.     };
  50.     d_stream.zalloc = (alloc_func)0;
  51.     d_stream.zfree = (free_func)0;
  52.     d_stream.opaque = (voidpf)0;
  53.     d_stream.next_in = zdata;
  54.     d_stream.avail_in = 0;
  55.     d_stream.next_out = data;
  56.     if(inflateInit2(&d_stream, -MAX_WBITS) != Z_OK) return -1;
  57.     //if(inflateInit2(&d_stream, 47) != Z_OK) return -1;
  58.     while (d_stream.total_out < *ndata && d_stream.total_in < nzdata) {
  59.         d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
  60.         if((err = inflate(&d_stream, Z_NO_FLUSH)) == Z_STREAM_END) break;
  61.         if(err != Z_OK )
  62.         {
  63.             if(err == Z_DATA_ERROR)
  64.             {
  65.                 d_stream.next_in = (Bytef*) dummy_head;
  66.                 d_stream.avail_in = sizeof(dummy_head);
  67.                 if((err = inflate(&d_stream, Z_NO_FLUSH)) != Z_OK)
  68.                 {
  69.                     return -1;
  70.                 }
  71.             }
  72.             else return -1;
  73.         }
  74.     }
  75.     if(inflateEnd(&d_stream) != Z_OK) return -1;
  76.     *ndata = d_stream.total_out;
  77.     return 0;
  78. }

  79. int file_read(char** pData)
  80. {
  81.     char *pchBuf = NULL;
  82.     int nLen = 0;
  83.     FILE *pF = fopen("siminfo_ok.xml", "r"); //打开文件
  84.     if(NULL== pF)
  85.     {
  86.         dbmsg("file open error=%s",strerror(errno));
  87.         return -2;
  88.     }

  89.     fseek(pF, 0, SEEK_END); //文件指针移到文件尾
  90.     nLen = ftell(pF); //得到当前指针位置, 即是文件的长度
  91.     rewind(pF); //文件指针恢复到文件头位置

  92.     //动态申请空间, 为保存字符串结尾标志\0, 多申请一个字符的空间
  93.     pchBuf = (char*) malloc(sizeof(char)*nLen+1);
  94.     if(!pchBuf)
  95.     {
  96.         perror("内存不够!\n");
  97.         exit(0);
  98.     }

  99.     //读取文件内容//读取的长度和源文件长度有可能有出入,这里自动调整 nLen
  100.     nLen = fread(pchBuf, sizeof(char), nLen, pF);

  101.     pchBuf[nLen] = '\0'; //添加字符串结尾标志

  102.     //printf("%s\n", pchBuf); //把读取的内容输出到屏幕看看

  103.     fclose(pF); //关闭文件
  104.     *pData = pchBuf;
  105. // free(pchBuf); //释放空间

  106.     return nLen;
  107. }

  108. #define BUF_SIZE 65535
  109. int main()
  110. {
  111.     //char *data = "kjdalkfjdflkjdlkfjdklfjdlkfjlkdjflkdjflddajfkdjfkdfaskf;ldsfk;ldakf;ldskfl;dskf;ld";    
  112.     //uLong ndata = strlen(data);    
  113.     char * data;
  114.     uLong ndata = file_read(&data);
  115.     Bytef zdata[BUF_SIZE];
  116.     uLong nzdata = BUF_SIZE;
  117.     Bytef odata[BUF_SIZE];
  118.     uLong nodata = BUF_SIZE;
  119.     
  120.     memset(zdata, 0, BUF_SIZE);
  121.     
  122.     if(gzcompress((Bytef *)data, ndata, zdata, &nzdata) == 0)
  123.     {
  124.         fprintf(stdout, "nzdata:%d %s\n", nzdata, zdata);
  125. #if 1
  126.     printf("orgin_len=%d, compressed_len=%d\n", ndata, nzdata);
  127.     for(int i=0; i<nzdata; i++)
  128.     {
  129.         //printf("%d=0x%x ", i,(unsigned char)base_info[i]);
  130.         printf("0x%x ", zdata[i]);
  131.         if((i+1)%10==0)
  132.             printf("\n");
  133.     }
  134.     printf("\n");
  135. #endif
  136.         memset(odata, 0, BUF_SIZE);
  137.         //if(gzdecompress(zdata, ndata, odata, &nodata) == 0) //ok
  138.         if(gzdecompress(zdata, ndata, odata, &nodata) == 0) //ok
  139.         {
  140.             //fprintf(stdout, "%d %s\n", nodata, odata);
  141.         }
  142.     }
  143. }




Makefile
  1. ####################################################
  2. # Generic makefile - 万能Makefile
  3. # for compiling and linking C++ projects on Linux
  4. # Author: George Foot Modified:Jackie Lee
  5. ####################################################
  6. ### Customising
  7. #
  8. # Adjust the following if necessary; EXECUTABLE is the target
  9. # executable's filename, and LIBS is a list of libraries to link in
  10. # (e.g. alleg, stdcx, iostr, etc). You can override these on make's
  11. # command line of course, if you prefer to do it that way.
  12. #
  13. #
  14. EXECUTABLE := test
  15. INCLUDES:=../../include ./
  16. SRCDIR:=
  17. #
  18. # # Now alter any implicit rules' variables if you like, e.g.:

  19. CC:=g++
  20. CFLAGS := -g -Wall -O3
  21. CPPFLAGS := $(CFLAGS)
  22. CPPFLAGS += $(addprefix -I,$(INCLUDES))
  23. CPPFLAGS += -MMD
  24. LDFLAGS = -Wl,-Bstatic -L../lib/ -lz -Wl,-Bdynamic -pthread -Wl,-Bdynamic
  25. #
  26. # # The next bit checks to see whether rm is in your djgpp bin
  27. # # directory; if not it uses del instead, but this can cause (harmless)
  28. # # `File not found' error messages. If you are not using DOS at all,
  29. # # set the variable to something which will unquestioningly remove
  30. # # files.
  31. #

  32. RM-F := rm -f


  33. # # You shouldn't need to change anything below this point.
  34. #
  35. SRCS := $(wildcard *.cpp) $(wildcard $(addsuffix /*.cpp, $(SRCDIR)))
  36. OBJS := $(patsubst %.cpp,%.o,$(SRCS))
  37. DEPS := $(patsubst %.o,%.d,$(OBJS))
  38. MISSING_DEPS := $(filter-out $(wildcard $(DEPS)),$(DEPS))
  39. MISSING_DEPS_SOURCES := $(wildcard $(patsubst %.d,%.cpp,$(MISSING_DEPS)))


  40. .PHONY : all deps objs clean veryclean rebuild info

  41. all: $(EXECUTABLE)

  42. deps : $(DEPS)

  43. objs : $(OBJS)

  44. clean :
  45.     @$(RM-F) *.o
  46.     @$(RM-F) *.d
  47.     @$(RM-F) $(EXECUTABLE)

  48. rebuild: veryclean all
  49. ifneq ($(MISSING_DEPS),)
  50. $(MISSING_DEPS) :
  51.     @$(RM-F) $(patsubst %.d,%.o,$@)
  52. endif
  53. -include $(DEPS)
  54. $(EXECUTABLE) : $(OBJS)
  55.     $(CC) -o $(EXECUTABLE) $(OBJS) $(LDFLAGS)

  56. info:
  57.     @echo $(SRCS)
  58.     @echo $(OBJS)
  59.     @echo $(DEPS)
  60.     @echo $(MISSING_DEPS)
  61.     @echo $(MISSING_DEPS_SOURCES)

3. 代码下载
gzip.rar(下载后改名为gzip.tar.gz)
阅读(3435) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~