{"id":4264,"date":"2022-02-14T07:08:39","date_gmt":"2022-02-14T07:08:39","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=4264"},"modified":"2022-02-14T07:08:40","modified_gmt":"2022-02-14T07:08:40","slug":"13g-wxwidgets-archive-formats","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2022\/02\/14\/13g-wxwidgets-archive-formats\/","title":{"rendered":"13g. wxWidgets &#8211; Archive Formats"},"content":{"rendered":"\n<p><strong>OVERVIEW<\/strong><\/p>\n\n\n\n<p>wxWidgets provides support for reading and writing the following archive formats: zip, tar and zlib. Of these, the zip format is the most commonly used archive, so the sample code below shows how to write and read zip files.<\/p>\n\n\n\n<p>The sample code creates a zip file which contains files from the current working directory based on a wildcard filter. In this case it looks for all files which have the extension of .cpp. You can change it to anything. Once the zip is created, the second button reads the zip files and lists the file names and their size in bytes.<\/p>\n\n\n\n<p><strong>SAMPLE CODE<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">archive.h<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;wx\/wxprec.h&gt;\n#ifndef WX_PRECOMP\n\t#include &lt;wx\/wx.h&gt;\n#endif\n\nclass Archive: public wxFrame {\n\n\tpublic:\n\t  Archive(const wxString &amp;title);\n\tprivate:\n\t  void onZip(wxCommandEvent &amp;event);\n\t  void onUnzip(wxCommandEvent &amp;event);\n\t  wxTextCtrl *mText;\n\t\n};\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">archive.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;archive.h&quot;\n#include &lt;wx\/wfstream.h&gt;\n#include &lt;wx\/zipstrm.h&gt;\n#include &lt;wx\/archive.h&gt;\n\n#include &lt;wx\/dir.h&gt;\n\nArchive::Archive(const wxString &amp;title): \n\twxFrame(NULL, -1, title, wxDefaultPosition, wxSize(600,400)) {\n\n\twxPanel *panel = new wxPanel(this, -1);\n\twxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);\n\tpanel-&gt;SetSizer(vbox);\n\n\tmText = new wxTextCtrl(panel, -1, wxT(&quot;&quot;), wxDefaultPosition, wxDefaultSize,\n\t\t\twxTE_MULTILINE | wxTE_READONLY);\n\n\twxButton *btnZip = new wxButton(panel, -1, wxT(&quot;Create Zip&quot;));\n\twxButton *btnUnzip = new wxButton(panel, -1, wxT(&quot;List Zip Contents&quot;));\n\tvbox-&gt;Add(btnZip,1, wxEXPAND | wxALL, 4);\n\tvbox-&gt;Add(btnUnzip,1, wxEXPAND | wxALL, 4);\n\tvbox-&gt;Add(mText,4,wxEXPAND | wxALL,2);\n\n\tbtnZip-&gt;Bind(wxEVT_BUTTON, &amp;Archive::onZip, this);\n\tbtnUnzip-&gt;Bind(wxEVT_BUTTON, &amp;Archive::onUnzip, this);\n\n\tCenter();\n\n}\n\nvoid Archive::onZip(wxCommandEvent &amp;event) {\n\n\twxString msg = wxT(&quot;&quot;);\n\tmText-&gt;Clear();\n\tmsg.Append(wxT(&quot;Creating test.zip.\\n&quot;));\n\twxFFileOutputStream *fos = new wxFFileOutputStream(wxT(&quot;test.zip&quot;));\n\twxZipOutputStream *zos = new wxZipOutputStream(fos);\n\n\twxDir dir(wxGetCwd());\n\tif (!dir.IsOpened()) {\n\t\tmsg.Append(wxT(&quot;Error opening current dir&quot;));\n\t} else {\n\t\twxString fileName;\n\t\tbool retVal = dir.GetFirst(&amp;fileName, wxT(&quot;*.cpp&quot;));\n\t\twhile (retVal) {\n\t\t\tzos-&gt;PutNextEntry(fileName);\n\t\t\twxFFileInputStream *fis = new wxFFileInputStream(fileName);\n\t\t\tzos-&gt;Write(*fis);\n\t\t\tmsg.Append(&quot;Added &quot;).Append(fileName).Append(wxT(&quot;\\n&quot;));\n\t\t\tretVal = dir.GetNext(&amp;fileName);\n\t\t}\n\t}\n\n\tzos-&gt;Close();\n\tfos-&gt;Close();\n\tmsg.Append(wxT(&quot;Closed zip file\\n&quot;));\n\tmText-&gt;AppendText(msg);\n\n}\n\nvoid Archive::onUnzip(wxCommandEvent &amp;event) {\n\n\twxString msg = wxT(&quot;&quot;);\n\tmText-&gt;Clear();\n\n\tif (wxFileExists(wxT(&quot;test.zip&quot;))) {\n\t\twxFFileInputStream *fis = new wxFFileInputStream(wxT(&quot;test.zip&quot;));\n\t\twxZipInputStream *zis = new wxZipInputStream(*fis);\n\t\twxArchiveEntry *entry = zis-&gt;GetNextEntry();\n\t\twhile (entry != NULL) {\n\t\t\toff_t size = entry-&gt;GetSize();\n\t\t\tmsg.Append(entry-&gt;GetName()).Append(wxT(&quot; (&quot;))\n\t\t\t\t.Append(wxString::Format(wxT(&quot;%lu&quot;), size))\n\t\t\t\t.Append(wxT(&quot; bytes)&quot;))\n\t\t\t\t.Append(wxT(&quot;\\n&quot;));\n\t\t\tentry = zis-&gt;GetNextEntry();\n\t\t}\t\n\t\tmText-&gt;AppendText(msg);\n\n\t} else {\n\t\tmsg.Append(wxT(&quot;test.zip does not exist&quot;));\n\t}\n\tmText-&gt;AppendText(msg);\n\n}\n\n\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">main.h<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;wx\/wxprec.h&gt;\n#ifndef WX_PRECOMP\n    #include &lt;wx\/wx.h&gt;\n#endif\n\nclass Main: public wxApp {\n\n\tpublic:\n\t        virtual bool OnInit();\n};\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">main.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;main.h&quot;\n#include &quot;archive.h&quot;\n \nIMPLEMENT_APP(Main)\n\nbool Main::OnInit() {\n\n    Archive *app = new Archive(wxT(&quot;Archive Handling&quot;));\n    app-&gt;Show(true);\n}\n\n<\/pre><\/div>\n\n\n<p>The output is shown below<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"595\" height=\"455\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/Screenshot-from-2022-02-14-12-32-06.png\" alt=\"\" class=\"wp-image-4268\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/Screenshot-from-2022-02-14-12-32-06.png 595w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/Screenshot-from-2022-02-14-12-32-06-300x229.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/Screenshot-from-2022-02-14-12-32-06-80x60.png 80w\" sizes=\"auto, (max-width: 595px) 100vw, 595px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"595\" height=\"455\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/Screenshot-from-2022-02-14-12-32-22.png\" alt=\"\" class=\"wp-image-4269\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/Screenshot-from-2022-02-14-12-32-22.png 595w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/Screenshot-from-2022-02-14-12-32-22-300x229.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/Screenshot-from-2022-02-14-12-32-22-80x60.png 80w\" sizes=\"auto, (max-width: 595px) 100vw, 595px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW wxWidgets provides support for reading and writing the following archive formats: zip, tar and zlib. Of these, the zip format is the most commonly <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2022\/02\/14\/13g-wxwidgets-archive-formats\/\" title=\"13g. wxWidgets &#8211; Archive Formats\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":3595,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[302,365],"tags":[],"class_list":["post-4264","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cc","category-wxwidgets"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4264","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=4264"}],"version-history":[{"count":4,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4264\/revisions"}],"predecessor-version":[{"id":4270,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4264\/revisions\/4270"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/3595"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=4264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=4264"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=4264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}