zlib库源码编绎与使用

下载源码http://zlib.net
解压
tar -zxvf zlib-1.2.11.tar.gz
进入目录
cd zlib-1.2.11
新建文件夹并进入
mkdir build
cd build
执行cmake
cmake …
编绎与安装
make
make install
编绎完成
Install the project…
– Install configuration: “”
– Installing: /usr/local/lib/libz.so.1.2.11
– Installing: /usr/local/lib/libz.so.1
– Installing: /usr/local/lib/libz.so
– Installing: /usr/local/lib/libz.a
– Installing: /usr/local/include/zconf.h
– Installing: /usr/local/include/zlib.h
– Installing: /usr/local/share/man/man3/zlib.3
– Installing: /usr/local/share/pkgconfig/zlib.pc

#include <iostream>
#include <fstream>
#include "file_compress.h"
#include "zip/zipconf.h"
#include "zip/zip.h"
#include "dirent.h"
#include <sys/stat.h>
#include "zip/zlib.h"
#include <vector>
#include <string>
#include <string.h>
#define UNPACKSIZE 1024;

using namespace std;

//Pack directory to zipfile
bool PackDirectory(const string& pack_path, const string& pack_file)
{
  vector<string> filenames;
  DIR* pdir = nullptr;
  struct dirent* pdirent = nullptr;
  if((pdir = opendir(pack_path.c_str())) == nullptr) return false;
  while ((pdirent = readdir(pdir)) != nullptr) {
    if(strcmp(pdirent->d_name, ".") != 0 && strcmp(pdirent->d_name, "..") != 0) {
      filenames.push_back(pack_path + pdirent->d_name);
    }
  }

  int ierr = 0;
  zip* zipfile = nullptr;
  zip_source* srcfile = nullptr;

  string path = pack_file.substr(0, pack_file.find_last_of("/"));
  if (NULL == opendir(path.c_str())) {
    if (mkdir(path.c_str(), 0777) != 0) return false;
  } else if (access(pack_file.c_str(), F_OK) != -1) {
    int r = remove(pack_file.c_str());
    if (r != 0) return false;
  }

  zipfile = zip_open(pack_file.c_str(), ZIP_CREATE | ZIP_TRUNCATE, &ierr);
  if(!zipfile) return false;
    
  for (const auto& file : filenames) {
    srcfile = zip_source_file(zipfile, file.c_str(), 0, -1);
    if (!srcfile) {
      zip_close(zipfile);
      return false;
    }
    zip_file_add(zipfile, file.c_str(), srcfile, ZIP_FL_OVERWRITE);
  }
  zip_close(zipfile);
  return true;
}

bool UnPackDirectory(const string& unpach_path, const string& unpack_file)
{
  int ierr = 0;
  zip* zipfile = nullptr;
  zip_source* srcfile = nullptr;

  if (NULL == opendir(unpach_path.c_str())) {
      if (mkdir(unpach_path.c_str(), 0777) != 0) return false;
  }

  zipfile = zip_open(unpack_file.c_str(), ZIP_CHECKCONS, &ierr);
  if(zipfile == nullptr) return false;
  zip_int64_t i64num = zip_get_num_entries(zipfile, 0);
  struct zip_stat stat;
  zip_file* pfile = nullptr;
  for (zip_int64_t i=0; i<i64num; i++) {
    zip_stat_index(zipfile, i, 0, &stat);
    pfile = zip_fopen_index(zipfile, i, 0);
    if (!pfile) {
      zip_close(zipfile);
      return false;
    }

    string filename(stat.name);
    filename = unpach_path + filename.substr(filename.find_last_of("/")+1);
    if (access(filename.c_str(), F_OK) != -1) {
      int r = remove(filename.c_str());
      if (r != 0) {
        zip_close(zipfile);
        return false;
      }
    }
    FILE *fp = fopen(filename.c_str(), "wb");
    if (!fp) {
      zip_close(zipfile);
      return false;
    }
    char buff[stat.size] = {0};
    zip_fread(pfile, buff, stat.size);
    fwrite(buff, 1, stat.size, fp);
    fclose(fp);
  }
  zip_close(zipfile);
  return true;
}

