// CZipFile
CZipFile::CZipFile()
{
m_FileName=“”;
}
CZipFile::CZipFile(string fn)
{
m_FileName = fn;
}
CZipFile::~CZipFile()
{
ResetContent();
}
void CZipFile::ResetContent(void)
{
for(int i=0;i<GetFilesNumber();i++)
delete(GetFileAttributes(i));
m_FileAttributes.clear();
m_FileName=“”;
}
string CZipFile::GetFileName(void)
{
return m_FileName;
}
void CZipFile::SetFileName(string fn)
{
m_FileName = fn;
}
int CZipFile::GetFilesNumber(void)
{
return (m_FileAttributes.size());
}
bool CZipFile::OpenFile(void)
{
if(m_FileName==“”)
return ZIP_ERR_OPEN;
//read all the file data
FILE * fp;
fp=fopen(m_FileName.c_str(),“rb”);
if(fp==NULL)
return ZIP_ERR_OPEN;
fseek(fp,0,SEEK_END);
long siz=ftell(fp);
fseek(fp,0,SEEK_SET);
BYTE *buf=new BYTE[siz];
ui32 n= fread((void*) buf,(unsigned int)siz,1,fp);
fclose(fp);
//local file header signature control to check the file correctiveness
if(*((ui32*)buf)!=0x04034b50)
return ZIP_ERR_WRONG_FILE;
ReadCentralDirectory(buf,siz);
return ZIP_OK;
}
void CZipFile::ReadCentralDirectory(BYTE * data,long len)
{
/*read the central Directory Data Structure;
data contains the zipped archive;
return the number of files read*/
BYTE * tmp;
//search the signature
tmp=data;
ui32 * tmp2;
tmp2= (ui32 *)tmp;
len-=4;
while((*tmp2)!=0x02014b50 && len)
{
tmp++;
tmp2= (ui32 *)tmp;
len--;
}
//retrieve the FileHeader for each file
int siz;
do
{
FileHeader fhdr;
siz = ReadFileHeader(tmp, &fhdr);
if(siz)
{
FileHeader *pfhdr = new(FileHeader);
*pfhdr=fhdr;
m_FileAttributes.push_back(pfhdr);
}
tmp+=siz;
}while(siz!=0);
}