The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
9.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_ZIPFILE_JUCEHEADER__
  19. #define __JUCE_ZIPFILE_JUCEHEADER__
  20. #include "../files/juce_File.h"
  21. #include "../streams/juce_InputSource.h"
  22. #include "../threads/juce_CriticalSection.h"
  23. #include "../containers/juce_OwnedArray.h"
  24. //==============================================================================
  25. /**
  26. Decodes a ZIP file from a stream.
  27. This can enumerate the items in a ZIP file and can create suitable stream objects
  28. to read each one.
  29. */
  30. class JUCE_API ZipFile
  31. {
  32. public:
  33. /** Creates a ZipFile based for a file. */
  34. explicit ZipFile (const File& file);
  35. //==============================================================================
  36. /** Creates a ZipFile for a given stream.
  37. @param inputStream the stream to read from
  38. @param deleteStreamWhenDestroyed if set to true, the object passed-in
  39. will be deleted when this ZipFile object is deleted
  40. */
  41. ZipFile (InputStream* inputStream, bool deleteStreamWhenDestroyed);
  42. /** Creates a ZipFile for a given stream.
  43. The stream will not be owned or deleted by this class - if you want the ZipFile to
  44. manage the stream's lifetime, use the other constructor.
  45. */
  46. explicit ZipFile (InputStream& inputStream);
  47. /** Creates a ZipFile for an input source.
  48. The inputSource object will be owned by the zip file, which will delete
  49. it later when not needed.
  50. */
  51. explicit ZipFile (InputSource* inputSource);
  52. /** Destructor. */
  53. ~ZipFile();
  54. //==============================================================================
  55. /**
  56. Contains information about one of the entries in a ZipFile.
  57. @see ZipFile::getEntry
  58. */
  59. struct ZipEntry
  60. {
  61. /** The name of the file, which may also include a partial pathname. */
  62. String filename;
  63. /** The file's original size. */
  64. unsigned int uncompressedSize;
  65. /** The last time the file was modified. */
  66. Time fileTime;
  67. };
  68. //==============================================================================
  69. /** Returns the number of items in the zip file. */
  70. int getNumEntries() const noexcept;
  71. /** Returns a structure that describes one of the entries in the zip file.
  72. This may return zero if the index is out of range.
  73. @see ZipFile::ZipEntry
  74. */
  75. const ZipEntry* getEntry (int index) const noexcept;
  76. /** Returns the index of the first entry with a given filename.
  77. This uses a case-sensitive comparison to look for a filename in the
  78. list of entries. It might return -1 if no match is found.
  79. @see ZipFile::ZipEntry
  80. */
  81. int getIndexOfFileName (const String& fileName) const noexcept;
  82. /** Returns a structure that describes one of the entries in the zip file.
  83. This uses a case-sensitive comparison to look for a filename in the
  84. list of entries. It might return 0 if no match is found.
  85. @see ZipFile::ZipEntry
  86. */
  87. const ZipEntry* getEntry (const String& fileName) const noexcept;
  88. /** Sorts the list of entries, based on the filename.
  89. */
  90. void sortEntriesByFilename();
  91. //==============================================================================
  92. /** Creates a stream that can read from one of the zip file's entries.
  93. The stream that is returned must be deleted by the caller (and
  94. zero might be returned if a stream can't be opened for some reason).
  95. The stream must not be used after the ZipFile object that created
  96. has been deleted.
  97. */
  98. InputStream* createStreamForEntry (int index);
  99. /** Creates a stream that can read from one of the zip file's entries.
  100. The stream that is returned must be deleted by the caller (and
  101. zero might be returned if a stream can't be opened for some reason).
  102. The stream must not be used after the ZipFile object that created
  103. has been deleted.
  104. */
  105. InputStream* createStreamForEntry (ZipEntry& entry);
  106. //==============================================================================
  107. /** Uncompresses all of the files in the zip file.
  108. This will expand all the entries into a target directory. The relative
  109. paths of the entries are used.
  110. @param targetDirectory the root folder to uncompress to
  111. @param shouldOverwriteFiles whether to overwrite existing files with similarly-named ones
  112. @returns success if the file is successfully unzipped
  113. */
  114. Result uncompressTo (const File& targetDirectory,
  115. bool shouldOverwriteFiles = true);
  116. /** Uncompresses one of the entries from the zip file.
  117. This will expand the entry and write it in a target directory. The entry's path is used to
  118. determine which subfolder of the target should contain the new file.
  119. @param index the index of the entry to uncompress - this must be a valid index
  120. between 0 and (getNumEntries() - 1).
  121. @param targetDirectory the root folder to uncompress into
  122. @param shouldOverwriteFiles whether to overwrite existing files with similarly-named ones
  123. @returns success if all the files are successfully unzipped
  124. */
  125. Result uncompressEntry (int index,
  126. const File& targetDirectory,
  127. bool shouldOverwriteFiles = true);
  128. //==============================================================================
  129. /** Used to create a new zip file.
  130. Create a ZipFile::Builder object, and call its addFile() method to add some files,
  131. then you can write it to a stream with write().
  132. Currently this just stores the files with no compression.. That will be added
  133. soon!
  134. */
  135. class Builder
  136. {
  137. public:
  138. Builder();
  139. ~Builder();
  140. /** Adds a file while should be added to the archive.
  141. The file isn't read immediately, all the files will be read later when the writeToStream()
  142. method is called.
  143. The compressionLevel can be between 0 (no compression), and 9 (maximum compression).
  144. If the storedPathName parameter is specified, you can customise the partial pathname that
  145. will be stored for this file.
  146. */
  147. void addFile (const File& fileToAdd, int compressionLevel,
  148. const String& storedPathName = String::empty);
  149. /** Generates the zip file, writing it to the specified stream.
  150. If the progress parameter is non-null, it will be updated with an approximate
  151. progress status between 0 and 1.0
  152. */
  153. bool writeToStream (OutputStream& target, double* progress) const;
  154. //==============================================================================
  155. private:
  156. class Item;
  157. friend class OwnedArray<Item>;
  158. OwnedArray<Item> items;
  159. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Builder);
  160. };
  161. private:
  162. //==============================================================================
  163. class ZipInputStream;
  164. class ZipEntryHolder;
  165. friend class ZipInputStream;
  166. friend class ZipEntryHolder;
  167. OwnedArray <ZipEntryHolder> entries;
  168. CriticalSection lock;
  169. InputStream* inputStream;
  170. ScopedPointer <InputStream> streamToDelete;
  171. ScopedPointer <InputSource> inputSource;
  172. #if JUCE_DEBUG
  173. struct OpenStreamCounter
  174. {
  175. OpenStreamCounter() : numOpenStreams (0) {}
  176. ~OpenStreamCounter();
  177. int numOpenStreams;
  178. };
  179. OpenStreamCounter streamCounter;
  180. #endif
  181. void init();
  182. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ZipFile);
  183. };
  184. #endif // __JUCE_ZIPFILE_JUCEHEADER__