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.

250 lines
9.3KB

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