open file in zip file archive 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
syntec
Earned a small fee
Earned a small fee
Posts: 23
Joined: Fri Jul 05, 2013 2:25 pm

open file in zip file archive

Post by syntec » Mon Nov 18, 2013 12:23 pm

Hello together,

i´m fighting with reading compressed txt or xml files in zip files.

i could read out the items in the archive with the help of

Code: Select all

std::auto_ptr<wxZipEntry> pZipItem(new wxZipEntry());

wxFFileInputStream in(sFile);
wxZipInputStream zipFile(in);

while (pZipItem.reset(zipFile.GetNextEntry()), pZipItem.get() != NULL)
{
	wxString name = pZipItem->GetName();

	if (name.rfind("xmlfile.xml") != std::string::npos)
	{
		//open file item (xml file)
	}
}
but what is the best way to open then a file.
I also thought that you are able to open a zipped file like this: wxFSFile* file = fs.OpenFile(_T("c:\\temp\\file.zip#zip:subdirectory\\compressedFile.xml")); but i doesn´t work.

Can you please give me a short example, how to open a txt file of a zip file? I guess, that would help me greatly.

thx

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

Re: open file in zip file archive

Post by doublemax » Mon Nov 18, 2013 12:48 pm

I also thought that you are able to open a zipped file like this: wxFSFile* file = fs.OpenFile(_T("c:\\temp\\file.zip#zip:subdirectory\\compressedFile.xml")); but i doesn´t work.
That should work. Did you add the Zipfile handler?

Code: Select all

// call this once, e.g. in wxApp::OnInit()
wxFileSystem::AddHandler(new wxZipFSHandler);
Use the source, Luke!

ColinTaylor
Earned a small fee
Earned a small fee
Posts: 21
Joined: Wed Oct 30, 2013 9:22 am

Re: open file in zip file archive

Post by ColinTaylor » Mon Nov 18, 2013 4:23 pm

The following will extract the zipped file to a standard file ...

Code: Select all

         wxString filename = entry->GetName();
         wxFFileOutputStream FileOutputStream (filename);
         if (FileOutputStream.IsOk())
         {
            zip.Read (FileOutputStream);
            FileOutputStream.Close();
         }

syntec
Earned a small fee
Earned a small fee
Posts: 23
Joined: Fri Jul 05, 2013 2:25 pm

Re: open file in zip file archive

Post by syntec » Tue Nov 19, 2013 5:22 pm

hello and thanks for your suggestions. unfortunaty they didn´t help.

i put ColinTaylors code into my if condition: if (name.rfind("xmlfile.xml") != std::string::npos), which is also entered, but the condition if (FileOutputStream.IsOk()) is not valid.

Code: Select all

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

...

wxFileSystem::AddHandler(new wxZipFSHandler);

		std::auto_ptr<wxZipEntry> pZipItem(new wxZipEntry());

		wxFFileInputStream in(sFile);
		wxZipInputStream zipFile(in);

		while (pZipItem.reset(zipFile.GetNextEntry()), pZipItem.get() != NULL)
		{
			wxString sFileName = pZipItem->GetName();

			if (sFileName.rfind("xmlfile.xml") != std::string::npos)
			{
				//---> still called ----
				wxFFileOutputStream FileOutputStream (sFileName);
				if (FileOutputStream.IsOk())
				{
					//---> not called -----
					zipFile.Read (FileOutputStream);
					FileOutputStream.Close();
				}
			}
		}

syntec
Earned a small fee
Earned a small fee
Posts: 23
Joined: Fri Jul 05, 2013 2:25 pm

Re: open file in zip file archive

Post by syntec » Thu Nov 21, 2013 8:08 am

has maybe anybody an example or his code snippet for me, how he extract any files (xml) from a compressed (zip) file?

That would help me a lot.

Thanks in advance.

fancyivan
Experienced Solver
Experienced Solver
Posts: 80
Joined: Wed May 26, 2010 8:42 am
Location: Beijing, China
Contact:

Re: open file in zip file archive

Post by fancyivan » Thu Nov 21, 2013 9:39 am

syntec wrote:has maybe anybody an example or his code snippet for me, how he extract any files (xml) from a compressed (zip) file?

That would help me a lot.

Thanks in advance.

Code: Select all

// in ggeiApp.h
class ggeiApp : public wxApp
{
    public:
        virtual bool OnInit();
};
// in ggeiApp.cpp

bool ggeiApp::OnInit()
{
    wxFileSystem::AddHandler(new wxArchiveFSHandler);
	wxFileSystem fileSys;
	// Res.bin is a zip file,  get xml file from zip file and save it to out.xml
       // Or you can visit this xml file by using variable 'doc' before save as.
	wxFSFile *fs = fileSys.OpenFile(wxT("Res.bin#zip:ghgei.xml"));
	if(fs)
	{
	    wxInputStream *inS = fs->GetStream();

	    wxXmlDocument doc(*inS);
	    delete fs;

	    if(!doc.Save(wxT("out.xml")))
            {
                wxLogError(wxT("can not save, pls check what happened."));
                return false;
            }
            return true;
	}
	wxLogError(wxT("can not find ghgei.xml in zip file, pls check."));
       return false;
}
Enjoy
OS: Win7 Ultimate SP1 x64(Windows XP Pro SP3 in VirtualBox)
Compiler: MinGW32 (gcc4.8.1 + gdb7.6.1)
IDE: Code::Blocks 12.11
Lib: wxWidgets3.0.0

syntec
Earned a small fee
Earned a small fee
Posts: 23
Joined: Fri Jul 05, 2013 2:25 pm

Re: open file in zip file archive

Post by syntec » Fri Nov 22, 2013 3:52 pm

thank you very much fancyivan.
Now i´m able to extract xml files :)

Post Reply