123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- #include "UZipFile.h"
- #include <string>
- #include <codecvt>
- UZipFile::UZipFile(void)
- :m_nCurrentEntryNum(0),
- m_nEntryNum(0),
- m_bIsCurrentFileOpened(false),
- m_bIsZipOpened(false)
- {
- }
- UZipFile::~UZipFile(void)
- {
- CloseZip();
- }
- bool UZipFile::OpenZip()
- {
- uf = unzOpen2(NULL,(zlib_filefunc_def* )m_pzlib_filefunc_def);
- if (uf == NULL)
- {
- return false;
- }
- unz_global_info64 gi;
- if (unzGetGlobalInfo64(uf, &gi) != UNZ_OK)
- {
- return false;
- }
- m_nEntryNum = gi.number_entry;
- m_nCurrentEntryNum =0;
- m_bIsCurrentFileOpened =m_nEntryNum>0;
- return true;
- }
- std::string ws2s(const std::wstring& ws)
- {
- std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
- setlocale(LC_ALL, "chs");
- const wchar_t* _Source = ws.c_str();
- size_t _Dsize = 2 * ws.size() + 1;
- char *_Dest = new char[_Dsize];
- memset(_Dest,0,_Dsize);
- wcstombs(_Dest,_Source,_Dsize);
- std::string result = _Dest;
- delete []_Dest;
- setlocale(LC_ALL, curLocale.c_str());
- return result;
- }
- bool UZipFile::GetNextEntry( std::string & strFileName )
- {
- if(!HasMoreEntry()) return false;
- m_nCurrentEntryNum++;
- if(m_nCurrentEntryNum>1&&m_bIsCurrentFileOpened){
- unzCloseCurrentFile(uf);
- }
- if (m_nCurrentEntryNum>1&&unzGoToNextFile(uf) != UNZ_OK)
- {
- return false;
- }
- char szFilePathA[1024];
- unz_file_info64 FileInfo;
- if (unzGetCurrentFileInfo64(uf, &FileInfo, szFilePathA, sizeof(szFilePathA), NULL, 0, NULL, 0) != UNZ_OK)
- {
- return false;
- }
- #define ZIP_GPBF_LANGUAGE_ENCODING_FLAG 0x800
- if ((FileInfo.flag & ZIP_GPBF_LANGUAGE_ENCODING_FLAG) != 0)
- {
- std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
- std::wstring ws=conv.from_bytes(szFilePathA);
- strFileName = ws2s(ws);
- }
- else
- {
- strFileName =(szFilePathA);
- }
- if (unzOpenCurrentFile(uf) != UNZ_OK)
- {
- return false;
- }
- m_bIsCurrentFileOpened =true;
- return true;
- }
- int UZipFile::Read( void * buffer,int bufferSize )
- {
- return unzReadCurrentFile(uf, buffer, bufferSize);
- }
- void UZipFile::CloseZip()
- {
- if(m_bIsCurrentFileOpened){
- unzCloseCurrentFile(uf);
- m_bIsCurrentFileOpened =false;
- }
- if(m_bIsZipOpened){
- unzClose(uf);
- m_bIsZipOpened =false;
- }
- }
|