bool CompressFile(const string& deflate_file, const string& inflate_file)
{
  ifstream ifs;
  ifs.open(inflate_file.c_str(), fstream::binary);
  if (!ifs.is_open()) return false;
  ifs.seekg(0, ios::end);
  unsigned long sourcelen = ifs.tellg();
  char sourcebuff[sourcelen] = {0};
  ifs.seekg(0, ios::beg);
  ifs.read(sourcebuff, sourcelen);
  ifs.close();

  unsigned long destlen = compressBound(sourcelen);
  unsigned char destbuff[destlen] = {0};
  
  int len = compress(destbuff, &destlen, (unsigned char*)(sourcebuff), sourcelen);

  string path = deflate_file.substr(0, deflate_file.find_last_of("/"));
  if (NULL == opendir(path.c_str())) {
    if (mkdir(path.c_str(), 0777) != 0) return false;
  } else if (access(deflate_file.c_str(), F_OK) != -1) {
    int r = remove(deflate_file.c_str());
    if (r != 0) return false;
  }
  FILE *fp = fopen(deflate_file.c_str(), "wb");
  if (!fp) return false;
  fwrite(destbuff, 1, destlen, fp);
  fclose(fp);
  return true;
}

bool UnCompressFile(const string& deflate_file, const string& inflate_file)
{
  ifstream ifs;
  ifs.open(deflate_file.c_str(), fstream::binary);
  if (!ifs.is_open()) return false;
  ifs.seekg(0, ios::end);
  unsigned long sourcelen = ifs.tellg();
  char sourcebuff[sourcelen] = {0};
  ifs.seekg(0, ios::beg);
  ifs.read(sourcebuff, sourcelen);
  ifs.close();

  string path = inflate_file.substr(0, inflate_file.find_last_of("/"));
  if (NULL == opendir(path.c_str())) {
    if (mkdir(path.c_str(), 0777) != 0) return false;
  } else if (access(inflate_file.c_str(), F_OK) != -1) {
    int r = remove(inflate_file.c_str());
    if (r != 0) return false;
  }

  unsigned long destlen = sourcelen * UNPACKSIZE;
  unsigned char* destbuff = new unsigned char[destlen];
  memset(destbuff, 0, destlen);
  int iresult = uncompress(destbuff, &destlen, (unsigned char*)sourcebuff, sourcelen);
  if (iresult == Z_BUF_ERROR) {
    delete destbuff;
    destlen = destlen * UNPACKSIZE;
    destbuff = new unsigned char[destlen];
    memset(destbuff, 0, destlen);
    iresult = uncompress(destbuff, &destlen, (unsigned char*)sourcebuff, sourcelen);
    if (iresult != Z_OK) {
      delete destbuff;
      return false;
    }
  }
  
  FILE *fp = fopen(inflate_file.c_str(), "w+");
  if (!fp) {
    delete destbuff;
    return false;
  }
  fwrite(destbuff, 1, destlen, fp);
  delete destbuff;
  fclose(fp);
  return true;
}

bool CompressgzFile(const string& deflate_file, const string& inflate_file)
{ 
  ifstream ifs;
  ifs.open(inflate_file.c_str(), fstream::binary);
  if (!ifs.is_open()) return false;
  ifs.seekg(0, ios::end);
  unsigned long sourcelen = ifs.tellg();
  char sourcebuff[sourcelen] = {0};
  ifs.seekg(0, ios::beg);
  ifs.read(sourcebuff, sourcelen);
  ifs.close();

  string path = deflate_file.substr(0, deflate_file.find_last_of("/"));
  if (NULL == opendir(path.c_str())) {
    if (mkdir(path.c_str(), 0777) != 0) return false;
  } else if (access(deflate_file.c_str(), F_OK) != -1) {
    int r = remove(deflate_file.c_str());
    if (r != 0) return false;
  }

  gzFile gzfile = gzopen(deflate_file.c_str(), "wb");
  if (!gzfile) return false;

  gzwrite(gzfile, sourcebuff, sourcelen);
  gzclose(gzfile);
  return true;
}

bool UnCompressgzFile(const string& deflate_file, const string& inflate_file)
{
  string path = inflate_file.substr(0, inflate_file.find_last_of("/"));
  if (NULL == opendir(path.c_str())) {
    if (mkdir(path.c_str(), 0777) != 0) return false;
  } else if (access(inflate_file.c_str(), F_OK) != -1) {
    int r = remove(inflate_file.c_str());
    if (r != 0) return false;
  }
  FILE *fp = fopen(inflate_file.c_str(), "wb+");
  if (!fp) return false;
  
  gzFile gzfile = gzopen(deflate_file.c_str(), "rb");
  if (!gzfile) {
    fclose(fp);
    return false;
  }
  
  while (1) {
    char buff[1024] = {0};
    int len = gzread(gzfile, buff, sizeof(buff));
    if (len <= 0)
      break;
    fwrite(buff, 1, len, fp);
  }
  gzclose(gzfile);
  fclose(fp);
  return true;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值