C++获取zip文件列表方法

 更新时间:2012年12月05日 09:22:18   作者:  
本文将介绍获取zip文件列表的方法,有些新手的朋友可以参考下

// ZipFile.h
//
#ifndef ZIPFILE_H
#define ZIPFILE_H
#include <string>
#include <vector>
#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<void*> m_FileAttributes;
};
#endif /*ZIPFILE_H */
//
// ZipFile.cpp : implementation file
//
#include <stdio.h>
#include <assert.h>
#include <string.h>
#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;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);
}
int CZipFile::ReadFileHeader(BYTE * data, FileHeader * hdr)
{
/*Name: int CZipFile::ReadFileHeader(BYTE * data, CString* stData)
/*It read the file header in the Central Directory Structure
Return the number of bytes read; if the stream does not contain
a valid local_file_header 0 is returned and the stream pointer (f)
is not modified
st is filled with all the data;
*/
BYTE * origdata=data;
// FileHeader hdr;
//fill the values into the file_header structure
hdr->signature = (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]='\0';
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]='\0';
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]='\0';
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))<<8)+(ui32)*(buf);
break;
case(24):
value = (((ui32)*(buf+2))<<16)+(((ui32)*(buf+1))<<8)+((ui32)*(buf));
break;
case(32):
value = (((ui32)*(buf+3))<<24)+(((ui32)*(buf+2))<<16)+(((ui32)*(buf+1))<<8)+((ui32)*(buf));
break;
default:
assert(1);
break;
}
return(value);
}
FileHeader *CZipFile::GetFileAttributes(int index)
{
if(index<0 || index >m_FileAttributes.size())
return NULL;
else
return((FileHeader *)m_FileAttributes.at(index));
}
//main.cpp
#include <stdio.h>
#include "ZipFile.h"
int main(int argc , char* argv[])
{
if(2 != argc)
{
printf("zipFile must provide.\n");
return 0;
}
CZipFile zipTest;
zipTest.SetFileName(argv[1]);
zipTest.OpenFile();
for(int i = 0;i< zipTest.GetFilesNumber();i++)
{
printf("%s\n", zipTest.GetFileAttributes(i)->file_name);
}
return 0;
}

相关文章

  • C++实现简易反弹小球游戏的示例代码

    C++实现简易反弹小球游戏的示例代码

    我们利用printf 函数实现一个在屏幕上弹跳的小球。弹跳的小球游戏比较简单、容易入门,也是反弹球消砖块、接金币、台球等很多游戏的基础,感兴趣的可以了解一下
    2022-10-10
  • C语言杨氏矩阵简单实现方法

    C语言杨氏矩阵简单实现方法

    杨氏矩阵是一个数字矩阵,矩阵的每一行从左到右一次递增,矩阵从上到下递增,在这样的矩阵中查找一个数字是否存在。时间复杂度小于O(N),有需要的朋友可以借鉴参考下
    2023-02-02
  • C++模拟实现STL容器vector的示例代码

    C++模拟实现STL容器vector的示例代码

    这篇文章主要为大家详细介绍了C++如何模拟实现STL容器vector的相关资料,文中的示例代码讲解详细,对我们学习C++有一定帮助,需要的可以参考一下
    2022-11-11
  • C语言实现顺序表的插入删除

    C语言实现顺序表的插入删除

    这篇文章主要介绍了C语言实现顺序表的插入删除,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-05-05
  • C++中类的成员函数及内联函数使用及说明

    C++中类的成员函数及内联函数使用及说明

    这篇文章主要介绍了C++中类的成员函数及内联函数使用及说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-11-11
  • C语言实现BMP图像处理(彩色图转灰度图)

    C语言实现BMP图像处理(彩色图转灰度图)

    这篇文章主要为大家详细介绍了C语言实现BMP图像处理,彩色图转灰度图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-10-10
  • 大数据情况下桶排序算法的运用与C++代码实现示例

    大数据情况下桶排序算法的运用与C++代码实现示例

    在排序元素很多的情况下,其实桶排序的性能并不是太高,这里我们配合单链表的直接插入排序,来看下一大数据情况下桶排序算法的运用与C++代码实现示例:
    2016-07-07
  • C++初级线程管理

    C++初级线程管理

    这篇文章主要介绍了C++初级线程管理,C++11中提供了std::thread库,本文将从线程的启动、线程等待、线程分离、线程传参、线程识别等几个方面介绍初级线程管理的知识,需要的朋友可以参考一下
    2021-12-12
  • C++命名空间namespace的介绍与使用

    C++命名空间namespace的介绍与使用

    今天小编就为大家分享一篇关于C++命名空间namespace的介绍与使用,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • cmake跨平台构建工具的学习笔记

    cmake跨平台构建工具的学习笔记

    CMake是一个跨平台的安装/编译工具,通过CMake我们可以通过简单的语句来描述所有平台的安装/编译过程,下面这篇文章主要给大家介绍了关于cmake跨平台构建工具的相关资料,需要的朋友可以参考下
    2023-02-02

最新评论