zipObject.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "io/zip/zipObject.h"
  23. #include "memory/safeDelete.h"
  24. #include "zipObject_ScriptBinding.h"
  25. //////////////////////////////////////////////////////////////////////////
  26. // Constructor/Destructor
  27. //////////////////////////////////////////////////////////////////////////
  28. ZipObject::ZipObject()
  29. {
  30. mZipArchive = NULL;
  31. }
  32. ZipObject::~ZipObject()
  33. {
  34. closeArchive();
  35. }
  36. IMPLEMENT_CONOBJECT(ZipObject);
  37. //////////////////////////////////////////////////////////////////////////
  38. // Protected Methods
  39. //////////////////////////////////////////////////////////////////////////
  40. StreamObject *ZipObject::createStreamObject(Stream *stream)
  41. {
  42. for(S32 i = 0;i < mStreamPool.size();++i)
  43. {
  44. StreamObject *so = mStreamPool[i];
  45. if(so == NULL)
  46. {
  47. // Reuse any free locations in the pool
  48. so = new StreamObject(stream);
  49. so->registerObject();
  50. mStreamPool[i] = so;
  51. return so;
  52. }
  53. if(so->getStream() == NULL)
  54. {
  55. // Existing unused stream, update it and return it
  56. so->setStream(stream);
  57. return so;
  58. }
  59. }
  60. // No free object found, create a new one
  61. StreamObject *so = new StreamObject(stream);
  62. so->registerObject();
  63. mStreamPool.push_back(so);
  64. return so;
  65. }
  66. //////////////////////////////////////////////////////////////////////////
  67. // Public Methods
  68. //////////////////////////////////////////////////////////////////////////
  69. bool ZipObject::openArchive(const char *filename, Zip::ZipArchive::AccessMode mode /* = Read */)
  70. {
  71. closeArchive();
  72. mZipArchive = new Zip::ZipArchive;
  73. if(mZipArchive->openArchive(filename, mode))
  74. return true;
  75. SAFE_DELETE(mZipArchive);
  76. return false;
  77. }
  78. void ZipObject::closeArchive()
  79. {
  80. if(mZipArchive == NULL)
  81. return;
  82. for(S32 i = 0;i < mStreamPool.size();++i)
  83. {
  84. StreamObject *so = mStreamPool[i];
  85. if(so && so->getStream() != NULL)
  86. closeFile(so);
  87. SAFE_DELETE_OBJECT(mStreamPool[i]);
  88. }
  89. mStreamPool.clear();
  90. mZipArchive->closeArchive();
  91. SAFE_DELETE(mZipArchive);
  92. }
  93. //////////////////////////////////////////////////////////////////////////
  94. StreamObject * ZipObject::openFileForRead(const char *filename)
  95. {
  96. if(mZipArchive == NULL)
  97. return NULL;
  98. Stream *stream;
  99. if((stream = mZipArchive->openFile(filename, Zip::ZipArchive::Read)))
  100. return createStreamObject(stream);
  101. return NULL;
  102. }
  103. StreamObject * ZipObject::openFileForWrite(const char *filename)
  104. {
  105. if(mZipArchive == NULL)
  106. return NULL;
  107. Stream *stream;
  108. if((stream = mZipArchive->openFile(filename, Zip::ZipArchive::Write)))
  109. return createStreamObject(stream);
  110. return NULL;
  111. }
  112. void ZipObject::closeFile(StreamObject *stream)
  113. {
  114. if(mZipArchive == NULL)
  115. return;
  116. #ifdef TORQUE_DEBUG
  117. bool found = false;
  118. for(S32 i = 0;i < mStreamPool.size();++i)
  119. {
  120. StreamObject *so = mStreamPool[i];
  121. if(so && so == stream)
  122. {
  123. found = true;
  124. break;
  125. }
  126. }
  127. AssertFatal(found, "ZipObject::closeFile() - Attempting to close stream not opened by this ZipObject");
  128. #endif
  129. mZipArchive->closeFile(stream->getStream());
  130. stream->setStream(NULL);
  131. }
  132. //////////////////////////////////////////////////////////////////////////
  133. bool ZipObject::addFile(const char *filename, const char *pathInZip, bool replace /* = true */)
  134. {
  135. return mZipArchive->addFile(filename, pathInZip, replace);
  136. }
  137. bool ZipObject::extractFile(const char *pathInZip, const char *filename)
  138. {
  139. return mZipArchive->extractFile(pathInZip, filename);
  140. }
  141. bool ZipObject::deleteFile(const char *filename)
  142. {
  143. return mZipArchive->deleteFile(filename);
  144. }
  145. //////////////////////////////////////////////////////////////////////////
  146. S32 ZipObject::getFileEntryCount()
  147. {
  148. return mZipArchive ? mZipArchive->numEntries() : 0;
  149. }
  150. const char *ZipObject::getFileEntry(S32 idx)
  151. {
  152. if(mZipArchive == NULL)
  153. return "";
  154. const Zip::CentralDir &dir = (*mZipArchive)[idx];
  155. char buffer[1024];
  156. dSprintf(buffer, sizeof(buffer), "%s\t%d\t%d\t%d\t%08x",
  157. dir.mFilename, dir.mUncompressedSize, dir.mCompressedSize,
  158. dir.mCompressMethod, dir.mCRC32);
  159. return StringTable->insert(buffer, true);
  160. }