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.

257 lines
10KB

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