Can I read a zip file using wxZip? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Sangz
In need of some credit
In need of some credit
Posts: 4
Joined: Tue Nov 01, 2016 7:35 am

Can I read a zip file using wxZip?

Post by Sangz » Tue Nov 01, 2016 1:20 pm

Without unzipping, I want to read a file ( xml file or text file ) which is zipped in an archive. I want to do this process during the runtime of the program to get the data from that file which is used for the further processing of that program.

Manolo
Can't get richer than this
Can't get richer than this
Posts: 712
Joined: Mon Apr 30, 2012 11:07 pm

Re: Can I read a zip file using wxZip?

Post by Manolo » Tue Nov 01, 2016 4:21 pm


Sangz
In need of some credit
In need of some credit
Posts: 4
Joined: Tue Nov 01, 2016 7:35 am

Re: Can I read a zip file using wxZip?

Post by Sangz » Wed Nov 02, 2016 5:05 am

Code: Select all

wxZipInputStream zip(in);

            while (entry.reset(zip.GetNextEntry()), entry.get() != NULL) {
                wxString name = entry->GetName();
                name = strPageName.BeforeLast('\\') + wxFileName::GetPathSeparator() + name;

                    zip.OpenEntry(*entry.get());

                    wxFileOutputStream file(name);

                    if (!file) {
                    wxLogError(_T("Can not create file '") + name + _T("'."));
                    break;
                    }   

                    zip.Read(file); 
In the above mentioned code, I can read after extracting the file from the zip archive. I want to read the data of particular file directly from the zip archive. Is that possible to do?

User avatar
doublemax
Moderator
Moderator
Posts: 14954
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Can I read a zip file using wxZip?

Post by doublemax » Wed Nov 02, 2016 6:41 am

Code: Select all

zip.Read(file); 
Replace this line with your own code reading directly from the wxInputStream "zip".

http://docs.wxwidgets.org/trunk/classwx ... tream.html
Use the source, Luke!

Sangz
In need of some credit
In need of some credit
Posts: 4
Joined: Tue Nov 01, 2016 7:35 am

Re: Can I read a zip file using wxZip?

Post by Sangz » Thu Nov 03, 2016 11:40 am

Code: Select all

wxFFileInputStream in(wxT("test.zip"));
			wxZipInputStream zipin(in);			
			wxInputStream *ins = zipin.GetFilterInputStream();
			wxFileOutputStream file( wxT("check.txt"));
			ins->Read(file);
@doublemax I have tried like the above code. I couldn't read the file properly. Am I doing right or I have to do it in some other way?

User avatar
doublemax
Moderator
Moderator
Posts: 14954
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Can I read a zip file using wxZip?

Post by doublemax » Sat Nov 05, 2016 12:07 am

You need zipentry to "select" a specific file in the archive.

Personally i prefer to use wxFileSystem to access files in archives, because the handling is more similar to "normal" file operations.

Suppose you have a zip file "test.zip" which just contains one file: "test.txt". The following code will read that text file and save it as "test_unpacked.txt"

Code: Select all

#include <wx/fs_zip.h>
#include <wx/zipstrm.h>
#include <wx/wfstream.h>

wxFileSystem::AddHandler(new wxZipFSHandler);

wxFileSystem fs;
wxFSFile *zip = fs.OpenFile( "d:\\test.zip#zip:test.txt");
if(zip!=NULL)
{
  wxInputStream *in = zip->GetStream();
  if ( in != NULL )
  {
    wxFileOutputStream out( "d:\\test_unpacked.txt" );
    out.Write(*in);
    out.Close();
  }
  delete zip;
}
Use the source, Luke!

Sangz
In need of some credit
In need of some credit
Posts: 4
Joined: Tue Nov 01, 2016 7:35 am

Re: Can I read a zip file using wxZip?

Post by Sangz » Wed Nov 09, 2016 4:58 am

Thank You @doublemax.

It is working fine. I got what i want.

Post Reply