#include "UZipFile.h" #include #include 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> 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; } }