zip.mli 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. (***********************************************************************)
  2. (* *)
  3. (* The CamlZip library *)
  4. (* *)
  5. (* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
  6. (* adapted to Extc lib by Caue Waneck *)
  7. (* *)
  8. (* Copyright 2001 Institut National de Recherche en Informatique et *)
  9. (* en Automatique. All rights reserved. This file is distributed *)
  10. (* under the terms of the GNU Lesser General Public License, with *)
  11. (* the special exception on linking described in file LICENSE. *)
  12. (* *)
  13. (***********************************************************************)
  14. (* $Id: zip.mli,v 1.7 2008/12/07 09:23:08 xleroy Exp $ *)
  15. (** Reading and writing ZIP archives
  16. This module provides functions for reading and writing ZIP archive
  17. files. ZIP archives package one or more compressed files into
  18. a single ``ZIP file'' along with information about the files,
  19. including file name, date and time of last modification, user-provided
  20. comments, and a checksum to verify the integrity of each entry.
  21. The entries of a ZIP file are not necessarily actual files, and can
  22. actually consist of arbitrary data.
  23. The ZIP file format used in this module is identical to that
  24. implemented by the popular [pkzip] archiver under Windows,
  25. and by the Info-ZIP [zip] and [unzip] commands under Unix and Windows.
  26. This format is also identical to the JAR file format used by Java. *)
  27. (** {6 Information on ZIP entries} *)
  28. type compression_method =
  29. Stored (** data is stored without compression *)
  30. | Deflated (** data is compressed with the ``deflate'' algorithm *)
  31. (** Indicate whether the data in the entry is compressed or not. *)
  32. type entry =
  33. { filename: string; (** file name for entry *)
  34. extra: string; (** extra information attached to entry *)
  35. comment: string; (** comment attached to entry *)
  36. methd: compression_method; (** compression method *)
  37. mtime: float; (** last modification time (seconds since epoch) *)
  38. crc: int32; (** cyclic redundancy check for data *)
  39. uncompressed_size: int; (** size of original data in bytes *)
  40. compressed_size: int; (** size of compressed data *)
  41. is_directory: bool; (** whether this entry represents a directory *)
  42. file_offset: int64 (** for internal use *)
  43. }
  44. (** Description of an entry in a ZIP file. *)
  45. (** {6 Reading from ZIP files} *)
  46. type in_file
  47. (** Abstract type representing a handle opened for reading from
  48. a ZIP file. *)
  49. val open_in: string -> in_file
  50. (** Open the ZIP file with the given filename. Return a
  51. handle opened for reading from this file. *)
  52. val entries: in_file -> entry list
  53. (** Return a list of all entries in the given ZIP file. *)
  54. val comment: in_file -> string
  55. (** Return the comment attached to the given ZIP file, or the
  56. empty string if none. *)
  57. val find_entry: in_file -> string -> entry
  58. (** [Zip.find_entry zf filename] returns the description of the
  59. entry having name [filename] in the ZIP file [zf].
  60. Raises [Not_found] if no such entry exists.
  61. The file name must match exactly; in particular, case is
  62. significant. File names must use [/] (slash) as the directory
  63. separator. The name of a directory must end with a trailing
  64. [/] (slash). *)
  65. val read_entry: in_file -> entry -> string
  66. (** [Zip.read_entry zf e] reads and uncompresses the data
  67. (file contents) associated with entry [e] of ZIP file [zf].
  68. The data is returned as a character string. *)
  69. val copy_entry_to_channel: in_file -> entry -> out_channel -> unit
  70. (** [Zip.copy_entry_to_channel zf e oc] reads and uncompresses
  71. the data associated with entry [e] of ZIP file [zf].
  72. It then writes this data to the output channel [oc]. *)
  73. val copy_entry_to_file: in_file -> entry -> string -> unit
  74. (** [Zip.copy_entry_to_file zf e destfile] reads and uncompresses
  75. the data associated with entry [e] of ZIP file [zf].
  76. It then writes this data to the file named [destfile].
  77. The file [destfile] is created if it does not exist,
  78. and overwritten otherwise. The last modification date of
  79. the file is set to that indicated in the ZIP entry [e],
  80. if possible. *)
  81. val close_in: in_file -> unit
  82. (** Close the given ZIP file handle. If the ZIP file handle was
  83. created by [open_in_channel], the underlying input channel
  84. is closed. *)
  85. (** {6 Writing to ZIP files} *)
  86. type out_file
  87. (** Abstract type representing a handle opened for writing to
  88. a ZIP file. *)
  89. val open_out: ?comment: string -> string -> out_file
  90. (** Create (or truncate to zero length) the ZIP file with
  91. the given filename. Return a handle opened for writing
  92. to this file. The optional argument [comment] is a
  93. comment string that is attached to the ZIP file as a whole
  94. (as opposed to the comments that can be attached to individual
  95. ZIP entries). *)
  96. val add_entry:
  97. string -> out_file ->
  98. ?extra: string -> ?comment: string -> ?level: int ->
  99. ?mtime: float -> string -> unit
  100. (** [Zip.add_entry data zf name] adds a new entry to the
  101. ZIP file [zf]. The data (file contents) associated with
  102. the entry is taken from the string [data]. It is compressed
  103. and written to the ZIP file [zf]. [name] is the file name
  104. stored along with this entry. Several optional arguments
  105. can be provided to control the format and attached information
  106. of the entry:
  107. @param extra extra data attached to the entry (a string).
  108. Default: empty.
  109. @param comment attached to the entry (a string).
  110. Default: empty.
  111. @param level compression level for the entry. This is an
  112. integer between 0 and 9, with 0 meaning no compression (store
  113. as is), 1 lowest compression, 9 highest compression. Higher
  114. levels result in smaller compressed data, but longer
  115. compression times.
  116. Default: 6 (moderate compression).
  117. @param mtime last modification time (in seconds since the
  118. epoch).
  119. Default: the current time. *)
  120. val copy_channel_to_entry:
  121. in_channel -> out_file ->
  122. ?extra: string -> ?comment: string -> ?level: int ->
  123. ?mtime: float -> string -> unit
  124. (** Same as [Zip.add_entry], but the data associated with the
  125. entry is read from the input channel given as first argument.
  126. The channel is read up to end of file. *)
  127. val copy_file_to_entry:
  128. string -> out_file ->
  129. ?extra: string -> ?comment: string -> ?level: int ->
  130. ?mtime: float -> string -> unit
  131. (** Same as [Zip.add_entry], but the data associated with the
  132. entry is read from the file whose name is given as first
  133. argument. Also, the default value for the [mtime]
  134. optional parameter is the time of last modification of the
  135. file. *)
  136. val add_entry_generator:
  137. out_file ->
  138. ?extra: string -> ?comment: string -> ?level: int ->
  139. ?mtime: float -> string -> (string -> int -> int -> unit) * (unit -> unit)
  140. (** [Zip.add_entry_generator zf name] returns a pair of functions
  141. [(add, finish)]. It adds a new entry to the
  142. ZIP file [zf]. The file name stored along with this entry
  143. is [name]. Initially, no data is stored in this entry.
  144. To store data in this entry, the program must repeatedly call
  145. the [add] function returned by [Zip.add_entry_generator].
  146. An invocation [add s ofs len] stores [len] characters of
  147. string [s] starting at offset [ofs] in the ZIP entry.
  148. When all the data forming the entry has been sent, the
  149. program must call the [finish] function returned by
  150. [Zip.add_entry_generator]. [finish] must be called exactly once.
  151. The optional arguments to [Zip.add_entry_generator]
  152. are as described in {!Zip.add_entry}. *)
  153. val close_out: out_file -> unit
  154. (** Finish writing the ZIP archive by adding the table of
  155. contents, and close it. *)
  156. (** {6 Error reporting} *)
  157. exception Error of string * string * string
  158. (** Exception raised when an ill-formed ZIP archive is encountered,
  159. or illegal parameters are given to the functions in this
  160. module. The exception is of the form
  161. [Error(ZIP_name, entry_name, message)] where [ZIP_name]
  162. is the name of the ZIP file, [entry_name] the name of
  163. the offending entry, and [message] an explanation of the
  164. error. *)