wxWidgets中解压ZIP文件的代码

来源:互联网 发布:淘宝阿里妈妈推广 编辑:程序博客网 时间:2024/05/17 22:15

wxWidgets2.9.3

 

#include <wx/filesys.h>
#include <wx/zipstrm.h>
#include <wx/wfstream.h>
#include <wx/log.h>
#include <memory>

 

bool ExtractZipFiles(const wxString& aZipFile, const wxString& aTargetDir)
{
    wxString PathSeparator=wxFileName::GetPathSeparator();
    wxString TargetDir;
    if (aTargetDir.substr(aTargetDir.Length()-1,1) != PathSeparator)
       TargetDir=aTargetDir + PathSeparator;
    else
       TargetDir=aTargetDir;

    wxFileInputStream in(aZipFile);
    if (!in) {
       wxLogError(wxT("打开压缩文件 '") + aZipFile + wxT("'错误!"));
       return false;
    }
    wxZipInputStream zip(in);
    wxZipEntry* entry=zip.GetNextEntry();
    while (entry != NULL)
    {
        wxString name=entry->GetName();
        wxFileName fname(name);
        wxString FilePath=fname.GetPath();
        if (!wxFileName::DirExists(TargetDir + FilePath))
          wxFileName::Mkdir(TargetDir + FilePath,wxS_DIR_DEFAULT,wxPATH_MKDIR_FULL);

        wxFFileOutputStream file(TargetDir + name, wxT("w"));
        if (!file) {
            wxLogError(wxT("创建输出文件 '") + TargetDir + name + wxT("'错误."));
            return false;
        }
        zip.Read(file);
        delete entry;
        entry=zip.GetNextEntry();
    }
  return true;
}

 

使用示例:ExtractZipFiles(wxT("c:\\test.zip"),wxT("c:\\test"));  //其中C:\\test目录必须存在


 

原创粉丝点击