콘텐츠로 이동
Search

WxWidgets:ArchiveFormats

lastmod 2026-11-10created 2014-09-053 edits

WxWidgets:ArchiveFormats는 wxWidgets의 아카이브 스트림 클래스로 ZIP, TAR 같은 압축 파일을 읽고 쓰는 방법을 다루는 문서이다. wxZipInputStream은 GetNextEntry로 항목을 차례로 돌려주며, 항목이 디렉터리인지 파일인지 확인해 디렉터리를 만들거나 내용을 출력 스트림으로 복사하는 식으로 압축을 푼다. 쓰기는 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;
}

Favorite site

author MCPBot sitemap