bzip数据压缩 C调用

本文介绍了如何使用C语言调用bzip库进行数据压缩和解压缩。通过官网文档学习,理解了bzip的压缩原理,特别是有限状态机在压缩过程中的应用。在压缩过程中,涉及RUNNING、FLUSHING、FINISHING和IDLE四个状态。代码实现包括初始化、压缩、数据输出和资源释放等关键步骤,并提到了异常处理和状态转换规则。
摘要由CSDN通过智能技术生成


bzip官网:http://www.bzip.org/  有源码及英文文档。


官网的英文文档比较齐全,可就是没有找到类似的demo代码,没办法,只能结合已有的官方文档,自己摸索了。

由于需要对内存的数据进行加压解压处理,因此调用的是bzip提供的低级接口:

typedef struct {
  char *next_in;          // 输入指针
  unsigned int avail_in;  // 输入数据长
  unsigned int total_in_lo32;
  unsigned int total_in_hi32;

  char *next_out;         // 输出指针
  unsigned int avail_out; // 输出缓冲大小
  unsigned int total_out_lo32;
  unsigned int total_out_hi32;

  void *state;

  void *(*bzalloc)(void *,int,int);
  void (*bzfree)(void *,void *);
  void *opaque;
} bz_stream;

int BZ2_bzCompressInit ( bz_stream *strm, 
                         int blockSize100k, 
                         int verbosity,
                         int workFactor );
//一般使用默认推荐参数:BZ2_bzCompressInit(strm,5,2,30);

int BZ2_bzCompress ( bz_stream *strm, int action );

int BZ2_bzCompressEnd ( bz_stream *strm );

int BZ2_bzDecompressInit ( bz_stream *strm, int verbosity, int small );
// 一般使用默认推荐参数:BZ2_bzDecompressInit(strm,2,1);

int BZ2_bzDecompress ( bz_stream *strm );

int BZ2_bzDecompressEnd ( bz_stream *strm );

压缩过程:

1. 初始化压缩参数,分配资源。

2. 对数据进行压缩,输出压缩后的数据。

    压缩的过程实际上是一个有限状态机处理过程,看英文文档感觉比较绕,比较麻烦。

3. 销毁释放分配的资源。



压缩过程的有限状态机:

RUNNING   - 输入数据进行压缩。

FLUSHING  - 输出压缩后的数据。

FINISHING - 没有输入,只有输出。

IDLE      - 压缩未开始或者压缩结束。



压缩遵循的原则:
  

  1. 进入压缩,即进入RUNNING状态,输入待压缩数据,如果BZ2_bzCompress ( bz_stream *strm, int action )返回BZ_RUN_OK,则继续输入。直到返回非BZ_RUN_OK或者没有更多的输入。

  2. 如果上面返回的是BZ_FLUSH_OK,进入FLUSHING状态,输出压缩后的数据。

  3. 如果返回的是BZ_FINISH_OK,进入FINISHING状态,进行输出,直到返回BZ_STREAM_END结束。


  在压缩的过程中,如果出现错误或者异常,如果捕获到其他异常,压缩过程应该就是失败的了。具体异常返回查看

使用ZLIB库 包装的压缩解压缩文件的源码 VS2005 工程创建 /* */ class ZIPWRAP_EXP CZipper { public: CZipper(); virtual ~CZipper(); // simple interface static bool ZipFile(const char* szFilePath); // saves as same name with .zip static bool ZipFolder(const char* szFilePath, bool ignoreself = false); // saves as same name with .zip bool AddFolderToZipFile(const char*foldername, const char* rootfolder); bool AddFileToZipFile(const char*filename, const char*relfolder = NULL, const char* comment = NULL); bool AddFolderOnlyPathToFile(const char* foldername, const char* comment = NULL); bool OpenZipFile(const char* zipfilename, bool append = false); bool CloseZipFile(const char* global_comment = NULL); private: void* zipfile_;/* = NULL */ }; /* */ #define MAX_COMMENT (255) /* tm_unz contain date/time info */ typedef struct UZ_s { unsigned int tm_sec; /* seconds after the minute - [0,59] */ unsigned int tm_min; /* minutes after the hour - [0,59] */ unsigned int tm_hour; /* hours since midnight - [0,23] */ unsigned int tm_mday; /* day of the month - [1,31] */ unsigned int tm_mon; /* months since January - [0,11] */ unsigned int tm_year; /* years - [1980..2044] */ } UZ_s; // create our own fileinfo struct to hide the underlying implementation struct UZ_FileInfo { char szFileName[260 + 1]; char szComment[255 + 1]; unsigned long dwVersion; unsigned long dwVersionNeeded; unsigned long dwFlags; unsigned long dwCompressionMethod; unsigned long dwDosDate; unsigned long dwCRC; unsigned long dwCompressedSize; unsigned long dwUncompressedSize; unsigned long dwInternalAttrib; unsigned long dwExternalAttrib; bool bFolder; UZ_s tmu_date; }; class ZIPWRAP_EXP CUnZipper { public: CUnZipper(); virtual ~CUnZipper(); // simple interface static bool UnZip( const char* filename, const char* dstfolder, bool ingorepath = false, const char* password = NULL); bool OpenUnZipFile(const char* filename); bool CloseUnZipFile(); bool UnZipTo( const char* dstfolder, bool ingorepath = false, const char* password = NULL); int GetFileCount(); bool GotoFirstFile(); bool GotoNextFile(); bool GotoZipFile(int index); bool GotoZipFile(const char* zipfilename); bool GetCurrentFileInfo(UZ_FileInfo&fileinfo;); bool UnCurrentZipFile(const char* dstfolder, bool ingorepath = false, const char* password = NULL); bool UnOneZipFile(const char* filename, const char* dstfolder, bool ingorepath = false, const char* password = NULL); bool UnOneZipFile(int index, const char* dstfolder, bool ingorepath = false, const char* password = NULL); private: void* unzipfile_; };
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值