UZipFile.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "UZipFile.h"
  2. #include <string>
  3. #include <codecvt>
  4. UZipFile::UZipFile(void)
  5. :m_nCurrentEntryNum(0),
  6. m_nEntryNum(0),
  7. m_bIsCurrentFileOpened(false),
  8. m_bIsZipOpened(false)
  9. {
  10. }
  11. UZipFile::~UZipFile(void)
  12. {
  13. CloseZip();
  14. }
  15. bool UZipFile::OpenZip()
  16. {
  17. uf = unzOpen2(NULL,(zlib_filefunc_def* )m_pzlib_filefunc_def);
  18. if (uf == NULL)
  19. {
  20. return false;
  21. }
  22. unz_global_info64 gi;
  23. if (unzGetGlobalInfo64(uf, &gi) != UNZ_OK)
  24. {
  25. return false;
  26. }
  27. m_nEntryNum = gi.number_entry;
  28. m_nCurrentEntryNum =0;
  29. m_bIsCurrentFileOpened =m_nEntryNum>0;
  30. return true;
  31. }
  32. std::string ws2s(const std::wstring& ws)
  33. {
  34. std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
  35. setlocale(LC_ALL, "chs");
  36. const wchar_t* _Source = ws.c_str();
  37. size_t _Dsize = 2 * ws.size() + 1;
  38. char *_Dest = new char[_Dsize];
  39. memset(_Dest,0,_Dsize);
  40. wcstombs(_Dest,_Source,_Dsize);
  41. std::string result = _Dest;
  42. delete []_Dest;
  43. setlocale(LC_ALL, curLocale.c_str());
  44. return result;
  45. }
  46. bool UZipFile::GetNextEntry( std::string & strFileName )
  47. {
  48. if(!HasMoreEntry()) return false;
  49. m_nCurrentEntryNum++;
  50. if(m_nCurrentEntryNum>1&&m_bIsCurrentFileOpened){
  51. unzCloseCurrentFile(uf);
  52. }
  53. if (m_nCurrentEntryNum>1&&unzGoToNextFile(uf) != UNZ_OK)
  54. {
  55. return false;
  56. }
  57. char szFilePathA[1024];
  58. unz_file_info64 FileInfo;
  59. if (unzGetCurrentFileInfo64(uf, &FileInfo, szFilePathA, sizeof(szFilePathA), NULL, 0, NULL, 0) != UNZ_OK)
  60. {
  61. return false;
  62. }
  63. #define ZIP_GPBF_LANGUAGE_ENCODING_FLAG 0x800
  64. if ((FileInfo.flag & ZIP_GPBF_LANGUAGE_ENCODING_FLAG) != 0)
  65. {
  66. std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
  67. std::wstring ws=conv.from_bytes(szFilePathA);
  68. strFileName = ws2s(ws);
  69. }
  70. else
  71. {
  72. strFileName =(szFilePathA);
  73. }
  74. if (unzOpenCurrentFile(uf) != UNZ_OK)
  75. {
  76. return false;
  77. }
  78. m_bIsCurrentFileOpened =true;
  79. return true;
  80. }
  81. int UZipFile::Read( void * buffer,int bufferSize )
  82. {
  83. return unzReadCurrentFile(uf, buffer, bufferSize);
  84. }
  85. void UZipFile::CloseZip()
  86. {
  87. if(m_bIsCurrentFileOpened){
  88. unzCloseCurrentFile(uf);
  89. m_bIsCurrentFileOpened =false;
  90. }
  91. if(m_bIsZipOpened){
  92. unzClose(uf);
  93. m_bIsZipOpened =false;
  94. }
  95. }