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.

234 lines
8.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_ZIPFILE_JUCEHEADER__
  19. #define __JUCE_ZIPFILE_JUCEHEADER__
  20. #include "../files/juce_File.h"
  21. #include "../streams/juce_InputSource.h"
  22. #include "../threads/juce_CriticalSection.h"
  23. #include "../containers/juce_OwnedArray.h"
  24. //==============================================================================
  25. /**
  26. Decodes a ZIP file from a stream.
  27. This can enumerate the items in a ZIP file and can create suitable stream objects
  28. to read each one.
  29. */
  30. class JUCE_API ZipFile
  31. {
  32. public:
  33. //==============================================================================
  34. /** Creates a ZipFile for a given stream.
  35. @param inputStream the stream to read from
  36. @param deleteStreamWhenDestroyed if set to true, the object passed-in
  37. will be deleted when this ZipFile object is deleted
  38. */
  39. ZipFile (InputStream* inputStream, bool deleteStreamWhenDestroyed);
  40. /** Creates a ZipFile based for a file. */
  41. ZipFile (const File& file);
  42. /** Creates a ZipFile for an input source.
  43. The inputSource object will be owned by the zip file, which will delete
  44. it later when not needed.
  45. */
  46. ZipFile (InputSource* inputSource);
  47. /** Destructor. */
  48. ~ZipFile();
  49. //==============================================================================
  50. /**
  51. Contains information about one of the entries in a ZipFile.
  52. @see ZipFile::getEntry
  53. */
  54. struct ZipEntry
  55. {
  56. /** The name of the file, which may also include a partial pathname. */
  57. String filename;
  58. /** The file's original size. */
  59. unsigned int uncompressedSize;
  60. /** The last time the file was modified. */
  61. Time fileTime;
  62. };
  63. //==============================================================================
  64. /** Returns the number of items in the zip file. */
  65. int getNumEntries() const noexcept;
  66. /** Returns a structure that describes one of the entries in the zip file.
  67. This may return zero if the index is out of range.
  68. @see ZipFile::ZipEntry
  69. */
  70. const ZipEntry* getEntry (int index) const noexcept;
  71. /** Returns the index of the first entry with a given filename.
  72. This uses a case-sensitive comparison to look for a filename in the
  73. list of entries. It might return -1 if no match is found.
  74. @see ZipFile::ZipEntry
  75. */
  76. int getIndexOfFileName (const String& fileName) const noexcept;
  77. /** Returns a structure that describes one of the entries in the zip file.
  78. This uses a case-sensitive comparison to look for a filename in the
  79. list of entries. It might return 0 if no match is found.
  80. @see ZipFile::ZipEntry
  81. */
  82. const ZipEntry* getEntry (const String& fileName) const noexcept;
  83. /** Sorts the list of entries, based on the filename.
  84. */
  85. void sortEntriesByFilename();
  86. //==============================================================================
  87. /** Creates a stream that can read from one of the zip file's entries.
  88. The stream that is returned must be deleted by the caller (and
  89. zero might be returned if a stream can't be opened for some reason).
  90. The stream must not be used after the ZipFile object that created
  91. has been deleted.
  92. */
  93. InputStream* createStreamForEntry (int index);
  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. zero 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. */
  100. InputStream* createStreamForEntry (ZipEntry& entry);
  101. //==============================================================================
  102. /** Uncompresses all of the files in the zip file.
  103. This will expand all the entries into a target directory. The relative
  104. paths of the entries are used.
  105. @param targetDirectory the root folder to uncompress to
  106. @param shouldOverwriteFiles whether to overwrite existing files with similarly-named ones
  107. @returns success if the file is successfully unzipped
  108. */
  109. Result uncompressTo (const File& targetDirectory,
  110. bool shouldOverwriteFiles = true);
  111. /** Uncompresses one of the entries from the zip file.
  112. This will expand the entry and write it in a target directory. The entry's path is used to
  113. determine which subfolder of the target should contain the new file.
  114. @param index the index of the entry to uncompress - this must be a valid index
  115. between 0 and (getNumEntries() - 1).
  116. @param targetDirectory the root folder to uncompress into
  117. @param shouldOverwriteFiles whether to overwrite existing files with similarly-named ones
  118. @returns success if all the files are successfully unzipped
  119. */
  120. Result uncompressEntry (int index,
  121. const File& targetDirectory,
  122. bool shouldOverwriteFiles = true);
  123. //==============================================================================
  124. /** Used to create a new zip file.
  125. Create a ZipFile::Builder object, and call its addFile() method to add some files,
  126. then you can write it to a stream with write().
  127. Currently this just stores the files with no compression.. That will be added
  128. soon!
  129. */
  130. class Builder
  131. {
  132. public:
  133. Builder();
  134. ~Builder();
  135. /** Adds a file while should be added to the archive.
  136. The file isn't read immediately, all the files will be read later when the writeToStream()
  137. method is called.
  138. The compressionLevel can be between 0 (no compression), and 9 (maximum compression).
  139. If the storedPathName parameter is specified, you can customise the partial pathname that
  140. will be stored for this file.
  141. */
  142. void addFile (const File& fileToAdd, int compressionLevel,
  143. const String& storedPathName = String::empty);
  144. /** Generates the zip file, writing it to the specified stream. */
  145. bool writeToStream (OutputStream& target) const;
  146. //==============================================================================
  147. private:
  148. class Item;
  149. friend class OwnedArray<Item>;
  150. OwnedArray<Item> items;
  151. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Builder);
  152. };
  153. private:
  154. //==============================================================================
  155. class ZipInputStream;
  156. class ZipFilenameComparator;
  157. class ZipEntryInfo;
  158. friend class ZipInputStream;
  159. friend class ZipFilenameComparator;
  160. friend class ZipEntryInfo;
  161. OwnedArray <ZipEntryInfo> entries;
  162. CriticalSection lock;
  163. InputStream* inputStream;
  164. ScopedPointer <InputStream> streamToDelete;
  165. ScopedPointer <InputSource> inputSource;
  166. #if JUCE_DEBUG
  167. int numOpenStreams;
  168. #endif
  169. void init();
  170. int findEndOfZipEntryTable (InputStream& input, int& numEntries);
  171. static int compareElements (const ZipEntryInfo* first, const ZipEntryInfo* second);
  172. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ZipFile);
  173. };
  174. #endif // __JUCE_ZIPFILE_JUCEHEADER__