zlib将数据直接写为gz压缩文件
2013-07-03 17:40
561 查看
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;
}
////////////////////////////////////////////////////////////////
//
//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;
}
相关文章推荐
- python直接按行读取gz压缩文件中的文本文件的数据
- hive 压缩全解读(hive表存储格式以及外部表直接加载压缩格式数据);HADOOP存储数据压缩方案对比(LZO,gz,ORC)
- (转载)IQ 16.0 SP02起支持从压缩文件直接装载数据到表中
- hive 压缩全解读(hive表存储格式以及外部表直接加载压缩格式数据);HADOOP存储数据压缩方案对比(LZO,gz,ORC)
- 提取gz文件中的压缩部分数据,C语言实现
- 10g直接删除数据文件后的启动
- 如何通过直接复制frm文件以实现恢复/复制innodb数据表?
- perl 打开和输出gz压缩文件【更新perl打开的时候自动判断是否压缩文件的模块】
- 文件下载时直接对流进行zip加密压缩
- php 数据文件的压缩
- tar.gz文件压缩解压方法
- php生成gz格式压缩文件
- 在DELPHI中使用ADO直接访问Excel数据文件(收藏)
- [Zlib]_[初级]_[使用zlib库压缩文件]
- 修复损坏的gz或tar.gz压缩文件之方法篇
- pip 直接安装tar.gz zip文件包 (windows linux mac 可用)
- Linux_查看.gz压缩文件行数_zcat或者gzip_-dc
- 将hive数据查询直接写入文件
- Oracle txt文件直接使用sqldlr将数据导入到数据库表中
- mysql 序列化缓存到txt文件查找数据与直接查找数据 性能对比,13,601条数据文件缓存平均0.085秒后只需0.025秒 推荐 程序员导航网http://www.je666.com