UnZipFile.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "StdAfx.h"
  2. #include "UnZipFile.h"
  3. #include <afx.h>
  4. #define ZIP_GPBF_LANGUAGE_ENCODING_FLAG 0x800
  5. CUnZipFile::CUnZipFile(void)
  6. {
  7. }
  8. CUnZipFile::CUnZipFile( LPCTSTR fileName )
  9. {
  10. CStringA strSourceFileA = CT2A(fileName);
  11. uf = unzOpen64(strSourceFileA);
  12. if (uf == NULL)
  13. {
  14. return ;
  15. }
  16. unz_global_info64 gi;
  17. if (unzGetGlobalInfo64(uf, &gi) != UNZ_OK)
  18. {
  19. return ;
  20. }
  21. m_nEntryNum = gi.number_entry;
  22. m_nCurrentEntryNum =0;
  23. m_bIsCurrentFileOpened =m_nEntryNum>0? TRUE:FALSE;
  24. }
  25. CUnZipFile::~CUnZipFile(void)
  26. {
  27. Close();
  28. }
  29. void CUnZipFile::Close( ){
  30. if(m_bIsCurrentFileOpened){
  31. unzCloseCurrentFile(uf);
  32. m_bIsCurrentFileOpened =FALSE;
  33. }
  34. if(m_bIsZipOpened){
  35. unzClose(uf);
  36. m_bIsZipOpened =FALSE;
  37. }
  38. }
  39. int CUnZipFile::Read( void * buffer,int bufferSize )
  40. {
  41. return unzReadCurrentFile(uf, buffer, bufferSize);
  42. }
  43. BOOL CUnZipFile::GetNextEntry( CString & strFileName )
  44. {
  45. if(!HasMoreEntry()) return FALSE;
  46. m_nCurrentEntryNum++;
  47. if(m_nCurrentEntryNum>1&&m_bIsCurrentFileOpened){
  48. unzCloseCurrentFile(uf);
  49. }
  50. if (m_nCurrentEntryNum>1&&unzGoToNextFile(uf) != UNZ_OK)
  51. {
  52. return FALSE;
  53. }
  54. char szFilePathA[MAX_PATH];
  55. unz_file_info64 FileInfo;
  56. if (unzGetCurrentFileInfo64(uf, &FileInfo, szFilePathA, sizeof(szFilePathA), NULL, 0, NULL, 0) != UNZ_OK)
  57. {
  58. return FALSE;
  59. }
  60. if ((FileInfo.flag & ZIP_GPBF_LANGUAGE_ENCODING_FLAG) != 0)
  61. {
  62. strFileName = CA2T(szFilePathA,CP_UTF8);
  63. }
  64. else
  65. {
  66. strFileName = CA2T(szFilePathA);
  67. }
  68. if (unzOpenCurrentFile(uf) != UNZ_OK)
  69. {
  70. return FALSE;
  71. }
  72. m_bIsCurrentFileOpened =TRUE;
  73. return TRUE;
  74. }
  75. BOOL CUnZipFile::HasMoreEntry()
  76. {
  77. return m_nCurrentEntryNum<m_nEntryNum;
  78. }