最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間: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;
}

相關(guān)文章

  • C語言基礎(chǔ)解析之分支與循環(huán)語句

    C語言基礎(chǔ)解析之分支與循環(huán)語句

    C語言是一門結(jié)構(gòu)化的程序設(shè)計(jì)語言,當(dāng)C語言用來描述生活中的事物時(shí),會(huì)用到三種結(jié)構(gòu):順序結(jié)構(gòu)(不去贅述),選擇結(jié)構(gòu)(對(duì)應(yīng)分支語句),循環(huán)結(jié)構(gòu)(對(duì)應(yīng)循環(huán)語句),分支語句:分支語句分為兩種,一種是if語句,一種是switch語句
    2021-09-09
  • 指針操作數(shù)組的兩種方法(總結(jié))

    指針操作數(shù)組的兩種方法(總結(jié))

    下面小編就為大家?guī)硪黄羔槻僮鲾?shù)組的兩種方法(總結(jié))。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-05-05
  • 深入理解二叉樹的非遞歸遍歷

    深入理解二叉樹的非遞歸遍歷

    本篇文章是對(duì)二叉樹的非遞歸遍歷進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • C++ sleep()和usleep()的區(qū)別

    C++ sleep()和usleep()的區(qū)別

    本文主要介紹了C++ sleep()和usleep()的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • C語言實(shí)現(xiàn)冒泡排序算法的示例詳解

    C語言實(shí)現(xiàn)冒泡排序算法的示例詳解

    這篇文章主要介紹了C語言如何實(shí)現(xiàn)冒泡排序算法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • C語言多線程服務(wù)器的實(shí)現(xiàn)實(shí)例

    C語言多線程服務(wù)器的實(shí)現(xiàn)實(shí)例

    這篇文章主要介紹了C語言多線程服務(wù)器的實(shí)現(xiàn)實(shí)例,文章用實(shí)例講解的很清楚,有對(duì)這方面不太懂的同學(xué)可以參考下
    2021-02-02
  • 200行C語言代碼實(shí)現(xiàn)簡易三子棋游戲

    200行C語言代碼實(shí)現(xiàn)簡易三子棋游戲

    三子棋(井字棋)我們的童年或多或少都體驗(yàn)過這個(gè)游戲的樂趣,本子隨手一畫就是一局游戲的開始,下面這篇文章主要給大家介紹了關(guān)于200行C語言代碼實(shí)現(xiàn)簡易三子棋游戲的相關(guān)資料,需要的朋友可以參考下
    2023-05-05
  • C語言文件操作大全

    C語言文件操作大全

    這篇文章主要介紹了C語言文件操作大全的相關(guān)資料,需要的朋友可以參考下
    2018-03-03
  • MFC對(duì)話框中添加狀態(tài)欄的方法

    MFC對(duì)話框中添加狀態(tài)欄的方法

    這篇文章主要介紹了MFC對(duì)話框中添加狀態(tài)欄的方法,實(shí)例分析了MFC對(duì)話框添加狀態(tài)欄所涉及的相關(guān)成員變量與事件實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2015-07-07
  • C語言深入探究棧的原理

    C語言深入探究棧的原理

    一種特殊的線性表,其只允許在固定的一端進(jìn)行插入和刪除元素操作。進(jìn)行數(shù)據(jù)插入和刪除操作的一端 稱為棧頂,另一端稱為棧底。棧中的數(shù)據(jù)元素遵守后進(jìn)先出LIFO(Last In First Out)的原則
    2021-11-11

最新評(píng)論

政和县| 田东县| 扬州市| 沙洋县| 宜兴市| 弥勒县| 宜川县| 高陵县| 绍兴市| 兴隆县| 米脂县| 柘城县| 南丰县| 吴忠市| 鹤壁市| 托里县| 白银市| 五家渠市| 北宁市| 库车县| 兴安县| 汉沽区| 江孜县| 安阳市| 监利县| 越西县| 无为县| 凌海市| 沧源| 漳平市| 常熟市| 榆树市| 农安县| 沙田区| 潮州市| 綦江县| 仁布县| 缙云县| 台江县| 离岛区| 莒南县|