免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3266 | 回复: 1
打印 上一主题 下一主题

[C++] Stream Server 上 , 利用 C++ POCO 制作 Gzp + zlib , Deflate 與 Inflate [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-08-22 08:36 |只看该作者 |倒序浏览
本帖最后由 fiendcly 于 2012-08-22 08:57 编辑

# 作者 : FIEND
# 文章來源 :  
http://bbs.ecstart.com/forum.php ... id=43305&extra=

http://bbs.chinaunix.net/forum.p ... mp;fromuid=20071307
# 轉貼請注明出處 , 以方便討論.

在使用 C++ 做 Socket 的服務端時 , 利用 GZIP 和 ZLIB 做 Deflate 和 Inflate 來做 壓縮的 Stream Server

以下是我利用 POCO 來編寫的.

在使用這個元件時 , 我發現它的 GZIP 的 deflate 和 infalte 不能用 .

所以另外寫了一個獨立的 元件 .

在編寫過程中 , 發現這個 FREE 的 元件庫 問題還蠻多的 , 只好 一 一 解決.


這是我碰到這個問題時的討論主題 :

http://bbs.chinaunix.net/thread-3766361-1-1.html
http://bbs.ecstart.com/thread-43305-1-1.html



不想花錢 : (SUPPORT 是要錢的.)

http://pocoproject.org/download/index.html

使用版本 : 1.4.3p1 , 請先下載編譯.




請服用 .......

1.
zlib.h
  1. #ifndef _ZLIB_H
  2. #define _ZLIB_H

  3. #pragma once
  4. #include <string>

  5. #define ZLIB_HEADER_LENGTH 1
  6. #define ZLIB_END_LENGTH 4

  7. //#define ZLIB_HEADER_STR "0x780x9c"
  8. //#define ZLIB_END_STR "0x0d0xd50x230x55"


  9. class Zlib
  10. {

  11. private:
  12.         //static int len ;
  13. public:

  14.         char zlibHeader[ZLIB_HEADER_LENGTH];
  15.         char zlibEnd[ZLIB_END_LENGTH] ;
  16.         static std::string zlibHeaderStr ;
  17.         static std::string zlibEndStr ;


  18.         unsigned int i ;
  19.         unsigned int num ;

  20.         //const char* zlibHeader ;
  21.         //const char* zlibEnd ;
  22.         static std::string inflateStr ;
  23.         static std::string deflateStr ;
  24.         static std::string zlibDeflateStr ;

  25.         static std::string fixHeader ;

  26.         //std::string inflate (const std::string &source,int level);
  27.         std::string inflate (const std::string &source);
  28.         //std::string inflate2 (const std::string &source);
  29.         std::string deflate (const std::string &source,int level);


  30.         // convert zlib deflate to gzip deflate
  31.         std::string gzdeflate (const std::string &source,int level);



  32.         // convert zlib inflate to gzip inflate
  33.         std::string gzinflate (const std::string &source);



  34. };

  35. #endif
复制代码
2. CPP
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <string>
  4. #include <stdio.h>
  5. #include <math.h>
  6. #include <sstream>

  7. // zlib
  8. #include "Poco/InflatingStream.h"
  9. #include "Poco/DeflatingStream.h"
  10. #include "Poco/StreamCopier.h"
  11. #include <fstream>


  12. using namespace std;

  13. using Poco::InflatingInputStream;
  14. using Poco::InflatingOutputStream;
  15. using Poco::DeflatingOutputStream;
  16. using Poco::DeflatingStreamBuf;
  17. using Poco::StreamCopier;


  18. #include "wge/zlib.h"

  19. std::string Zlib::inflateStr ;
  20. std::string Zlib::deflateStr ;

  21. std::string Zlib::fixHeader ;


  22. char zlibHeader[ZLIB_HEADER_LENGTH];
  23. char zlibEnd[ZLIB_END_LENGTH] ;

  24. /*
  25. * zlib deflate 字串壓縮
  26. */
  27. std::string Zlib::deflate (const std::string &source , int level = (-1))        {

  28.         std::istringstream in(source);
  29.         std::ostringstream out;
  30.         Poco::DeflatingOutputStream deflaterOut(out,DeflatingStreamBuf::STREAM_ZLIB,level);
  31.         deflaterOut << in.str() ;
  32.         deflaterOut.close() ;

  33.         //std::cout << " input         : " << in.str() << endl ;
  34.         //std::cout << " input length  : " << in.str().length() << endl ;
  35.         //std::cout << " output        : " << out.str() << endl ;
  36.         //std::cout << " output length : " << out.str().length() << endl ;
  37.         deflateStr = out.str();
  38.         return deflateStr ;
  39. }


  40. /*
  41. * zlib inflate 解壓縮
  42. */
  43. //std::string Zlib::inflate (const std::string &source , int level = (-1))      {
  44. std::string Zlib::inflate (const std::string &source)   {

  45.         std::istringstream in(source);
  46.         //std::cout << in.str() << endl ;
  47.         std::ostringstream out;
  48.         Poco::InflatingInputStream inflaterIn(in,DeflatingStreamBuf::STREAM_ZLIB); // 建立 inflaterIn
  49.         //Poco::InflatingInputStream inflaterIn(in); // 建立 inflaterIn
  50.         // 將解壓縮的內容復制到 out stringtream .
  51.         StreamCopier::copyStream(inflaterIn , out);
  52.         inflateStr = out.str();
  53.         return inflateStr ;
  54. }


复制代码

  1. /*
  2. * gzip deflate 字串壓縮
  3. */

  4. std::string Zlib::gzdeflate (const std::string &source , int level = (-1))      {

  5.         std::istringstream in(source);
  6.         std::ostringstream out;
  7.         Poco::DeflatingOutputStream deflaterOut(out,DeflatingStreamBuf::STREAM_ZLIB,level);
  8.         deflaterOut << in.str() ;
  9.         deflaterOut.close() ;
  10.         std::string zlibDeflateStr ;
  11.         zlibDeflateStr = out.str();
  12.         zlibDeflateStr = zlibDeflateStr.substr(2,zlibDeflateStr.length()-6);
  13.         deflateStr = zlibDeflateStr ;
  14.         return deflateStr ;
  15. }

复制代码

  1. /*
  2. * gzip inflate 解壓縮
  3. */
  4. std::string Zlib::gzinflate (const std::string &source) {


  5.         // fix header & end
  6.         //zlibHeader[ZLIB_HEADER_LENGTH] = ZLIB_HEADER_STR;
  7.         //zlibEnd[ZLIB_END_LENGTH] = ZLIB_END_STR ;

  8.         // zlib Header
  9.         std::string fixsource;
  10.         //std::string str = source ;
  11.         zlibHeader[0] = 0x78  ;
  12.         zlibHeader[1] = 0x9c  ;
  13.         std::string zlibHeaderStr =  zlibHeader;
  14.         std::string fixZlibHeaderStr = zlibHeaderStr.substr(0,2);

  15.         zlibEnd[0] = 0xd ;
  16.         zlibEnd[1] = 0xd5 ;
  17.         zlibEnd[2] = 0x23 ;
  18.         zlibEnd[3] = 0x55 ;
  19.         std::string zlibEndStr = zlibEnd ;
  20.         std::string fixZlibEndStr = zlibEndStr.substr(0,2);

  21.         fixsource = fixZlibHeaderStr + source + fixZlibEndStr ;

  22.         std::istringstream in(fixsource);
  23.         //std::cout << in.str() << endl ;
  24.         std::ostringstream out;
  25.         Poco::InflatingInputStream inflaterIn(in,DeflatingStreamBuf::STREAM_ZLIB); // 建立 inflaterIn
  26.         //Poco::InflatingInputStream inflaterIn(in); // 建立 inflaterIn
  27.         StreamCopier::copyStream(inflaterIn , out);
  28.         //std::cout << "Inflate  Success  \n" ;
  29.         //std::cout <<  out.str()  ;
  30.         inflateStr = out.str();
  31.         return inflateStr ;
  32. }

复制代码
test.cpp
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <string.h>
  4. #include <string>
  5. #include <stdio.h>

  6. #include <ctime>
  7. //#include <unistd.h>
  8. #include <sys/time.h>

  9. #include "zlib.h"


  10. using namespace std;


  11. int main (int argc, char *argv[])
  12. {

  13.         std::cout << "Zlib test start ...................\n" ;
  14.         unsigned int i  ;
  15.         unsigned int num ;



  16.         //std::cout << std::endl ;
  17.         Zlib zlib ; // init Zlib object
  18.         std::string test2 = "{\"SID\":\"123456789\",\"BID\":\"LD0001\",\"DATA\":{\"args\":{\"name\":\"test1\",\"passwd\":\"test1\",\"item\":[\"itme1\",\"itme2\",\"itme3\",\"itme4\"]}}}" ;
  19.         std::string inflate ;
  20.         std::string deflate ;
  21.         deflate = zlib.deflate(test2,-1);
  22.         inflate = zlib.inflate(deflate);
  23.         std::cout << "deflate attribute    : \n" << zlib.deflateStr << std::endl;
  24.         std::cout << "deflate return       : \n" << deflate << std::endl;
  25.         std::cout << "PHP deflate HEX : \n" ;
  26.         for(i=0 ; i< zlib.deflateStr.length(); i++){
  27.                 num = zlib.deflateStr[i] ;
  28.                 std::cout << hex << num << " " ;
  29.         }
  30.         std::cout << std::endl ;
  31.         std::cout << "inflate attribute    : \n" << zlib.inflateStr << std::endl;
  32.         std::cout << "inflate return       : \n" << inflate << std::endl;
  33.         std::cout << "Test the wrong compression string format : " << endl ;
  34.         std::string wrongStr = "Input the wrong compression string........."  ;
  35.         inflate = zlib.inflate(wrongStr);
  36.         if(inflate == ""){
  37.                 std::cout << ".........................The compression string is wrong!!" << endl;
  38.         }
  39.         std::cout << std::endl ;
  40.         std::cout << std::endl ;


  41.         /*
  42.          *  BASE64 是我另外寫的一個元件庫....懶得貼 , 這裡不用測試 請淦掉.....

  43.         std::cout << "gzip test start ...................\n" ;
  44.         std::string test3 = "q1YK9nRRslIyNDI2MTUzt7BU0lFyAov4uBgYGBgCuS6OIY5KVtVKiUXpxSA6LzE3FShfklpcApIuSCwuLk9BEsgsSc1VsooG0rmpEH5uqhGUNobSJkqxtbW1AA==" ;
  45.         std::cout << "PHP gzInflate & Base64Decode string is : \n" ;
  46.         std::cout << test3 << std::endl ;
  47.         std::cout << std::endl ;
  48.         decode = base64.decode(test3);
  49.         std::cout << "Base64 Decode attribute    : \n" << base64.decodeStr << std::endl;
  50.         std::cout << std::endl ;

  51.         std::cout << "PHP deflate HEX : \n" ;
  52.         for(i=0 ; i< base64.decodeStr.length(); i++){
  53.                 num = base64.decodeStr[i] ;
  54.                 std::cout << hex << num << " " ;
  55.         }
  56.         std::cout << std::endl ;


  57.         std::cout << std::endl ;
  58.         inflate = zlib.gzinflate(decode);
  59.         std::cout << "Gzip Inflate String : \n" << inflate << std::endl; ;
  60.         std::cout << std::endl ;
  61.           */


  62.         std::cout << std::endl ;
  63.         std::cout << std::endl ;
  64.         std::cout << "zlib deflate : \n" ;
  65.         deflate = zlib.gzdeflate(test2,-1);
  66.         std::cout << "gzdeflate attribute    : \n" << zlib.deflateStr << std::endl;
  67.         std::cout << std::endl ;
  68.         std::cout << "GZIP deflate HEX : \n" ;
  69.         for(i=0 ; i< zlib.deflateStr.length(); i++){
  70.                 num = zlib.deflateStr[i] ;
  71.                 std::cout << hex << num << " " ;
  72.         }
  73.         std::cout << std::endl ;

  74.         std::cout << "Gzip indeflate : \n" ;

  75.         inflate = zlib.gzinflate(deflate);
  76.         std::cout << "Gzip Inflate String : \n" << inflate << std::endl; ;

  77. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-08-22 08:42 |只看该作者
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP