Skip to content

WxWidgets:ArchiveFormats

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;
}

Favorite site