juce_ZipFile.h 11 KB

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