Compressor.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright (C) 2011 Michael McMaster <michael@codesrc.com>
  2. //
  3. // This file is part of libzipper.
  4. //
  5. // libzipper is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // libzipper is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with libzipper. If not, see <http://www.gnu.org/licenses/>.
  17. #include "zipper.hh"
  18. #include "gzip.hh"
  19. #include "zip.hh"
  20. #include "util.hh"
  21. #include <algorithm>
  22. using namespace zipper;
  23. class Compressor::CompressorImpl
  24. {
  25. public:
  26. virtual ~CompressorImpl() {}
  27. virtual void
  28. addFile(const std::string& filename, const Reader& reader) = 0;
  29. };
  30. namespace
  31. {
  32. class PlainCompressor : public Compressor::CompressorImpl
  33. {
  34. public:
  35. PlainCompressor(const WriterPtr& writer) : m_writer(writer) {}
  36. virtual void
  37. addFile(const std::string&, const Reader& reader)
  38. {
  39. enum Constants
  40. {
  41. ChunkSize = 64*1024
  42. };
  43. uint8_t buffer[ChunkSize];
  44. zsize_t offset(0);
  45. while (offset < reader.getSize())
  46. {
  47. zsize_t bytes(
  48. std::min(zsize_t(ChunkSize), reader.getSize() - offset));
  49. reader.readData(offset, bytes, &buffer[0]);
  50. m_writer->writeData(offset, bytes, &buffer[0]);
  51. offset += bytes;
  52. }
  53. }
  54. private:
  55. WriterPtr m_writer;
  56. };
  57. class ZipCompressor : public Compressor::CompressorImpl
  58. {
  59. public:
  60. ZipCompressor(const WriterPtr& writer) : m_writer(writer) {}
  61. virtual ~ZipCompressor()
  62. {
  63. zipFinalise(m_records, m_writer);
  64. }
  65. virtual void
  66. addFile(const std::string& filename, const Reader& reader)
  67. {
  68. ZipFileRecord record;
  69. zip(filename, reader, m_writer, record);
  70. m_records.push_back(record);
  71. }
  72. private:
  73. WriterPtr m_writer;
  74. std::vector<ZipFileRecord> m_records;
  75. };
  76. class GzipCompressor : public Compressor::CompressorImpl
  77. {
  78. public:
  79. GzipCompressor(const WriterPtr& writer) : m_writer(writer) {}
  80. virtual void
  81. addFile(const std::string& filename, const Reader& reader)
  82. {
  83. gzip(filename, reader, m_writer);
  84. }
  85. private:
  86. WriterPtr m_writer;
  87. };
  88. }
  89. Compressor::Compressor(ContainerFormat format, const WriterPtr& writer)
  90. {
  91. switch (format)
  92. {
  93. case Container_none:
  94. m_compressor = new PlainCompressor(writer); break;
  95. case Container_zip:
  96. m_compressor = new ZipCompressor(writer); break;
  97. case Container_gzip:
  98. m_compressor = new GzipCompressor(writer); break;
  99. default:
  100. throw UnsupportedException("Unknown format");
  101. }
  102. }
  103. Compressor::Compressor(ContainerFormat format, Writer& writer) :
  104. m_compressor(NULL)
  105. {
  106. WriterPtr ptr(&writer, dummy_delete<Writer>());
  107. switch (format)
  108. {
  109. case Container_none:
  110. m_compressor = new PlainCompressor(ptr); break;
  111. case Container_zip:
  112. m_compressor = new ZipCompressor(ptr); break;
  113. case Container_gzip:
  114. m_compressor = new GzipCompressor(ptr); break;
  115. default:
  116. throw UnsupportedException("Unknown format");
  117. }
  118. }
  119. Compressor::~Compressor()
  120. {
  121. delete m_compressor;
  122. }
  123. void
  124. Compressor::addFile(const Reader& reader)
  125. {
  126. m_compressor->addFile(reader.getSourceName(), reader);
  127. }