juce_ZipFile.h 10 KB

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