juce_ZipFile.h 11 KB

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