WxWidgets:ArchiveFormats
lastmod 2026-11-10created 2014-09-053 edits
WxWidgets:ArchiveFormats is a page about reading and writing compressed files such as ZIP and TAR with the wxWidgets archive stream classes. wxZipInputStream returns entries one by one through GetNextEntry, and extraction works by checking whether each entry is a directory or a file, then creating the directory or copying the contents to an output stream. Writing is handled by wxZipOutputStream.
Extracting files from a ZIP archive
#include <wx/filesys.h>
#include <wx/zipstrm.h>
#include <wx/wfstream.h>
#include <memory.h>
bool unzip(const wxString & zip_path, const wxString & target_dir)
{
bool result = true;
wxFileInputStream in(zip_path);
wxZipInputStream zip(in);
wxZipEntry * entry;
entry = zip.GetNextEntry();
while (entry != NULL) {
wxString name = entry->GetName();
wxString target_file_path = target_dir + wxFileName::GetPathSeparator() + name;
// Directory or File ?
if (entry->IsDir() == true) {
int perm = entry->GetMode();
wxFileName::Mkdir(target_file_path, perm, wxPATH_MKDIR_FULL);
} else {
zip.OpenEntry(*entry);
if (zip.CanRead() == false) {
_LOG(wxT("Can not read zip entry: ") + name);
result = false;
break;
}
wxFileOutputStream file(target_file_path);
zip.Read(file);
}
delete entry;
entry = zip.GetNextEntry();
}
return result;
}