CZipSaver.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * CZipSaver.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CZipSaver.h"
  12. ///CZipOutputStream
  13. CZipOutputStream::CZipOutputStream(zipFile archive, const std::string & archiveFilename):
  14. handle(archive)
  15. {
  16. //zip_fileinfo fileInfo;
  17. zipOpenNewFileInZip(handle,
  18. archiveFilename.c_str(),
  19. nullptr,//todo: use fileInfo,
  20. nullptr,
  21. 0,
  22. nullptr,
  23. 0,
  24. "",
  25. Z_DEFLATED,
  26. Z_DEFAULT_COMPRESSION);
  27. }
  28. CZipOutputStream::~CZipOutputStream()
  29. {
  30. zipCloseFileInZip(handle);
  31. }
  32. si64 CZipOutputStream::write(const ui8 * data, si64 size)
  33. {
  34. int ret = zipWriteInFileInZip(handle, (const void*)data, (unsigned)size);
  35. if (ret == ZIP_OK)
  36. return size;
  37. else
  38. return 0;
  39. }
  40. ///CZipSaver
  41. CZipSaver::CZipSaver(std::shared_ptr<CIOApi> api, const std::string & path):
  42. ioApi(api),
  43. zipApi(ioApi->getApiStructure()),
  44. handle(nullptr)
  45. {
  46. handle = zipOpen2_64(path.c_str(), APPEND_STATUS_CREATE, nullptr, &zipApi);
  47. if (handle == nullptr)
  48. throw new std::runtime_error("Failed to create archive");
  49. }
  50. CZipSaver::~CZipSaver()
  51. {
  52. if(handle != nullptr)
  53. zipClose(handle, nullptr);
  54. }
  55. std::unique_ptr<COutputStream> CZipSaver::addFile(const std::string & archiveFilename)
  56. {
  57. if(activeStream != nullptr)
  58. throw new std::runtime_error("CZipSaver::addFile: stream already opened");
  59. std::unique_ptr<COutputStream> stream(new CZipOutputStream(handle, archiveFilename));
  60. activeStream = stream.get();
  61. return stream;
  62. }