daeZAEUncompressHandler.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright 2008 Netallied Systems GmbH.
  3. *
  4. * Licensed under the MIT Open Source License, for details please see license.txt or the website
  5. * http://www.opensource.org/licenses/mit-license.php
  6. *
  7. */
  8. #ifndef __DAE_ZAE_UNCOMPRESS_HANDLER_H__
  9. #define __DAE_ZAE_UNCOMPRESS_HANDLER_H__
  10. #include <unzip.h>
  11. #include <libxml/xmlreader.h>
  12. #include <dae/daeURI.h>
  13. /**
  14. * Takes an URI to a ZAE file and extracts it to a temporary directory.
  15. * Use obtainRootFilePath() to accomplish this.
  16. *
  17. * The whole ZAE archive gets extracted because it is not specified
  18. * how an URL pointing inside a ZAE archive should look like.
  19. * By extracting the whole archive we can use the 'file' scheme.
  20. */
  21. class DLLSPEC daeZAEUncompressHandler
  22. {
  23. private:
  24. // zip file this object operates on.
  25. unzFile mZipFile;
  26. // URI th zip file this object operates on.
  27. const daeURI& mZipFileURI;
  28. // indicates if the passed URI is a valid zip file.
  29. bool mValidZipFile;
  30. // path to root file in archive this object handles.
  31. std::string mRootFilePath;
  32. // tmp dir where this archive is extracted.
  33. std::string mTmpDir;
  34. // disable copy c-tor and assignment operator.
  35. daeZAEUncompressHandler(const daeZAEUncompressHandler& copy);
  36. daeZAEUncompressHandler& operator=(const daeZAEUncompressHandler& copy);
  37. public:
  38. // Name of manifest file inside ZAE.
  39. static const std::string MANIFEST_FILE_NAME;
  40. // Root xml element inside manifest file.
  41. static const std::string MANIFEST_FILE_ROOT_ELEMENT_NAME;
  42. // Case insensitivity constant from minizip.
  43. static const int CASE_INSENSITIVE;
  44. // Buffer size for extracting files from zip archive.
  45. static const int BUFFER_SIZE;
  46. // Empty string to be returned in case of error.
  47. static const std::string EMPTY_STRING;
  48. /**
  49. * C-Tor.
  50. * @param zaeFile URI to the ZAE file to open.
  51. */
  52. daeZAEUncompressHandler(const daeURI& zaeFile);
  53. /**
  54. * D-Tor.
  55. */
  56. virtual ~daeZAEUncompressHandler();
  57. /**
  58. * Returns true if this object has been initialized
  59. * with a zip file.
  60. */
  61. bool isZipFile() {return mValidZipFile;}
  62. /**
  63. * Extracts ZAE file and returns resulting path to the root DAE file.
  64. */
  65. const std::string& obtainRootFilePath();
  66. /**
  67. * Returns currently known path to root DAE of ZAE file.
  68. * Only valid after obtainRootFilePath() has been called.
  69. */
  70. const std::string& getRootFilePath() {return mRootFilePath;}
  71. /**
  72. * Returns used temp dir.
  73. */
  74. const std::string& getTmpDir() {return mTmpDir;}
  75. private:
  76. /**
  77. * Tries to open manifest.xml inside tmpDir. On success
  78. * it parses the XML file to find URI of root DAE.
  79. */
  80. bool retrieveRootURIFromManifest(const std::string& tmpDir);
  81. /**
  82. * Iterates over zip archive and extracts each file.
  83. */
  84. bool extractArchive(unzFile zipFile, const std::string& destDir);
  85. /**
  86. * Extracts the current file inside zip archive.
  87. */
  88. bool extractFile(unzFile zipFile, const std::string& destDir);
  89. /**
  90. * Finds <dae_root> element in manifest.xml. Used by retrieveRootURIFromManifest().
  91. */
  92. bool findManifestRootElement(xmlTextReaderPtr xmlReader);
  93. /**
  94. * Checks if an extracted file is a zip archive itself and extracts it.
  95. */
  96. bool checkAndExtractInternalArchive(const std::string& filePath);
  97. };
  98. #endif //__DAE_ZAE_UNCOMPRESS_HANDLER_H__