]> git.saurik.com Git - wxWidgets.git/blob - docs/doxygen/overviews/arc.h
another batch of topic overviews reviewing (letters e,f)
[wxWidgets.git] / docs / doxygen / overviews / arc.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: arc.h
3 // Purpose: topic overview
4 // Author: wxWidgets team
5 // RCS-ID: $Id$
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
8
9 /*!
10
11 @page overview_arc Archive formats such as zip
12
13 The archive classes handle archive formats such as zip, tar, rar and cab.
14 Currently #wxZip and #wxTar classes are included.
15
16 For each archive type, there are the following classes (using zip here
17 as an example):
18
19 @li wxZipInputStream: input stream
20 @li wxZipOutputStream: output stream
21 @li wxZipEntry: holds the meta-data for an entry (e.g. filename, timestamp, etc.)
22
23 There are also abstract wxArchive classes that can be used to write code
24 that can handle any of the archive types, see @ref overview_arc_generic.
25
26 Also see #wxFileSystem for a higher level interface that
27 can handle archive files in a generic way.
28
29 The classes are designed to handle archives on both seekable streams such
30 as disk files, or non-seekable streams such as pipes and sockets
31 (see @ref overview_arc_noseek).
32
33 See also #wxFileSystem.
34
35 @li @ref overview_arc_create
36 @li @ref overview_arc_extract
37 @li @ref overview_arc_modify
38 @li @ref overview_arc_byname
39 @li @ref overview_arc_generic
40 @li @ref overview_arc_noseek
41
42
43 <hr>
44
45
46 @section overview_arc_create Creating an archive
47
48 Call #PutNextEntry() to create each new entry in the archive, then write the entry's data.
49 Another call to PutNextEntry() closes the current entry and begins the next.
50 For example:
51
52 @code
53 wxFFileOutputStream out(_T("test.zip"));
54 wxZipOutputStream zip(out);
55 wxTextOutputStream txt(zip);
56 wxString sep(wxFileName::GetPathSeparator());
57
58 zip.PutNextEntry(_T("entry1.txt"));
59 txt _T("Some text for entry1.txt\n");
60
61 zip.PutNextEntry(_T("subdir") + sep + _T("entry2.txt"));
62 txt _T("Some text for subdir/entry2.txt\n");
63 @endcode
64
65 The name of each entry can be a full path, which makes it possible to
66 store entries in subdirectories.
67
68
69 @section overview_arc_extract Extracting an archive
70
71 #GetNextEntry() returns a pointer to entry object containing the meta-data for
72 the next entry in the archive (and gives away ownership).
73
74 Reading from the input stream then returns the entry's data.
75 Eof() becomes @true after an attempt has been made to read past the end of the entry's data.
76
77 When there are no more entries, GetNextEntry() returns @NULL and sets Eof().
78
79 @code
80 auto_ptr<wxZipEntry> entry;
81
82 wxFFileInputStream in(_T("test.zip"));
83 wxZipInputStream zip(in);
84
85 while (entry.reset(zip.GetNextEntry()), entry.get() != @NULL)
86 {
87 // access meta-data
88 wxString name = entry-GetName();
89 // read 'zip' to access the entry's data
90 }
91 @endcode
92
93
94
95 @section overview_arc_modify Modifying an archive
96
97 To modify an existing archive, write a new copy of the archive to a new file,
98 making any necessary changes along the way and transferring any unchanged
99 entries using #CopyEntry().
100
101 For archive types which compress entry data, CopyEntry() is likely to be
102 much more efficient than transferring the data using Read() and Write()
103 since it will copy them without decompressing and recompressing them.
104
105 In general modifications are not possible without rewriting the archive,
106 though it may be possible in some limited cases. Even then, rewriting the
107 archive is usually a better choice since a failure can be handled without
108 losing the whole archive. #wxTempFileOutputStream can be helpful to do this.
109
110 For example to delete all entries matching the pattern "*.txt":
111
112 @code
113 auto_ptr<wxFFileInputStream> in(new wxFFileInputStream(_T("test.zip")));
114 wxTempFileOutputStream out(_T("test.zip"));
115
116 wxZipInputStream inzip(*in);
117 wxZipOutputStream outzip(out);
118
119 auto_ptr<wxZipEntry> entry;
120
121 // transfer any meta-data for the archive as a whole (the zip comment
122 // in the case of zip)
123 outzip.CopyArchiveMetaData(inzip);
124
125 // call CopyEntry for each entry except those matching the pattern
126 while (entry.reset(inzip.GetNextEntry()), entry.get() != @NULL)
127 if (!entry-GetName().Matches(_T("*.txt")))
128 if (!outzip.CopyEntry(entry.release(), inzip))
129 break;
130
131 // close the input stream by releasing the pointer to it, do this
132 // before closing the output stream so that the file can be replaced
133 in.reset();
134
135 // you can check for success as follows
136 bool success = inzip.Eof() && outzip.Close() && out.Commit();
137 @endcode
138
139
140
141 @section overview_arc_byname Looking up an archive entry by name
142
143 Also see #wxFileSystem for a higher level interface that is
144 more convenient for accessing archive entries by name.
145
146 To open just one entry in an archive, the most efficient way is
147 to simply search for it linearly by calling #GetNextEntry() until the
148 required entry is found. This works both for archives on seekable and
149 non-seekable streams.
150
151 The format of filenames in the archive is likely to be different
152 from the local filename format. For example zips and tars use
153 unix style names, with forward slashes as the path separator,
154 and absolute paths are not allowed. So if on Windows the file
155 "C:\MYDIR\MYFILE.TXT" is stored, then when reading the entry back #GetName()
156 will return "MYDIR\MYFILE.TXT". The conversion into the internal format
157 and back has lost some information.
158
159 So to avoid ambiguity when searching for an entry matching a local name,
160 it is better to convert the local name to the archive's internal format
161 and search for that:
162
163 @code
164 auto_ptr<wxZipEntry> entry;
165
166 // convert the local name we are looking for into the internal format
167 wxString name = wxZipEntry::GetInternalName(localname);
168
169 // open the zip
170 wxFFileInputStream in(_T("test.zip"));
171 wxZipInputStream zip(in);
172
173 // call GetNextEntry() until the required internal name is found
174 do {
175 entry.reset(zip.GetNextEntry());
176 }
177 while (entry.get() != @NULL && entry-GetInternalName() != name);
178
179 if (entry.get() != @NULL) {
180 // read the entry's data...
181 }
182 @endcode
183
184 To access several entries randomly, it is most efficient to transfer the
185 entire catalogue of entries to a container such as a std::map or a
186 #wxHashMap then entries looked up by name can be opened using the #OpenEntry() method.
187
188 @code
189 WX_DECLARE_STRING_HASH_MAP(wxZipEntry*, ZipCatalog);
190 ZipCatalog::iterator it;
191 wxZipEntry *entry;
192 ZipCatalog cat;
193
194 // open the zip
195 wxFFileInputStream in(_T("test.zip"));
196 wxZipInputStream zip(in);
197
198 // load the zip catalog
199 while ((entry = zip.GetNextEntry()) != @NULL) {
200 wxZipEntry*& current = cat[entry-GetInternalName()];
201 // some archive formats can have multiple entries with the same name
202 // (e.g. tar) though it is an error in the case of zip
203 delete current;
204 current = entry;
205 }
206
207 // open an entry by name
208 if ((it = cat.find(wxZipEntry::GetInternalName(localname))) != cat.end()) {
209 zip.OpenEntry(*it-second);
210 // ... now read entry's data
211 }
212 @endcode
213
214 To open more than one entry simultaneously you need more than one
215 underlying stream on the same archive:
216
217 @code
218 // opening another entry without closing the first requires another
219 // input stream for the same file
220 wxFFileInputStream in2(_T("test.zip"));
221 wxZipInputStream zip2(in2);
222 if ((it = cat.find(wxZipEntry::GetInternalName(local2))) != cat.end())
223 zip2.OpenEntry(*it-second);
224 @endcode
225
226
227
228 @section overview_arc_generic Generic archive programming
229
230 Also see #wxFileSystem for a higher level interface that
231 can handle archive files in a generic way.
232
233 The specific archive classes, such as the wxZip classes, inherit from
234 the following abstract classes which can be used to write code that can
235 handle any of the archive types:
236
237 @li wxArchiveInputStream: input stream
238 @li wxArchiveOutputStream: output stream
239 @li wxArchiveEntry: holds the meta-data for an entry (e.g. filename)
240
241 In order to able to write generic code it's necessary to be able to create
242 instances of the classes without knowing which archive type is being used.
243
244 To allow this there is a class factory for each archive type, derived from
245 #wxArchiveClassFactory, that can create the other classes.
246
247 For example, given @e wxArchiveClassFactory* factory, streams and
248 entries can be created like this:
249
250 @code
251 // create streams without knowing their type
252 auto_ptr<wxArchiveInputStream> inarc(factory-NewStream(in));
253 auto_ptr<wxArchiveOutputStream> outarc(factory-NewStream(out));
254
255 // create an empty entry object
256 auto_ptr<wxArchiveEntry> entry(factory-NewEntry());
257 @endcode
258
259 For the factory itself, the static member wxArchiveClassFactory::Find().
260 can be used to find a class factory that can handle a given file
261 extension or mime type. For example, given @e filename:
262
263 @code
264 const wxArchiveClassFactory *factory;
265 factory = wxArchiveClassFactory::Find(filename, wxSTREAM_FILEEXT);
266
267 if (factory)
268 stream = factory-NewStream(new wxFFileInputStream(filename));
269 @endcode
270
271 @e Find does not give away ownership of the returned pointer, so it
272 does not need to be deleted.
273
274 There are similar class factories for the filter streams that handle the
275 compression and decompression of a single stream, such as wxGzipInputStream.
276 These can be found using wxFilterClassFactory::Find().
277
278 For example, to list the contents of archive @e filename:
279
280 @code
281 auto_ptr<wxInputStream> in(new wxFFileInputStream(filename));
282
283 if (in-IsOk())
284 {
285 // look for a filter handler, e.g. for '.gz'
286 const wxFilterClassFactory *fcf;
287 fcf = wxFilterClassFactory::Find(filename, wxSTREAM_FILEEXT);
288 if (fcf) {
289 in.reset(fcf-NewStream(in.release()));
290 // pop the extension, so if it was '.tar.gz' it is now just '.tar'
291 filename = fcf-PopExtension(filename);
292 }
293
294 // look for a archive handler, e.g. for '.zip' or '.tar'
295 const wxArchiveClassFactory *acf;
296 acf = wxArchiveClassFactory::Find(filename, wxSTREAM_FILEEXT);
297 if (acf) {
298 auto_ptr<wxArchiveInputStream> arc(acf-NewStream(in.release()));
299 auto_ptr<wxArchiveEntry> entry;
300
301 // list the contents of the archive
302 while ((entry.reset(arc-GetNextEntry())), entry.get() != @NULL)
303 std::wcout entry-GetName().c_str() "\n";
304 }
305 else {
306 wxLogError(_T("can't handle '%s'"), filename.c_str());
307 }
308 }
309 @endcode
310
311
312
313 @section overview_arc_noseek Archives on non-seekable streams
314
315 In general, handling archives on non-seekable streams is done in the same
316 way as for seekable streams, with a few caveats.
317
318 The main limitation is that accessing entries randomly using #OpenEntry()
319 is not possible, the entries can only be accessed sequentially in the order
320 they are stored within the archive.
321
322 For each archive type, there will also be other limitations which will
323 depend on the order the entries' meta-data is stored within the archive.
324 These are not too difficult to deal with, and are outlined below.
325
326 @b PutNextEntry and the entry size
327 When writing archives, some archive formats store the entry size before
328 the entry's data (tar has this limitation, zip doesn't). In this case
329 the entry's size must be passed to #PutNextEntry() or an error occurs.
330
331 This is only an issue on non-seekable streams, since otherwise the archive
332 output stream can seek back and fix up the header once the size of the
333 entry is known.
334
335 For generic programming, one way to handle this is to supply the size
336 whenever it is known, and rely on the error message from the output
337 stream when the operation is not supported.
338
339 @b GetNextEntry and the weak reference mechanism
340 Some archive formats do not store all an entry's meta-data before the
341 entry's data (zip is an example). In this case, when reading from a
342 non-seekable stream, #GetNextEntry() can only return a partially populated
343 #wxArchiveEntry object - not all the fields are set.
344
345 The input stream then keeps a weak reference to the entry object and
346 updates it when more meta-data becomes available. A weak reference being
347 one that does not prevent you from deleting the wxArchiveEntry object - the
348 input stream only attempts to update it if it is still around.
349
350 The documentation for each archive entry type gives the details
351 of what meta-data becomes available and when. For generic programming,
352 when the worst case must be assumed, you can rely on all the fields
353 of wxArchiveEntry being fully populated when GetNextEntry() returns,
354 with the the following exceptions:
355
356 @li GetSize(): Guaranteed to be available after the entry has been read to #Eof(),
357 or #CloseEntry() has been called
358
359 @li IsReadOnly(): Guaranteed to be available after the end of the archive has been
360 reached, i.e. after GetNextEntry() returns @NULL and Eof() is @true
361
362 This mechanism allows #CopyEntry() to always fully preserve entries' meta-data.
363 No matter what order order the meta-data occurs within the archive, the input stream
364 will always have read it before the output stream must write it.
365
366 @b wxArchiveNotifier
367 Notifier objects can be used to get a notification whenever an input
368 stream updates a #wxArchiveEntry object's data
369 via the weak reference mechanism.
370
371 Consider the following code which renames an entry in an archive.
372 This is the usual way to modify an entry's meta-data, simply set the
373 required field before writing it with #CopyEntry():
374
375 @code
376 auto_ptr<wxArchiveInputStream> arc(factory-NewStream(in));
377 auto_ptr<wxArchiveOutputStream> outarc(factory-NewStream(out));
378 auto_ptr<wxArchiveEntry> entry;
379
380 outarc-CopyArchiveMetaData(*arc);
381
382 while (entry.reset(arc-GetNextEntry()), entry.get() != @NULL) {
383 if (entry-GetName() == from)
384 entry-SetName(to);
385 if (!outarc-CopyEntry(entry.release(), *arc))
386 break;
387 }
388
389 bool success = arc-Eof() && outarc-Close();
390 @endcode
391
392 However, for non-seekable streams, this technique cannot be used for
393 fields such as #IsReadOnly(), which are not necessarily set when
394 #GetNextEntry() returns. In this case a #wxArchiveNotifier can be used:
395
396 @code
397 class MyNotifier : public wxArchiveNotifier
398 {
399 public:
400 void OnEntryUpdated(wxArchiveEntry& entry) { entry.SetIsReadOnly(@false); }
401 };
402 @endcode
403
404 The meta-data changes are done in your notifier's #OnEntryUpdated() method,
405 then #SetNotifier() is called before CopyEntry():
406
407 @code
408 auto_ptr<wxArchiveInputStream> arc(factory-NewStream(in));
409 auto_ptr<wxArchiveOutputStream> outarc(factory-NewStream(out));
410 auto_ptr<wxArchiveEntry> entry;
411 MyNotifier notifier;
412
413 outarc-CopyArchiveMetaData(*arc);
414
415 while (entry.reset(arc-GetNextEntry()), entry.get() != @NULL) {
416 entry-SetNotifier(notifier);
417 if (!outarc-CopyEntry(entry.release(), *arc))
418 break;
419 }
420
421 bool success = arc-Eof() && outarc-Close();
422 @endcode
423
424 SetNotifier() calls OnEntryUpdated() immediately, then the input
425 stream calls it again whenever it sets more fields in the entry. Since
426 OnEntryUpdated() will be called at least once, this technique always
427 works even when it is not strictly necessary to use it. For example,
428 changing the entry name can be done this way too and it works on seekable
429 streams as well as non-seekable.
430
431 */
432