ZipFile.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include "ZipFile.h"
  2. #include "zlib/zip.h"
  3. CZipFile::CZipFile(void):m_IsZipOpened(false)
  4. {
  5. m_file_inzip_open =false;
  6. }
  7. CZipFile::~CZipFile(void)
  8. {
  9. if(m_IsZipOpened)CloseZip();
  10. }
  11. bool CZipFile::PutNextEntry( std::string lpszFileNameInZip, bool bUtf8 /*= false*/ )
  12. {
  13. #define ZIP_GPBF_LANGUAGE_ENCODING_FLAG 0x800
  14. if(m_file_inzip_open){
  15. zipCloseFileInZip(zf);
  16. m_file_inzip_open =false;
  17. }
  18. zip_fileinfo FileInfo;
  19. memset(&FileInfo, 0,sizeof(FileInfo));
  20. if (bUtf8)
  21. {
  22. return false;
  23. std::wstring wideStr ;
  24. std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
  25. std::string utf8 = conv.to_bytes(wideStr);
  26. if (zipOpenNewFileInZip4(zf, utf8.c_str(), &FileInfo, NULL, 0, NULL, 0, NULL, Z_DEFLATED, 9,
  27. 0, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, NULL, 0, 0, ZIP_GPBF_LANGUAGE_ENCODING_FLAG) != ZIP_OK)
  28. {
  29. return false;
  30. }
  31. }
  32. else
  33. {
  34. if (zipOpenNewFileInZip(zf, lpszFileNameInZip.c_str(), &FileInfo, NULL, 0, NULL, 0, NULL, Z_DEFLATED, 9) != ZIP_OK)
  35. {
  36. return false;
  37. }
  38. }
  39. m_file_inzip_open =true;
  40. return true;
  41. }
  42. void CZipFile::CloseZip()
  43. {
  44. if(m_file_inzip_open){
  45. zipCloseFileInZip(zf);
  46. m_file_inzip_open =false;
  47. }
  48. zipClose(zf,NULL);
  49. m_IsZipOpened =false;
  50. }
  51. bool CZipFile::Write( void * data,int dataLen )
  52. {
  53. return zipWriteInFileInZip(zf,data,dataLen)==ZIP_OK;
  54. }
  55. void CZipFile::OpenZip()
  56. {
  57. zf=zipOpen2(NULL,0,NULL,(zlib_filefunc_def*)m_pzlib_filefunc_def);
  58. m_IsZipOpened =true;
  59. }