2011年7月1日星期五

zlib的使用

zlib 下载
http://www.zlib.net/

也可以通过命令安装
sudo apt-get install zlib1g-dev zlib1g-dbg zlib1g

读取gz压缩文件与读取普通 文件的方法基本一致..更详细的使用方法可参考zlib自带的example.c

在eclipse下, 编译通过但是链接出错, 解决的方法是:
In Project Properties -> CGG++ linker ->libraries, add just "z".

class CGZFileReader
{
public:
CGZFileReader( const char* pszFName );
~CGZFileReader( );

public:
bool fnReadNextSentence( char *pszLine, int* piLength );

private:
gzFile m_fIn;
};

/*
* gzfilereader.cpp
*
* Created on: 1 Jul 2011
*/


/* user-defined headers */
#include "gzfilereader.h"

/* standard headers */
#include
#include

CGZFileReader::CGZFileReader( const char* pszFName )
{
m_fIn = gzopen( pszFName, "r" );
if ( m_fIn == NULL )
{
printf( "ERROR in CGZFileReader::CGZFileReader: FILE %s not found!\n", pszFName );
return;
}
}

CGZFileReader::~CGZFileReader( )
{
if ( m_fIn != NULL )
gzclose( m_fIn );
}

bool CGZFileReader::fnReadNextSentence( char *pszLine, int* piLength )
{
if ( gzeof( m_fIn ) == true )
return false;

int iLen;

pszLine[ 0 ] = '\0';

gzgets( m_fIn, pszLine, 10001 );
iLen = strlen( pszLine );
while ( iLen > 0 && pszLine[ iLen - 1 ] > 0 && pszLine[ iLen -1 ] < 33 )
{
pszLine[ iLen - 1 ] = '\0';
iLen--;
}

if ( piLength != NULL )
(*piLength) = iLen;

return true;
}


class CGZFileWriter
{
public:
CGZFileWriter( const char *pszFName, const char *pszModel );
~CGZFileWriter( );

public:
void fnWrite( const char *pszLine );
void fnWriteLine( const char *pszLine );

private:
gzFile m_fOut;
};

/*
* gzfilewriter.cpp
*
* Created on: 1 Jul 2011
*/

/* user-defined headers */
#include "gzfilewriter.h"

/* standard headers */
#include

CGZFileWriter::CGZFileWriter( const char *pszFName, const char *pszModel )
{
m_fOut = gzopen( pszFName, pszModel );

if ( m_fOut == NULL )
{
printf( "ERROR in CGZFileWriter::CGZFileWriter: cann't write FILE %s!\n", pszFName );
return;
}
}

CGZFileWriter::~CGZFileWriter( )
{
if ( m_fOut != NULL )
gzclose( m_fOut );
}

void CGZFileWriter::fnWrite( const char *pszLine )
{
gzprintf( m_fOut, "%s", pszLine );
}

void CGZFileWriter::fnWriteLine( const char *pszLine )
{
gzprintf( m_fOut, "%s\n", pszLine );
}


没有评论: