zlib写gz压缩文件,头文件ma_gzfile_op.h:
//
//Descript: gzfile option.
// Author: guowenyan
// Date: 2013.07.01
//
#pragma once
#include <string>
#include <zlib.h>
class CMaGzFileOp
{
public:
CMaGzFileOp(std::string file_name, std::string open_mode);
~CMaGzFileOp();
public:
bool open_file();
bool seek_file(long offset, int origin);
bool write_file(const char *buf, int len);
void close_file();
private:
bool is_gz_file();
public:
std::string m_file_name;
std::string m_open_mode;
gzFile m_gz_fp;
};
zlib写gz压缩文件,实现文件ma_gzfile_op.cpp:
//
//Descript: gzfile option.
// Author: guowenyan
// Date: 2013.07.01
//
#include "ma_gzfile_op.h"
#include "ma_dir_op.h"
#include "ma_type.h"
using namespace std;
CMaGzFileOp::CMaGzFileOp(string file_name, string open_mode) : m_file_name(file_name), m_open_mode(open_mode), m_gz_fp(NULL)
{
}
CMaGzFileOp::~CMaGzFileOp()
{
close_file();
}
bool CMaGzFileOp::open_file()
{
if(NULL == m_gz_fp)
m_gz_fp = gzopen(m_file_name.c_str(),m_open_mode.c_str());
return NULL != m_gz_fp;
}
bool CMaGzFileOp::seek_file(long offset, int origin)
{
if(NULL == m_gz_fp)
return false;
return -1 != gzseek(m_gz_fp, offset, origin);
}
bool CMaGzFileOp::write_file(const char *buf, int len)
{
if(NULL == m_gz_fp)
return false;
return gzwrite(m_gz_fp, buf, len) == len;
}
void CMaGzFileOp::close_file()
{
if(m_gz_fp)
{
gzclose(m_gz_fp);
m_gz_fp = NULL;
}
}
bool CMaGzFileOp::is_gz_file()
{
CMaDirOperation *p_dir_op = CMaDirOperation::get_instance();
if(p_dir_op)
return p_dir_op->is_file_type(m_file_name, MA_FILE_TYPE_GZ);
return false;
}
zlib缓存写gz压缩文件,头文件ma_gzfile_buf.h:
//
//Descript: gzfile option with buf when write.
// Author: guowenyan
// Date: 2013.07.02
//
#pragma once
#include <string.h>
#include "ma_gzfile_op.h"
class CMaGzfileBuf
{
public:
CMaGzfileBuf(std::string file_name, std::string open_mode);
~CMaGzfileBuf();
public:
bool open_file();
bool seek_file(long offset, int origin);
bool write_file(const char *buf, int len);
bool write_file_with_buf(const char *buf, int len);
void close_file();
private:
void new_read_buf();
void delete_read_buf();
bool write_buf();
private:
std::string m_file_name;
std::string m_open_mode;
CMaGzFileOp m_gzfile_op;
char *m_p_write_buf;
long m_write_buf_len;
};
zlib缓存写gz压缩文件,实现文件ma_gzfile_buf.cpp:
//
//Descript: gzfile option with buf when write.
// Author: guowenyan
// Date: 2013.07.02
//
#include "ma_gzfile_buf.h"
#include "ma_type.h"
#include <iostream>
using namespace std;
CMaGzfileBuf::CMaGzfileBuf(string file_name, string open_mode) : m_file_name(file_name), m_open_mode(open_mode), m_gzfile_op(m_file_name, m_open_mode),
m_p_write_buf(NULL), m_write_buf_len(0)
{
}
CMaGzfileBuf::~CMaGzfileBuf()
{
close_file();
delete_read_buf();
}
bool CMaGzfileBuf::open_file()
{
return m_gzfile_op.open_file();
}
bool CMaGzfileBuf::seek_file(long offset, int origin)
{
return m_gzfile_op.seek_file(offset, origin);
}
bool CMaGzfileBuf::write_file(const char *buf, int len)
{
return m_gzfile_op.write_file(buf, len);
}
bool CMaGzfileBuf::write_file_with_buf(const char *buf, int len)
{
if(len <= 0)
return false;
if((NULL == m_p_write_buf) && (0 == m_write_buf_len))
{
new_read_buf();
}
if(NULL != m_p_write_buf)
{
bool b_write = true;
if(len >= MA_WRITE_FILE_BUF_LEN)
{
b_write = write_buf();
return b_write && write_file(buf, len);
}
if(m_write_buf_len + len > MA_WRITE_FILE_BUF_LEN)
{
b_write = write_buf();
}
strncpy(m_p_write_buf+m_write_buf_len, buf, len);
m_write_buf_len += len;
return b_write;
}
return write_file(buf, len);
}
void CMaGzfileBuf::close_file()
{
write_buf();
return m_gzfile_op.close_file();
}
void CMaGzfileBuf::new_read_buf()
{
if(NULL == m_p_write_buf)
{
m_p_write_buf = new(nothrow) char[MA_WRITE_FILE_BUF_LEN + 1];
if(NULL == m_p_write_buf)
{
cout<<"Fail to new write buf in CMaGzfileBuf::new_read_buf()"<<endl;
}
}
}
void CMaGzfileBuf::delete_read_buf()
{
if(m_p_write_buf)
{
delete []m_p_write_buf;
m_p_write_buf = NULL;
}
}
bool CMaGzfileBuf::write_buf()
{
if((NULL != m_p_write_buf) && (0 != m_write_buf_len))
{
bool b_write = write_file(m_p_write_buf, m_write_buf_len);
if(b_write)
{
m_write_buf_len = 0;
}
return b_write;
}
return true;
}