summaryrefslogtreecommitdiffstats
path: root/Doc/lib/libzipfile.tex
blob: 0b1d43d8965e67464efd86c71fe2820ef9249126 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
\section{\module{zipfile} ---
         Work with ZIP archives}

\modulesynopsis{Read and write ZIP-format archive files.}
\moduleauthor{James C. Ahlstrom}{jim@interet.com}
\sectionauthor{James C. Ahlstrom}{jim@interet.com}
% LaTeX markup by Fred L. Drake, Jr. <fdrake@acm.org>

The ZIP file format is a common archive and compression standard.
This module provides tools to create, read, write, append, and list a
ZIP file.

The available attributes of this module are:

\begin{excdesc}{error}
  The error raised for bad ZIP files.
\end{excdesc}

\begin{datadesc}{_debug}
  Level of printing, defaults to \code{1}.
\end{datadesc}

\begin{classdesc}{ZipFile}{...}
  The class for reading and writing ZIP files.  See
  ``\citetitle{ZipFile Objects}'' (section \ref{zipfile-objects}) for
  constructor details.
\end{classdesc}

\begin{funcdesc}{is_zipfile}{path} 
  Returns true if \var{path} is a valid ZIP file based on its magic
  number, otherwise returns false.  This module does not currently
  handle ZIP files which have appended comments.
\end{funcdesc}

\begin{funcdesc}{zip2date}{zdate}
  Return \code{(\var{year}, \var{month}, \var{day})} for a ZIP date
  code.
\end{funcdesc}

\begin{funcdesc}{zip2time}{ztime}
  Return \code{(\var{hour}, \var{minute}, \var{second})} for a ZIP
  time code.
\end{funcdesc}

\begin{funcdesc}{date2zip}{year, month, day}
  Return a ZIP date code. 
\end{funcdesc}

\begin{funcdesc}{time2zip}{hour, minute, second}
  Return a ZIP time code.
\end{funcdesc}

\begin{datadesc}{ZIP_STORED}
  The numeric constant (\code{0}) for an uncompressed archive member.
\end{datadesc}

\begin{datadesc}{ZIP_DEFLATED}
  The numeric constant for the usual ZIP compression method.  This
  requires the zlib module.  No other compression methods are
  currently supported.
\end{datadesc}


\begin{seealso}
  \seetext{XXX point to ZIP format definition}
  \seetext{XXX point to Info-ZIP home page; mention WiZ}
\end{seealso}


\subsection{ZipFile Objects \label{zipfile-objects}}

\begin{classdesc}{ZipFile}{filename\optional{, mode\optional{, compression}}} 
  Open a ZIP file named \var{filename}.  The \var{mode} parameter
  should be \code{'r'} to read an existing file, \code{'w'} to
  truncate and write a new file, or \code{'a'} to append to an
  existing file.  For \var{mode} is \code{'a'} and \var{filename}
  refers to an existing ZIP file, then additional files are added to
  it.  If \var{filename} does not refer to a ZIP file, then a new ZIP
  archive is appended to the file.  This is meant for adding a ZIP
  archive to another file, such as \file{python.exe}.  Using
\begin{verbatim}
cat myzip.zip >> python.exe
\end{verbatim}
  also works, and at least \program{WinZip} can read such files.
  \var{compression} is the ZIP compression method to use when writing
  the archive, and should be \constant{ZIP_STORED} or
  \constant{ZIP_DEFLATED}; unrecognized values will cause
  \exception{ValueError} to be raised.  The default is
  \constant{ZIP_STORED}. 
\end{classdesc}

XXX explain the "extra" string for the ZIP format

\begin{memberdesc}{TOC}
  A read-only dictionary whose keys are the names in the archive, and
  whose values are tuples as follows:

\begin{tableii}{c|l}{code}{Index}{Meaning}
  \lineii{0}{File data seek offset}
  \lineii{1}{ZIP file "extra" data as a string}
  \lineii{2}{ZIP file bit flags}
  \lineii{3}{ZIP file compression type}
  \lineii{4}{File modification time in DOS format}
  \lineii{5}{File modification date in DOS format}
  \lineii{6}{The CRC-32 of the uncompressed data}
  \lineii{7}{The compressed size of the file}
  \lineii{8}{The uncompressed size of the file}
\end{tableii}
\end{memberdesc}

The class ZipFile has these methods: 

\begin{methoddesc}{listdir}{}
  Return a list of names in the archive.  Equivalent to
  \code{\var{zipfile}.TOC.keys()}.
\end{methoddesc}

\begin{methoddesc}{printdir}{}
  Print a table of contents for the archive to stdout. 
\end{methoddesc}

\begin{methoddesc}{read}{name}
  Return the bytes of the file in the archive.  The archive must be
  open for read or append.
\end{methoddesc}

\begin{methoddesc}{writestr}{bytes, arcname, year, month, day, hour,
                             minute, second\optional{, extra}}
  Write the string \var{bytes} and the other data to the archive, and
  give the archive member the name \var{arcname}.  \var{extra} is the
  ZIP extra data string.  The archive must be opened with mode
  \code{'w'} or \code{'a'}.
\end{methoddesc}

\begin{methoddesc}{write}{filename, arcname\optional{, extra}}
  Write the file named \var{filename} to the archive, giving it the
  archive name \var{arcname}.  \var{extra} is the ZIP extra data
  string.  The archive must be open with mode \code{'w'} or
  \code{'a'}.
\end{methoddesc}

\begin{methoddesc}{writepy}{pathname\optional{, basename}}
  Search for files \file{*.py} and add the corresponding file to the
  archive.  The corresponding file is a \file{*.pyo} file if
  available, else a \file{*.pyc} file, compiling if necessary.  If the
  pathname is a file, the filename must end with \file{.py}, and just
  the (corresponding \file{*.py[oc]}) file is added at the top level
  (no path information).  If it is a directory, and the directory is
  not a package directory, then all the files \file{*.py[oc]} are
  added at the top level.  If the directory is a package directory,
  then all \file{*.py[oc]} are added under the package name as a file
  path, and if any subdirectories are package directories, all of
  these are added recursively.  \var{basename} is intended for
  internal use only.  The \method{writepy()} method makes archives
  with file names like this:

\begin{verbatim}
    string.pyc                                # Top level name 
    test/__init__.pyc                         # Package directory 
    test/testall.pyc                          # Module test.testall
    test/bogus/__init__.pyc                   # Subpackage directory 
    test/bogus/myfile.pyc                     # Submodule test.bogus.myfile
\end{verbatim}
\end{methoddesc}

\begin{methoddesc}{close}{}
  Close the archive file.  You must call \method{close()} before
  exiting your program or essential records will not be written. 
\end{methoddesc}