archive formats such as zipthe archive classes handle archive formats such as zip, tar, rar and cab. currently wxzip and wxtar classes are included. for each archive type, there are the following classes (using zip here as an example):
there are also abstract wxarchive classes that can be used to write code that can handle any of the archive types, see 'generic archive programming'. also see wxfilesystem for a higher level interface that can handle archive files in a generic way. the classes are designed to handle archives on both seekable streams such as disk files, or non-seekable streams such as pipes and sockets (see 'archives on non-seekable streams'). see also
creating an archive
creating an archivecall putnextentry() to create each new entry in the archive, then write the entry's data. another call to putnextentry() closes the current entry and begins the next. for example:
wxffileoutputstream out(_t("test.zip")); wxzipoutputstream zip(out); wxtextoutputstream txt(zip); wxstring sep(wxfilename::getpathseparator()); zip.putnextentry(_t("entry1.txt")); txt << _t("some text for entry1.txt\n"); zip.putnextentry(_t("subdir") + sep + _t("entry2.txt")); txt << _t("some text for subdir/entry2.txt\n");the name of each entry can be a full path, which makes it possible to store entries in subdirectories.
extracting an archivegetnextentry() returns a pointer to entry object containing the meta-data for the next entry in the archive (and gives away ownership). reading from the input stream then returns the entry's data. eof() becomes true after an attempt has been made to read past the end of the entry's data. when there are no more entries, getnextentry() returns null and sets eof().
auto_ptr<wxzipentry> entry; wxffileinputstream in(_t("test.zip")); wxzipinputstream zip(in); while (entry.reset(zip.getnextentry()), entry.get() != null) { // access meta-data wxstring name = entry->getname(); // read 'zip' to access the entry's data } modifying an archiveto modify an existing archive, write a new copy of the archive to a new file, making any necessary changes along the way and transferring any unchanged entries using copyentry(). for archive types which compress entry data, copyentry() is likely to be much more efficient than transferring the data using read() and write() since it will copy them without decompressing and recompressing them. in general modifications are not possible without rewriting the archive, though it may be possible in some limited cases. even then, rewriting the archive is usually a better choice since a failure can be handled without losing the whole archive. wxtempfileoutputstream can be helpful to do this. for example to delete all entries matching the pattern "*.txt":
auto_ptr<wxffileinputstream> in(new wxffileinputstream(_t("test.zip"))); wxtempfileoutputstream out(_t("test.zip")); wxzipinputstream inzip(*in); wxzipoutputstream outzip(out); auto_ptr<wxzipentry> entry; // transfer any meta-data for the archive as a whole (the zip comment // in the case of zip) outzip.copyarchivemetadata(inzip); // call copyentry for each entry except those matching the pattern while (entry.reset(inzip.getnextentry()), entry.get() != null) if (!entry->getname().matches(_t("*.txt"))) if (!outzip.copyentry(entry.release(), inzip)) break; // close the input stream by releasing the pointer to it, do this // before closing the output stream so that the file can be replaced in.reset(); // you can check for success as follows bool success = inzip.eof() && outzip.close() && out.commit(); looking up an archive entry by namealso see wxfilesystem for a higher level interface that is more convenient for accessing archive entries by name. to open just one entry in an archive, the most efficient way is to simply search for it linearly by calling getnextentry() until the required entry is found. this works both for archives on seekable and non-seekable streams. the format of filenames in the archive is likely to be different from the local filename format. for example zips and tars use unix style names, with forward slashes as the path separator, and absolute paths are not allowed. so if on windows the file "c:\mydir\myfile.txt" is stored, then when reading the entry back getname() will return "mydir\myfile.txt". the conversion into the internal format and back has lost some information. so to avoid ambiguity when searching for an entry matching a local name, it is better to convert the local name to the archive's internal format and search for that:
auto_ptr<wxzipentry> entry; // convert the local name we are looking for into the internal format wxstring name = wxzipentry::getinternalname(localname); // open the zip wxffileinputstream in(_t("test.zip")); wxzipinputstream zip(in); // call getnextentry() until the required internal name is found do { entry.reset(zip.getnextentry()); } while (entry.get() != null && entry->getinternalname() != name); if (entry.get() != null) { // read the entry's data... }to access several entries randomly, it is most efficient to transfer the entire catalogue of entries to a container such as a std::map or a wxhashmap then entries looked up by name can be opened using the openentry() method.
wx_declare_string_hash_map(wxzipentry*, zipcatalog); zipcatalog::iterator it; wxzipentry *entry; zipcatalog cat; // open the zip wxffileinputstream in(_t("test.zip")); wxzipinputstream zip(in); // load the zip catalog while ((entry = zip.getnextentry()) != null) { wxzipentry*& current = cat[entry->getinternalname()]; // some archive formats can have multiple entries with the same name // (e.g. tar) though it is an error in the case of zip delete current; current = entry; } // open an entry by name if ((it = cat.find(wxzipentry::getinternalname(localname))) != cat.end()) { zip.openentry(*it->second); // ... now read entry's data }to open more than one entry simultaneously you need more than one underlying stream on the same archive:
// opening another entry without closing the first requires another // input stream for the same file wxffileinputstream in2(_t("test.zip")); wxzipinputstream zip2(in2); if ((it = cat.find(wxzipentry::getinternalname(local2))) != cat.end()) zip2.openentry(*it->second); generic archive programmingalso see wxfilesystem for a higher level interface that can handle archive files in a generic way. the specific archive classes, such as the wxzip classes, inherit from the following abstract classes which can be used to write code that can handle any of the archive types:
in order to able to write generic code it's necessary to be able to create instances of the classes without knowing which archive type is being used. to allow this there is a class factory for each archive type, derived from wxarchiveclassfactory, that can create the other classes. for example, given wxarchiveclassfactory* factory, streams and entries can be created like this:
// create streams without knowing their type auto_ptr<wxarchiveinputstream> inarc(factory->newstream(in)); auto_ptr<wxarchiveoutputstream> outarc(factory->newstream(out)); // create an empty entry object auto_ptr<wxarchiveentry> entry(factory->newentry());for the factory itself, the static member wxarchiveclassfactory::find(). can be used to find a class factory that can handle a given file extension or mime type. for example, given filename:
const wxarchiveclassfactory *factory; factory = wxarchiveclassfactory::find(filename, wxstream_fileext); if (factory) stream = factory->newstream(new wxffileinputstream(filename));find does not give away ownership of the returned pointer, so it does not need to be deleted. there are similar class factories for the filter streams that handle the compression and decompression of a single stream, such as wxgzipinputstream. these can be found using wxfilterclassfactory::find(). for example, to list the contents of archive filename:
auto_ptr<wxinputstream> in(new wxffileinputstream(filename)); if (in->isok()) { // look for a filter handler, e.g. for '.gz' const wxfilterclassfactory *fcf; fcf = wxfilterclassfactory::find(filename, wxstream_fileext); if (fcf) { in.reset(fcf->newstream(in.release())); // pop the extension, so if it was '.tar.gz' it is now just '.tar' filename = fcf->popextension(filename); } // look for a archive handler, e.g. for '.zip' or '.tar' const wxarchiveclassfactory *acf; acf = wxarchiveclassfactory::find(filename, wxstream_fileext); if (acf) { auto_ptr<wxarchiveinputstream> arc(acf->newstream(in.release())); auto_ptr<wxarchiveentry> entry; // list the contents of the archive while ((entry.reset(arc->getnextentry())), entry.get() != null) std::wcout << entry->getname().c_str() << "\n"; } else { wxlogerror(_t("can't handle '%s'"), filename.c_str()); } } archives on non-seekable streamsin general, handling archives on non-seekable streams is done in the same way as for seekable streams, with a few caveats. the main limitation is that accessing entries randomly using openentry() is not possible, the entries can only be accessed sequentially in the order they are stored within the archive. for each archive type, there will also be other limitations which will depend on the order the entries' meta-data is stored within the archive. these are not too difficult to deal with, and are outlined below. putnextentry and the entry size when writing archives, some archive formats store the entry size before the entry's data (tar has this limitation, zip doesn't). in this case the entry's size must be passed to putnextentry() or an error occurs. this is only an issue on non-seekable streams, since otherwise the archive output stream can seek back and fix up the header once the size of the entry is known. for generic programming, one way to handle this is to supply the size whenever it is known, and rely on the error message from the output stream when the operation is not supported. getnextentry and the weak reference mechanism some archive formats do not store all an entry's meta-data before the entry's data (zip is an example). in this case, when reading from a non-seekable stream, getnextentry() can only return a partially populated wxarchiveentry object - not all the fields are set. the input stream then keeps a weak reference to the entry object and updates it when more meta-data becomes available. a weak reference being one that does not prevent you from deleting the wxarchiveentry object - the input stream only attempts to update it if it is still around. the documentation for each archive entry type gives the details of what meta-data becomes available and when. for generic programming, when the worst case must be assumed, you can rely on all the fields of wxarchiveentry being fully populated when getnextentry() returns, with the the following exceptions:
this mechanism allows copyentry() to always fully preserve entries' meta-data. no matter what order order the meta-data occurs within the archive, the input stream will always have read it before the output stream must write it. wxarchivenotifier notifier objects can be used to get a notification whenever an input stream updates a wxarchiveentry object's data via the weak reference mechanism. consider the following code which renames an entry in an archive. this is the usual way to modify an entry's meta-data, simply set the required field before writing it with copyentry():
auto_ptr<wxarchiveinputstream> arc(factory->newstream(in)); auto_ptr<wxarchiveoutputstream> outarc(factory->newstream(out)); auto_ptr<wxarchiveentry> entry; outarc->copyarchivemetadata(*arc); while (entry.reset(arc->getnextentry()), entry.get() != null) { if (entry->getname() == from) entry->setname(to); if (!outarc->copyentry(entry.release(), *arc)) break; } bool success = arc->eof() && outarc->close();however, for non-seekable streams, this technique cannot be used for fields such as isreadonly(), which are not necessarily set when getnextentry() returns. in this case a wxarchivenotifier can be used:
class mynotifier : public wxarchivenotifier { public: void onentryupdated(wxarchiveentry& entry) { entry.setisreadonly(false); } };the meta-data changes are done in your notifier's onentryupdated() method, then setnotifier() is called before copyentry():
auto_ptr<wxarchiveinputstream> arc(factory->newstream(in)); auto_ptr<wxarchiveoutputstream> outarc(factory->newstream(out)); auto_ptr<wxarchiveentry> entry; mynotifier notifier; outarc->copyarchivemetadata(*arc); while (entry.reset(arc->getnextentry()), entry.get() != null) { entry->setnotifier(notifier); if (!outarc->copyentry(entry.release(), *arc)) break; } bool success = arc->eof() && outarc->close();setnotifier() calls onentryupdated() immediately, then the input stream calls it again whenever it sets more fields in the entry. since onentryupdated() will be called at least once, this technique always works even when it is not strictly necessary to use it. for example, changing the entry name can be done this way too and it works on seekable streams as well as non-seekable.
|