zstream.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. PARTIO SOFTWARE
  3. Copyright 2010 Disney Enterprises, Inc. All rights reserved
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. * Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. * The names "Disney", "Walt Disney Pictures", "Walt Disney Animation
  14. Studios" or the names of its contributors may NOT be used to
  15. endorse or promote products derived from this software without
  16. specific prior written permission from Walt Disney Pictures.
  17. Disclaimer: THIS SOFTWARE IS PROVIDED BY WALT DISNEY PICTURES AND
  18. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
  19. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
  20. FOR A PARTICULAR PURPOSE, NONINFRINGEMENT AND TITLE ARE DISCLAIMED.
  21. IN NO EVENT SHALL WALT DISNEY PICTURES, THE COPYRIGHT HOLDER OR
  22. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND BASED ON ANY
  26. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  29. */
  30. #pragma once
  31. #include <iostream>
  32. #include <memory>
  33. #include <unordered_map>
  34. #include <vector>
  35. #include <xlnt/xlnt_config.hpp>
  36. #include <xlnt/utils/path.hpp>
  37. //TODO: don't export these classes (some tests are using them for now)
  38. namespace xlnt {
  39. namespace detail {
  40. /// <summary>
  41. /// A structure representing the header that occurs before each compressed file in a ZIP
  42. /// archive and again at the end of the file with more information.
  43. /// </summary>
  44. struct XLNT_API zheader
  45. {
  46. std::uint16_t version = 20;
  47. std::uint16_t flags = 0;
  48. std::uint16_t compression_type = 8;
  49. std::uint16_t stamp_date = 0;
  50. std::uint16_t stamp_time = 0;
  51. std::uint32_t crc = 0;
  52. std::uint32_t compressed_size = 0;
  53. std::uint32_t uncompressed_size = 0;
  54. std::string filename;
  55. std::string comment;
  56. std::vector<std::uint8_t> extra;
  57. std::uint32_t header_offset = 0;
  58. };
  59. /// <summary>
  60. /// Writes a series of uncompressed binary file data as ostreams into another ostream
  61. /// according to the ZIP format.
  62. /// </summary>
  63. class XLNT_API ozstream
  64. {
  65. public:
  66. /// <summary>
  67. /// Construct a new zip_file_writer which writes a ZIP archive to the given stream.
  68. /// </summary>
  69. ozstream(std::ostream &stream);
  70. /// <summary>
  71. /// Destructor.
  72. /// </summary>
  73. virtual ~ozstream();
  74. /// <summary>
  75. /// Returns a pointer to a streambuf which compresses the data it receives.
  76. /// </summary>
  77. std::unique_ptr<std::streambuf> open(const path &file);
  78. private:
  79. std::vector<zheader> file_headers_;
  80. std::ostream &destination_stream_;
  81. };
  82. /// <summary>
  83. /// Reads an archive containing a number of files from an istream and allows them
  84. /// to be decompressed into an istream.
  85. /// </summary>
  86. class XLNT_API izstream
  87. {
  88. public:
  89. /// <summary>
  90. /// Construct a new zip_file_reader which reads a ZIP archive from the given stream.
  91. /// </summary>
  92. izstream(std::istream &stream);
  93. /// <summary>
  94. /// Destructor.
  95. /// </summary>
  96. virtual ~izstream();
  97. /// <summary>
  98. ///
  99. /// </summary>
  100. std::unique_ptr<std::streambuf> open(const path &file) const;
  101. /// <summary>
  102. ///
  103. /// </summary>
  104. std::string read(const path &file) const;
  105. /// <summary>
  106. ///
  107. /// </summary>
  108. std::vector<path> files() const;
  109. /// <summary>
  110. ///
  111. /// </summary>
  112. bool has_file(const path &filename) const;
  113. private:
  114. /// <summary>
  115. ///
  116. /// </summary>
  117. bool read_central_header();
  118. /// <summary>
  119. ///
  120. /// </summary>
  121. std::unordered_map<std::string, zheader> file_headers_;
  122. /// <summary>
  123. ///
  124. /// </summary>
  125. std::istream &source_stream_;
  126. };
  127. } // namespace detail
  128. } // namespace xlnt