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.

268 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #ifndef JUCE_ZIPFILE_H_INCLUDED
  24. #define JUCE_ZIPFILE_H_INCLUDED
  25. //==============================================================================
  26. /**
  27. Decodes a ZIP file from a stream.
  28. This can enumerate the items in a ZIP file and can create suitable stream objects
  29. to read each one.
  30. */
  31. class JUCE_API ZipFile
  32. {
  33. public:
  34. /** Creates a ZipFile to read a specific file. */
  35. explicit ZipFile (const File& file);
  36. //==============================================================================
  37. /** Creates a ZipFile for a given stream.
  38. @param inputStream the stream to read from
  39. @param deleteStreamWhenDestroyed if set to true, the object passed-in
  40. will be deleted when this ZipFile object is deleted
  41. */
  42. ZipFile (InputStream* inputStream, bool deleteStreamWhenDestroyed);
  43. /** Creates a ZipFile for a given stream.
  44. The stream will not be owned or deleted by this class - if you want the ZipFile to
  45. manage the stream's lifetime, use the other constructor.
  46. */
  47. explicit ZipFile (InputStream& inputStream);
  48. /** Creates a ZipFile for an input source.
  49. The inputSource object will be owned by the zip file, which will delete
  50. it later when not needed.
  51. */
  52. explicit ZipFile (InputSource* inputSource);
  53. /** Destructor. */
  54. ~ZipFile();
  55. //==============================================================================
  56. /**
  57. Contains information about one of the entries in a ZipFile.
  58. @see ZipFile::getEntry
  59. */
  60. struct ZipEntry
  61. {
  62. /** The name of the file, which may also include a partial pathname. */
  63. String filename;
  64. /** The file's original size. */
  65. int64 uncompressedSize;
  66. /** The last time the file was modified. */
  67. Time fileTime;
  68. };
  69. //==============================================================================
  70. /** Returns the number of items in the zip file. */
  71. int getNumEntries() const noexcept;
  72. /** Returns a structure that describes one of the entries in the zip file.
  73. This may return a nullptr if the index is out of range.
  74. @see ZipFile::ZipEntry
  75. */
  76. const ZipEntry* getEntry (int index) const noexcept;
  77. /** Returns the index of the first entry with a given filename.
  78. This uses a case-sensitive comparison to look for a filename in the
  79. list of entries. It might return -1 if no match is found.
  80. @see ZipFile::ZipEntry
  81. */
  82. int getIndexOfFileName (const String& fileName) const noexcept;
  83. /** Returns a structure that describes one of the entries in the zip file.
  84. This uses a case-sensitive comparison to look for a filename in the
  85. list of entries. It might return 0 if no match is found.
  86. @see ZipFile::ZipEntry
  87. */
  88. const ZipEntry* getEntry (const String& fileName) const noexcept;
  89. /** Sorts the list of entries, based on the filename. */
  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. a nullptr 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. Note that if the ZipFile was created with a user-supplied InputStream object,
  98. then all the streams which are created by this method will by trying to share
  99. the same source stream, so cannot be safely used on multiple threads! (But if
  100. you create the ZipFile from a File or InputSource, then it is safe to do this).
  101. */
  102. InputStream* createStreamForEntry (int index);
  103. /** Creates a stream that can read from one of the zip file's entries.
  104. The stream that is returned must be deleted by the caller (and
  105. a nullptr might be returned if a stream can't be opened for some reason).
  106. The stream must not be used after the ZipFile object that created
  107. has been deleted.
  108. Note that if the ZipFile was created with a user-supplied InputStream object,
  109. then all the streams which are created by this method will by trying to share
  110. the same source stream, so cannot be safely used on multiple threads! (But if
  111. you create the ZipFile from a File or InputSource, then it is safe to do this).
  112. */
  113. InputStream* createStreamForEntry (const ZipEntry& entry);
  114. //==============================================================================
  115. /** Uncompresses all of the files in the zip file.
  116. This will expand all the entries into a target directory. The relative
  117. paths of the entries are used.
  118. @param targetDirectory the root folder to uncompress to
  119. @param shouldOverwriteFiles whether to overwrite existing files with similarly-named ones
  120. @returns success if the file is successfully unzipped
  121. */
  122. Result uncompressTo (const File& targetDirectory,
  123. bool shouldOverwriteFiles = true);
  124. /** Uncompresses one of the entries from the zip file.
  125. This will expand the entry and write it in a target directory. The entry's path is used to
  126. determine which subfolder of the target should contain the new file.
  127. @param index the index of the entry to uncompress - this must be a valid index
  128. between 0 and (getNumEntries() - 1).
  129. @param targetDirectory the root folder to uncompress into
  130. @param shouldOverwriteFiles whether to overwrite existing files with similarly-named ones
  131. @returns success if all the files are successfully unzipped
  132. */
  133. Result uncompressEntry (int index,
  134. const File& targetDirectory,
  135. bool shouldOverwriteFiles = true);
  136. //==============================================================================
  137. /** Used to create a new zip file.
  138. Create a ZipFile::Builder object, and call its addFile() method to add some files,
  139. then you can write it to a stream with write().
  140. */
  141. class JUCE_API Builder
  142. {
  143. public:
  144. /** Creates an empty builder object. */
  145. Builder();
  146. /** Destructor. */
  147. ~Builder();
  148. /** Adds a file to the list of items which will be added to the archive.
  149. The file isn't read immediately: the files will be read later when the writeToStream()
  150. method is called.
  151. The compressionLevel can be between 0 (no compression), and 9 (maximum compression).
  152. If the storedPathName parameter is specified, you can customise the partial pathname that
  153. will be stored for this file.
  154. */
  155. void addFile (const File& fileToAdd, int compressionLevel,
  156. const String& storedPathName = String());
  157. /** Adds a stream to the list of items which will be added to the archive.
  158. @param streamToRead this stream isn't read immediately - a pointer to the stream is
  159. stored, then used later when the writeToStream() method is called, and
  160. deleted by the Builder object when no longer needed, so be very careful
  161. about its lifetime and the lifetime of any objects on which it depends!
  162. This must not be null.
  163. @param compressionLevel this can be between 0 (no compression), and 9 (maximum compression).
  164. @param storedPathName the partial pathname that will be stored for this file
  165. @param fileModificationTime the timestamp that will be stored as the last modification time
  166. of this entry
  167. */
  168. void addEntry (InputStream* streamToRead, int compressionLevel,
  169. const String& storedPathName, Time fileModificationTime);
  170. /** Generates the zip file, writing it to the specified stream.
  171. If the progress parameter is non-null, it will be updated with an approximate
  172. progress status between 0 and 1.0
  173. */
  174. bool writeToStream (OutputStream& target, double* progress) const;
  175. //==============================================================================
  176. private:
  177. class Item;
  178. friend struct ContainerDeletePolicy<Item>;
  179. OwnedArray<Item> items;
  180. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Builder)
  181. };
  182. private:
  183. //==============================================================================
  184. class ZipInputStream;
  185. class ZipEntryHolder;
  186. friend class ZipInputStream;
  187. friend class ZipEntryHolder;
  188. OwnedArray<ZipEntryHolder> entries;
  189. CriticalSection lock;
  190. InputStream* inputStream;
  191. ScopedPointer<InputStream> streamToDelete;
  192. ScopedPointer<InputSource> inputSource;
  193. #if JUCE_DEBUG
  194. struct OpenStreamCounter
  195. {
  196. OpenStreamCounter() : numOpenStreams (0) {}
  197. ~OpenStreamCounter();
  198. int numOpenStreams;
  199. };
  200. OpenStreamCounter streamCounter;
  201. #endif
  202. void init();
  203. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ZipFile)
  204. };
  205. #endif // JUCE_ZIPFILE_H_INCLUDED