激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - C/C++ - C++獲取zip文件列表方法

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

2020-11-13 11:55C++教程網(wǎng) C/C++

本文將介紹C++獲取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++詳解c語言中的 strcpy和strncpy字符串函數(shù)使用

    詳解c語言中的 strcpy和strncpy字符串函數(shù)使用

    strcpy 和strcnpy函數(shù)是字符串復(fù)制函數(shù)。接下來通過本文給大家介紹c語言中的strcpy和strncpy字符串函數(shù)使用,感興趣的朋友跟隨小編要求看看吧...

    spring-go5642021-07-02
  • C/C++C++之重載 重定義與重寫用法詳解

    C++之重載 重定義與重寫用法詳解

    這篇文章主要介紹了C++之重載 重定義與重寫用法詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下...

    青山的青6062022-01-04
  • C/C++c++ 單線程實現(xiàn)同時監(jiān)聽多個端口

    c++ 單線程實現(xiàn)同時監(jiān)聽多個端口

    這篇文章主要介紹了c++ 單線程實現(xiàn)同時監(jiān)聽多個端口的方法,幫助大家更好的理解和學習使用c++,感興趣的朋友可以了解下...

    源之緣11542021-10-27
  • C/C++C語言中炫酷的文件操作實例詳解

    C語言中炫酷的文件操作實例詳解

    內(nèi)存中的數(shù)據(jù)都是暫時的,當程序結(jié)束時,它們都將丟失,為了永久性的保存大量的數(shù)據(jù),C語言提供了對文件的操作,這篇文章主要給大家介紹了關(guān)于C語言中文件...

    針眼_6702022-01-24
  • C/C++C/C++經(jīng)典實例之模擬計算器示例代碼

    C/C++經(jīng)典實例之模擬計算器示例代碼

    最近在看到的一個需求,本以為比較簡單,但花了不少時間,所以下面這篇文章主要給大家介紹了關(guān)于C/C++經(jīng)典實例之模擬計算器的相關(guān)資料,文中通過示...

    jia150610152021-06-07
  • C/C++C語言實現(xiàn)電腦關(guān)機程序

    C語言實現(xiàn)電腦關(guān)機程序

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)電腦關(guān)機程序,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    xiaocaidayong8482021-08-20
  • C/C++學習C++編程的必備軟件

    學習C++編程的必備軟件

    本文給大家分享的是作者在學習使用C++進行編程的時候所用到的一些常用的軟件,這里推薦給大家...

    謝恩銘10102021-05-08
  • C/C++深入理解goto語句的替代實現(xiàn)方式分析

    深入理解goto語句的替代實現(xiàn)方式分析

    本篇文章是對goto語句的替代實現(xiàn)方式進行了詳細的分析介紹,需要的朋友參考下...

    C語言教程網(wǎng)7342020-12-03
主站蜘蛛池模板: 国产精品剧情一区二区三区 | 久久久久久久久久综合 | 亚洲视频在线免费看 | 一起草av在线 | 天天操天天碰 | 久久久久久久久国产精品 | 国产xxxx岁13xxxxhd | 久久久久久久久久久av | 99精品视频网站 | 国产精品久久久久久久四虎电影 | 在线免费观看日韩视频 | 在线观看国产 | 91伊人久久 | 久久精品一区二区三区国产主播 | va免费视频| 一级电影在线观看 | 久久精品成人免费国产片桃视频 | 欧美激情精品久久久久久黑人 | 日韩欧美色综合 | 直接在线观看的三级网址 | 在线中文字幕观看 | 九九热视频免费观看 | 成人国产精品齐天大性 | 欧美a久久 | 午夜视频在线观 | 国产亚洲精久久久久久蜜臀 | 小视频免费在线观看 | a视频在线看 | 91网址在线观看 | 色阁阁69婷婷 | 国产在线一级片 | 综合在线一区 | 国内毛片视频 | 成人在线观看免费爱爱 | av7777777| 天天鲁在线视频免费观看 | av免费在线播放网址 | 成人一区三区 | 精品一区在线视频 | 国产一区二区三区在线免费观看 | 亚洲午夜不卡 |