// 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++获取zip文件列表方法
2020-11-13 11:55C++教程网 C/C++
本文将介绍C++获取zip文件列表的方法,有些新手的朋友可以参考下
延伸 · 阅读
- 2022-03-11C++多线程之互斥锁与死锁
- 2022-03-11C++学习之线程详解
- 2022-03-11如何通过C++求出链表中环的入口结点
- 2022-03-11一篇文章了解c++中的new和delete
- 2022-03-11C/C++ Qt数据库SqlRelationalTable关联表详解
- 2022-03-11C++中signed main和int main的区别
- C/C++
如何用C语言、Python实现栈及典型应用
本文先通过实例分别介绍了如何用C语言、Python实现栈,后又介绍栈的典型应用,对大家学习栈很有借鉴参考价值,下面一起来看看吧。...
- C/C++
C语言实现随机抽奖程序
这篇文章主要为大家详细介绍了C语言实现随机抽奖程序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...
- C/C++
Opencv绘制最小外接矩形、最小外接圆
这篇文章主要为大家详细介绍了Opencv绘制最小外接矩形、最小外接圆的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以...
- C/C++
构造函数不能声明为虚函数的原因及分析
构造函数不需要是虚函数,也不允许是虚函数,因为创建一个对象时我们总是要明确指定对象的类型,尽管我们可能通过实验室的基类的指针或引用去访问...
- C/C++
C++输入一个字符串,把其中的字符按照逆序输出的两种方法解析
以下是对C++中输入一个字符串,把其中的字符按照逆序输出的两种方法进行了详细的分析介绍,需要的朋友可以过来参考下...
- C/C++
浅谈防不胜防的unsigned int的运算
下面小编就为大家带来一篇浅谈防不胜防的unsigned int的运算。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...
- C/C++
深入分析C++中deque的使用
本篇文章介绍了,深入分析C++中deque的使用。需要的朋友参考下...
- C/C++
Visual Studio C++指针靠前靠后的问题全面解析
这篇文章主要介绍了Visual Studio C++指针靠前靠后的问题全面解析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可...