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.

265 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. #pragma once
  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 to read a specific 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. int64 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 a nullptr 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. void sortEntriesByFilename();
  90. //==============================================================================
  91. /** Creates a stream that can read from one of the zip file's entries.
  92. The stream that is returned must be deleted by the caller (and
  93. a nullptr might be returned if a stream can't be opened for some reason).
  94. The stream must not be used after the ZipFile object that created
  95. has been deleted.
  96. Note that if the ZipFile was created with a user-supplied InputStream object,
  97. then all the streams which are created by this method will by trying to share
  98. the same source stream, so cannot be safely used on multiple threads! (But if
  99. you create the ZipFile from a File or InputSource, then it is safe to do this).
  100. */
  101. InputStream* createStreamForEntry (int index);
  102. /** Creates a stream that can read from one of the zip file's entries.
  103. The stream that is returned must be deleted by the caller (and
  104. a nullptr might be returned if a stream can't be opened for some reason).
  105. The stream must not be used after the ZipFile object that created
  106. has been deleted.
  107. Note that if the ZipFile was created with a user-supplied InputStream object,
  108. then all the streams which are created by this method will by trying to share
  109. the same source stream, so cannot be safely used on multiple threads! (But if
  110. you create the ZipFile from a File or InputSource, then it is safe to do this).
  111. */
  112. InputStream* createStreamForEntry (const ZipEntry& entry);
  113. //==============================================================================
  114. /** Uncompresses all of the files in the zip file.
  115. This will expand all the entries into a target directory. The relative
  116. paths of the entries are used.
  117. @param targetDirectory the root folder to uncompress to
  118. @param shouldOverwriteFiles whether to overwrite existing files with similarly-named ones
  119. @returns success if the file is successfully unzipped
  120. */
  121. Result uncompressTo (const File& targetDirectory,
  122. bool shouldOverwriteFiles = true);
  123. /** Uncompresses one of the entries from the zip file.
  124. This will expand the entry and write it in a target directory. The entry's path is used to
  125. determine which subfolder of the target should contain the new file.
  126. @param index the index of the entry to uncompress - this must be a valid index
  127. between 0 and (getNumEntries() - 1).
  128. @param targetDirectory the root folder to uncompress into
  129. @param shouldOverwriteFiles whether to overwrite existing files with similarly-named ones
  130. @returns success if all the files are successfully unzipped
  131. */
  132. Result uncompressEntry (int index,
  133. const File& targetDirectory,
  134. bool shouldOverwriteFiles = true);
  135. //==============================================================================
  136. /** Used to create a new zip file.
  137. Create a ZipFile::Builder object, and call its addFile() method to add some files,
  138. then you can write it to a stream with write().
  139. */
  140. class JUCE_API Builder
  141. {
  142. public:
  143. /** Creates an empty builder object. */
  144. Builder();
  145. /** Destructor. */
  146. ~Builder();
  147. /** Adds a file to the list of items which will be added to the archive.
  148. The file isn't read immediately: the files will be read later when the writeToStream()
  149. method is called.
  150. The compressionLevel can be between 0 (no compression), and 9 (maximum compression).
  151. If the storedPathName parameter is specified, you can customise the partial pathname that
  152. will be stored for this file.
  153. */
  154. void addFile (const File& fileToAdd, int compressionLevel,
  155. const String& storedPathName = String());
  156. /** Adds a stream to the list of items which will be added to the archive.
  157. @param streamToRead this stream isn't read immediately - a pointer to the stream is
  158. stored, then used later when the writeToStream() method is called, and
  159. deleted by the Builder object when no longer needed, so be very careful
  160. about its lifetime and the lifetime of any objects on which it depends!
  161. This must not be null.
  162. @param compressionLevel this can be between 0 (no compression), and 9 (maximum compression).
  163. @param storedPathName the partial pathname that will be stored for this file
  164. @param fileModificationTime the timestamp that will be stored as the last modification time
  165. of this entry
  166. */
  167. void addEntry (InputStream* streamToRead, int compressionLevel,
  168. const String& storedPathName, Time fileModificationTime);
  169. /** Generates the zip file, writing it to the specified stream.
  170. If the progress parameter is non-null, it will be updated with an approximate
  171. progress status between 0 and 1.0
  172. */
  173. bool writeToStream (OutputStream& target, double* progress) const;
  174. //==============================================================================
  175. private:
  176. class Item;
  177. friend struct ContainerDeletePolicy<Item>;
  178. OwnedArray<Item> items;
  179. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Builder)
  180. };
  181. private:
  182. //==============================================================================
  183. class ZipInputStream;
  184. class ZipEntryHolder;
  185. friend class ZipInputStream;
  186. friend class ZipEntryHolder;
  187. OwnedArray<ZipEntryHolder> entries;
  188. CriticalSection lock;
  189. InputStream* inputStream;
  190. ScopedPointer<InputStream> streamToDelete;
  191. ScopedPointer<InputSource> inputSource;
  192. #if JUCE_DEBUG
  193. struct OpenStreamCounter
  194. {
  195. OpenStreamCounter() : numOpenStreams (0) {}
  196. ~OpenStreamCounter();
  197. int numOpenStreams;
  198. };
  199. OpenStreamCounter streamCounter;
  200. #endif
  201. void init();
  202. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ZipFile)
  203. };