C++获取zip文件列表方法
本文导语: // ZipFile.h // #ifndef ZIPFILE_H #define ZIPFILE_H #include #include #define ZIP_OK 0 #define ZIP_ERR_OPEN 1 #define ZIP_ERR_WRONG_FILE 2 #define ZIP_ERR_WRONG_HEADER 3 #define BYTE unsigned char #define ui32 unsigned int #define ui16 unsigned short struct FileHeader { ui32 signature; ui16 vers...
// ZipFile.h
//
#ifndef ZIPFILE_H
#define ZIPFILE_H
#include
#include
#define ZIP_OK 0
#define ZIP_ERR_OPEN 1
#define ZIP_ERR_WRONG_FILE 2
#define ZIP_ERR_WRONG_HEADER 3
#define BYTE unsigned char
#define ui32 unsigned int
#define ui16 unsigned short
struct FileHeader
{
ui32 signature;
ui16 version_made_by;
ui16 version_needed;
ui16 bitflags;
ui16 comp_method;
ui16 lastModFileTime;
ui16 lastModFileDate;
ui32 crc_32;
ui32 comp_size;
ui32 uncompr_size;
ui16 fname_len;
ui16 extra_field_len;
ui16 fcomment_len;
ui16 disk_num_start;
ui16 internal_fattribute;
ui32 external_fattribute;
ui32 relative_offset;
char* file_name;
char* extra_field;
char* file_comment;
};
class CZipFile
{
private:
public:
CZipFile();
CZipFile(std::string);
virtual ~CZipFile();
void ResetContent(void);
std::string GetFileName(void);
void SetFileName(std::string);
bool OpenFile(void);
int GetFilesNumber(void);
FileHeader * GetFileAttributes(int);
private:
void ReadCentralDirectory(BYTE * data,long len);
int ReadFileHeader(BYTE * data, FileHeader * hdr);
ui32 ReadValue(unsigned char * buf, int nbits);
std::string m_FileName;
std::vector m_FileAttributes;
};
#endif /*ZIPFILE_H */
//
// ZipFile.cpp : implementation file
//
#include
#include
#include
#include "ZipFile.h"
using namespace std;
/////////////////////////////////////////////////////////////////////////////
// CZipFile
CZipFile::CZipFile()
{
m_FileName="";
}
CZipFile::CZipFile(string fn)
{
m_FileName = fn;
}
CZipFile::~CZipFile()
{
ResetContent();
}
void CZipFile::ResetContent(void)
{
for(int i=0;isignature = (ui32) ReadValue(data ,32);
if(hdr->signature!=0x02014b50)
return 0; //no further file
hdr->version_made_by = (ui16) ReadValue(data+4 ,16);
hdr->version_needed = (ui16) ReadValue(data+6 ,16);
hdr->bitflags = (ui16) ReadValue(data+8 ,16);
hdr->comp_method = (ui16) ReadValue(data+10,16);
hdr->lastModFileTime = (ui16) ReadValue(data+12,16);
hdr->lastModFileDate = (ui16) ReadValue(data+14,16);
hdr->crc_32 = (ui32) ReadValue(data+16,32);
hdr->comp_size = (ui32) ReadValue(data+20,32);
hdr->uncompr_size = (ui32) ReadValue(data+24,32);
hdr->fname_len = (ui16) ReadValue(data+28,16);
hdr->extra_field_len = (ui16) ReadValue(data+30,16);
hdr->fcomment_len = (ui16) ReadValue(data+32,16);
hdr->disk_num_start = (ui16) ReadValue(data+34,16);
hdr->internal_fattribute = (ui16) ReadValue(data+36,16);
hdr->external_fattribute = (ui32) ReadValue(data+38,32);
hdr->relative_offset = (ui32) ReadValue(data+42,32);
data+=46;
if(hdr->fname_len>0)
{
char *fn;
fn=new (char[hdr->fname_len+1]);
strncpy(fn,(char*)data,hdr->fname_len);
fn[hdr->fname_len]='';
hdr->file_name = fn;
data+=hdr->fname_len;
}
if(hdr->extra_field_len>0)
{
char *fn;
fn=new (char[hdr->extra_field_len+1]);
strncpy(fn,(char*)data,hdr->extra_field_len);
fn[hdr->extra_field_len]='';
hdr->extra_field = fn;
data += hdr->extra_field_len;
}
//file comment
if(hdr->fcomment_len>0)
{
char *fn;
fn=new (char[hdr->fcomment_len+1]);
strncpy(fn,(char*)data,hdr->fcomment_len);
fn[hdr->fcomment_len]='';
hdr->file_comment = fn;
data += hdr->extra_field_len;
}
return (data-origdata);
}
ui32 CZipFile::ReadValue(unsigned char * buf, int nbits)
{
/*Name: void ReadValue(char*buf, int nbits)
/*Return the value read from the buffer of size nbits;
*/
ui32 value = 0;
switch (nbits)
{
case (8):
value = (ui32)*(buf);
break;
case(16):
value = (((ui32)*(buf+1))