zipstrm.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: zipstrm.h
  3. // Purpose: interface of wxZipNotifier
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /// Compression Method, only 0 (store) and 8 (deflate) are supported here
  8. enum wxZipMethod
  9. {
  10. wxZIP_METHOD_STORE,
  11. wxZIP_METHOD_SHRINK,
  12. wxZIP_METHOD_REDUCE1,
  13. wxZIP_METHOD_REDUCE2,
  14. wxZIP_METHOD_REDUCE3,
  15. wxZIP_METHOD_REDUCE4,
  16. wxZIP_METHOD_IMPLODE,
  17. wxZIP_METHOD_TOKENIZE,
  18. wxZIP_METHOD_DEFLATE,
  19. wxZIP_METHOD_DEFLATE64,
  20. wxZIP_METHOD_BZIP2 = 12,
  21. wxZIP_METHOD_DEFAULT = 0xffff
  22. };
  23. /// Originating File-System.
  24. ///
  25. /// These are Pkware's values. Note that Info-zip disagree on some of them,
  26. /// most notably NTFS.
  27. enum wxZipSystem
  28. {
  29. wxZIP_SYSTEM_MSDOS,
  30. wxZIP_SYSTEM_AMIGA,
  31. wxZIP_SYSTEM_OPENVMS,
  32. wxZIP_SYSTEM_UNIX,
  33. wxZIP_SYSTEM_VM_CMS,
  34. wxZIP_SYSTEM_ATARI_ST,
  35. wxZIP_SYSTEM_OS2_HPFS,
  36. wxZIP_SYSTEM_MACINTOSH,
  37. wxZIP_SYSTEM_Z_SYSTEM,
  38. wxZIP_SYSTEM_CPM,
  39. wxZIP_SYSTEM_WINDOWS_NTFS,
  40. wxZIP_SYSTEM_MVS,
  41. wxZIP_SYSTEM_VSE,
  42. wxZIP_SYSTEM_ACORN_RISC,
  43. wxZIP_SYSTEM_VFAT,
  44. wxZIP_SYSTEM_ALTERNATE_MVS,
  45. wxZIP_SYSTEM_BEOS,
  46. wxZIP_SYSTEM_TANDEM,
  47. wxZIP_SYSTEM_OS_400
  48. };
  49. /// Dos/Win file attributes
  50. enum wxZipAttributes
  51. {
  52. wxZIP_A_RDONLY = 0x01,
  53. wxZIP_A_HIDDEN = 0x02,
  54. wxZIP_A_SYSTEM = 0x04,
  55. wxZIP_A_SUBDIR = 0x10,
  56. wxZIP_A_ARCH = 0x20,
  57. wxZIP_A_MASK = 0x37
  58. };
  59. /// Values for the flags field in the zip headers
  60. enum wxZipFlags
  61. {
  62. wxZIP_ENCRYPTED = 0x0001,
  63. wxZIP_DEFLATE_NORMAL = 0x0000, // normal compression
  64. wxZIP_DEFLATE_EXTRA = 0x0002, // extra compression
  65. wxZIP_DEFLATE_FAST = 0x0004, // fast compression
  66. wxZIP_DEFLATE_SUPERFAST = 0x0006, // superfast compression
  67. wxZIP_DEFLATE_MASK = 0x0006,
  68. wxZIP_SUMS_FOLLOW = 0x0008, // crc and sizes come after the data
  69. wxZIP_ENHANCED = 0x0010,
  70. wxZIP_PATCH = 0x0020,
  71. wxZIP_STRONG_ENC = 0x0040,
  72. wxZIP_UNUSED = 0x0F80,
  73. wxZIP_RESERVED = 0xF000
  74. };
  75. /**
  76. @class wxZipNotifier
  77. If you need to know when a wxZipInputStream updates a wxZipEntry,
  78. you can create a notifier by deriving from this abstract base class,
  79. overriding wxZipNotifier::OnEntryUpdated().
  80. An instance of your notifier class can then be assigned to wxZipEntry
  81. objects, using wxZipEntry::SetNotifier().
  82. Setting a notifier is not usually necessary. It is used to handle
  83. certain cases when modifying an zip in a pipeline (i.e. between
  84. non-seekable streams).
  85. See @ref overview_archive_noseek.
  86. @library{wxbase}
  87. @category{archive,streams}
  88. @see @ref overview_archive_noseek, wxZipEntry, wxZipInputStream, wxZipOutputStream
  89. */
  90. class wxZipNotifier
  91. {
  92. public:
  93. /**
  94. Override this to receive notifications when an wxZipEntry object changes.
  95. */
  96. virtual void OnEntryUpdated(wxZipEntry& entry) = 0;
  97. };
  98. /**
  99. @class wxZipEntry
  100. Holds the meta-data for an entry in a zip.
  101. @section zipentry_avail Field availability
  102. When reading a zip from a stream that is seekable, wxZipEntry::GetNextEntry()
  103. returns a fully populated wxZipEntry object except for wxZipEntry::GetLocalExtra().
  104. wxZipEntry::GetLocalExtra() becomes available when the entry is opened, either by
  105. calling wxZipInputStream::OpenEntry() or by making an attempt to read the entry's data.
  106. For zips on non-seekable streams, the following fields are always available
  107. when wxZipEntry::GetNextEntry() returns:
  108. - wxZipEntry::GetDateTime
  109. - wxZipEntry::GetInternalFormat
  110. - wxZipEntry::GetInternalName
  111. - wxZipEntry::GetFlags
  112. - wxZipEntry::GetLocalExtra
  113. - wxZipEntry::GetMethod
  114. - wxZipEntry::GetName
  115. - wxZipEntry::GetOffset
  116. - wxZipEntry::IsDir
  117. The following fields are also usually available when GetNextEntry() returns,
  118. however, if the zip was also written to a non-seekable stream the zipper is
  119. permitted to store them after the entry's data. In that case they become
  120. available when the entry's data has been read to Eof(), or CloseEntry()
  121. has been called. (GetFlags() & wxZIP_SUMS_FOLLOW) != 0 indicates that
  122. one or more of these come after the data:
  123. - wxZipEntry::GetCompressedSize
  124. - wxZipEntry::GetCrc
  125. - wxZipEntry::GetSize
  126. The following are stored at the end of the zip, and become available when the
  127. end of the zip has been reached, i.e. after GetNextEntry() returns @NULL
  128. and Eof() is true:
  129. - wxZipEntry::GetComment
  130. - wxZipEntry::GetExternalAttributes
  131. - wxZipEntry::GetExtra
  132. - wxZipEntry::GetMode
  133. - wxZipEntry::GetSystemMadeBy
  134. - wxZipEntry::IsReadOnly
  135. - wxZipEntry::IsMadeByUnix
  136. - wxZipEntry::IsText
  137. @library{wxbase}
  138. @category{archive,streams}
  139. @see @ref overview_archive, wxZipInputStream, wxZipOutputStream, wxZipNotifier
  140. */
  141. class wxZipEntry : public wxArchiveEntry
  142. {
  143. public:
  144. wxZipEntry(const wxString& name = wxEmptyString,
  145. const wxDateTime& dt = Now(),
  146. wxFileOffset size = wxInvalidOffset);
  147. /**
  148. Copy constructor.
  149. */
  150. wxZipEntry(const wxZipEntry& entry);
  151. /**
  152. Make a copy of this entry.
  153. */
  154. wxZipEntry* Clone() const;
  155. //@{
  156. /**
  157. Gets and sets the short comment for this entry.
  158. */
  159. wxString GetComment() const;
  160. void SetComment(const wxString& comment);
  161. //@}
  162. //@{
  163. /**
  164. The low 8 bits are always the DOS/Windows file attributes for this entry.
  165. The values of these attributes are given in the enumeration ::wxZipAttributes.
  166. The remaining bits can store platform specific permission bits or
  167. attributes, and their meaning depends on the value of SetSystemMadeBy().
  168. If IsMadeByUnix() is @true then the high 16 bits are unix mode bits.
  169. The following other accessors access these bits:
  170. - IsReadOnly() / SetIsReadOnly()
  171. - IsDir() / SetIsDir()
  172. - GetMode() / SetMode()
  173. */
  174. wxUint32 GetExternalAttributes() const;
  175. void SetExternalAttributes(wxUint32 attr);
  176. //@}
  177. //@{
  178. /**
  179. The extra field from the entry's central directory record.
  180. The extra field is used to store platform or application specific
  181. data. See Pkware's document 'appnote.txt' for information on its format.
  182. */
  183. const char* GetExtra() const;
  184. size_t GetExtraLen() const;
  185. void SetExtra(const char* extra, size_t len);
  186. //@}
  187. //@{
  188. /**
  189. The extra field from the entry's local record.
  190. The extra field is used to store platform or application specific
  191. data. See Pkware's document 'appnote.txt' for information on its format.
  192. */
  193. const char* GetLocalExtra() const;
  194. size_t GetLocalExtraLen() const;
  195. void SetLocalExtra(const char* extra, size_t len);
  196. //@}
  197. //@{
  198. /**
  199. The compression method.
  200. The enumeration ::wxZipMethod lists the possible values.
  201. The default constructor sets this to @c wxZIP_METHOD_DEFAULT,
  202. which allows wxZipOutputStream to choose the method when writing the entry.
  203. */
  204. int GetMethod() const;
  205. void SetMethod(int method);
  206. //@}
  207. //@{
  208. /**
  209. If IsMadeByUnix() is true then returns the unix permission bits stored
  210. in GetExternalAttributes(). Otherwise synthesises them from the DOS attributes.
  211. */
  212. int GetMode() const;
  213. /**
  214. Sets the DOS attributes in GetExternalAttributes() to be consistent with
  215. the @a mode given.
  216. If IsMadeByUnix() is @true then also stores @a mode in GetExternalAttributes().
  217. Note that the default constructor sets GetSystemMadeBy() to
  218. @c wxZIP_SYSTEM_MSDOS by default. So to be able to store unix
  219. permissions when creating zips, call SetSystemMadeBy(wxZIP_SYSTEM_UNIX).
  220. */
  221. void SetMode(int mode);
  222. //@}
  223. //@{
  224. /**
  225. The originating file-system.
  226. The default constructor sets this to @c wxZIP_SYSTEM_MSDOS.
  227. Set it to @c wxZIP_SYSTEM_UNIX in order to be able to store unix
  228. permissions using SetMode().
  229. */
  230. int GetSystemMadeBy() const;
  231. void SetSystemMadeBy(int system);
  232. //@}
  233. /**
  234. The compressed size of this entry in bytes.
  235. */
  236. wxFileOffset GetCompressedSize() const;
  237. /**
  238. CRC32 for this entry's data.
  239. */
  240. wxUint32 GetCrc() const;
  241. /**
  242. Returns a combination of the bits flags in the enumeration @c wxZipFlags.
  243. */
  244. int GetFlags() const;
  245. //@{
  246. /**
  247. A static member that translates a filename into the internal format used
  248. within the archive. If the third parameter is provided, the bool pointed
  249. to is set to indicate whether the name looks like a directory name
  250. (i.e. has a trailing path separator).
  251. @see @ref overview_archive_byname
  252. */
  253. wxString GetInternalName(const wxString& name,
  254. wxPathFormat format = wxPATH_NATIVE,
  255. bool* pIsDir = NULL);
  256. /**
  257. Returns the entry's filename in the internal format used within the archive.
  258. The name can include directory components, i.e. it can be a full path.
  259. The names of directory entries are returned without any trailing path separator.
  260. This gives a canonical name that can be used in comparisons.
  261. */
  262. wxString GetInternalName() const;
  263. //@}
  264. /**
  265. Returns @true if GetSystemMadeBy() is a flavour of unix.
  266. */
  267. bool IsMadeByUnix() const;
  268. //@{
  269. /**
  270. Indicates that this entry's data is text in an 8-bit encoding.
  271. */
  272. bool IsText() const;
  273. void SetIsText(bool isText = true);
  274. //@}
  275. //@{
  276. /**
  277. Sets the notifier (see wxZipNotifier) for this entry.
  278. Whenever the wxZipInputStream updates this entry, it will then invoke
  279. the associated notifier's wxZipNotifier::OnEntryUpdated() method.
  280. Setting a notifier is not usually necessary. It is used to handle
  281. certain cases when modifying an zip in a pipeline (i.e. between
  282. non-seekable streams).
  283. @see @ref overview_archive_noseek, wxZipNotifier
  284. */
  285. void SetNotifier(wxZipNotifier& notifier);
  286. void UnsetNotifier();
  287. //@}
  288. /**
  289. Assignment operator.
  290. */
  291. wxZipEntry& operator=(const wxZipEntry& entry);
  292. };
  293. /**
  294. @class wxZipInputStream
  295. Input stream for reading zip files.
  296. wxZipInputStream::GetNextEntry() returns a wxZipEntry object containing the
  297. meta-data for the next entry in the zip (and gives away ownership).
  298. Reading from the wxZipInputStream then returns the entry's data.
  299. Eof() becomes @true after an attempt has been made to read past the end of
  300. the entry's data.
  301. When there are no more entries, GetNextEntry() returns @NULL and sets Eof().
  302. Note that in general zip entries are not seekable, and
  303. wxZipInputStream::SeekI() always returns ::wxInvalidOffset.
  304. @library{wxbase}
  305. @category{archive,streams}
  306. @see @ref overview_archive, wxZipEntry, wxZipOutputStream
  307. */
  308. class wxZipInputStream : public wxArchiveInputStream
  309. {
  310. public:
  311. //@{
  312. /**
  313. Constructor. In a Unicode build the second parameter @a conv is used to
  314. translate the filename and comment fields into Unicode.
  315. It has no effect on the stream's data.
  316. If the parent stream is passed as a pointer then the new filter stream
  317. takes ownership of it. If it is passed by reference then it does not.
  318. */
  319. wxZipInputStream(wxInputStream& stream,
  320. wxMBConv& conv = wxConvLocal);
  321. wxZipInputStream(wxInputStream* stream,
  322. wxMBConv& conv = wxConvLocal);
  323. //@}
  324. /**
  325. @deprecated
  326. Compatibility constructor (requires WXWIN_COMPATIBILITY_2_6).
  327. When this constructor is used, an emulation of seeking is
  328. switched on for compatibility with previous versions. Note however,
  329. that it is deprecated.
  330. */
  331. wxZipInputStream(const wxString& archive,
  332. const wxString& file);
  333. /**
  334. Closes the current entry.
  335. On a non-seekable stream reads to the end of the current entry first.
  336. */
  337. bool CloseEntry();
  338. /**
  339. Returns the zip comment.
  340. This is stored at the end of the zip, therefore when reading a zip
  341. from a non-seekable stream, it returns the empty string until the end
  342. of the zip has been reached, i.e. when GetNextEntry() returns @NULL.
  343. */
  344. wxString GetComment();
  345. /**
  346. Closes the current entry if one is open, then reads the meta-data for
  347. the next entry and returns it in a wxZipEntry object, giving away ownership.
  348. The stream is then open and can be read.
  349. */
  350. wxZipEntry* GetNextEntry();
  351. /**
  352. For a zip on a seekable stream returns the total number of entries in
  353. the zip. For zips on non-seekable streams returns the number of entries
  354. returned so far by GetNextEntry().
  355. */
  356. int GetTotalEntries();
  357. /**
  358. Closes the current entry if one is open, then opens the entry specified
  359. by the @a entry object.
  360. @a entry should be from the same zip file, and the zip should
  361. be on a seekable stream.
  362. @see overview_archive_byname
  363. */
  364. bool OpenEntry(wxZipEntry& entry);
  365. };
  366. /**
  367. @class wxZipClassFactory
  368. Class factory for the zip archive format.
  369. See the base class for details.
  370. @library{wxbase}
  371. @category{archive,streams}
  372. @see @ref overview_archive,
  373. @ref overview_archive_generic,
  374. wxZipEntry, wxZipInputStream, wxZipOutputStream
  375. */
  376. class wxZipClassFactory : public wxArchiveClassFactory
  377. {
  378. public:
  379. };
  380. /**
  381. @class wxZipOutputStream
  382. Output stream for writing zip files.
  383. wxZipOutputStream::PutNextEntry() is used to create a new entry in the
  384. output zip, then the entry's data is written to the wxZipOutputStream.
  385. Another call to wxZipOutputStream::PutNextEntry() closes the current
  386. entry and begins the next.
  387. @library{wxbase}
  388. @category{archive,streams}
  389. @see @ref overview_archive, wxZipEntry, wxZipInputStream
  390. */
  391. class wxZipOutputStream : public wxArchiveOutputStream
  392. {
  393. public:
  394. //@{
  395. /**
  396. Constructor.
  397. @a level is the compression level to use.
  398. It can be a value between 0 and 9 or -1 to use the default value
  399. which currently is equivalent to 6.
  400. If the parent stream is passed as a pointer then the new filter stream
  401. takes ownership of it. If it is passed by reference then it does not.
  402. In a Unicode build the third parameter @a conv is used to translate
  403. the filename and comment fields to an 8-bit encoding.
  404. It has no effect on the stream's data.
  405. */
  406. wxZipOutputStream(wxOutputStream& stream, int level = -1,
  407. wxMBConv& conv = wxConvLocal);
  408. wxZipOutputStream(wxOutputStream* stream, int level = -1,
  409. wxMBConv& conv = wxConvLocal);
  410. //@}
  411. /**
  412. The destructor calls Close() to finish writing the zip if it has
  413. not been called already.
  414. */
  415. virtual ~wxZipOutputStream();
  416. /**
  417. Finishes writing the zip, returning @true if successful.
  418. Called by the destructor if not called explicitly.
  419. */
  420. bool Close();
  421. /**
  422. Close the current entry.
  423. It is called implicitly whenever another new entry is created with CopyEntry()
  424. or PutNextEntry(), or when the zip is closed.
  425. */
  426. bool CloseEntry();
  427. /**
  428. Transfers the zip comment from the wxZipInputStream
  429. to this output stream.
  430. */
  431. bool CopyArchiveMetaData(wxZipInputStream& inputStream);
  432. /**
  433. Takes ownership of @a entry and uses it to create a new entry
  434. in the zip. @a entry is then opened in @a inputStream and its contents
  435. copied to this stream.
  436. CopyEntry() is much more efficient than transferring the data using
  437. Read() and Write() since it will copy them without decompressing and
  438. recompressing them.
  439. For zips on seekable streams, @a entry must be from the same zip file
  440. as @a inputStream. For non-seekable streams, @a entry must also be the
  441. last thing read from @a inputStream.
  442. */
  443. bool CopyEntry(wxZipEntry* entry, wxZipInputStream& inputStream);
  444. //@{
  445. /**
  446. Set the compression level that will be used the next time an entry is
  447. created.
  448. It can be a value between 0 and 9 or -1 to use the default value
  449. which currently is equivalent to 6.
  450. */
  451. int GetLevel() const;
  452. void SetLevel(int level);
  453. //@}
  454. /**
  455. Create a new directory entry (see wxArchiveEntry::IsDir) with the given
  456. name and timestamp.
  457. PutNextEntry() can also be used to create directory entries, by supplying
  458. a name with a trailing path separator.
  459. */
  460. bool PutNextDirEntry(const wxString& name,
  461. const wxDateTime& dt = wxDateTime::Now());
  462. //@{
  463. /**
  464. Takes ownership of @a entry and uses it to create a new entry in the zip.
  465. */
  466. bool PutNextEntry(wxZipEntry* entry);
  467. /**
  468. Create a new entry with the given name, timestamp and size.
  469. */
  470. bool PutNextEntry(const wxString& name,
  471. const wxDateTime& dt = wxDateTime::Now(),
  472. wxFileOffset size = wxInvalidOffset);
  473. //@}
  474. /**
  475. Sets a comment for the zip as a whole.
  476. It is written at the end of the zip.
  477. */
  478. void SetComment(const wxString& comment);
  479. };