How to put a file inside a zip file 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
rrcn
Earned a small fee
Earned a small fee
Posts: 14
Joined: Mon Nov 17, 2008 4:57 pm

How to put a file inside a zip file

Post by rrcn » Wed Mar 11, 2009 4:49 pm

I want to create a compressed zip file and put inside it a single existing file.
I saw the help documents and I don't understand how can I do it.

I would really appreciate your help!

JimFairway
wxWorld Domination!
wxWorld Domination!
Posts: 1059
Joined: Sun Dec 30, 2007 6:40 pm
Location: Canada

Post by JimFairway » Wed Mar 11, 2009 7:36 pm

Hi,

Check the reference code here: http://docs.wxwidgets.org/stable/wx_wxa ... xarccreate

Hope that helps,

Jim
OS: Vista SP1, wxWidgets 2.8.7.

computerquip
Experienced Solver
Experienced Solver
Posts: 72
Joined: Fri Feb 20, 2009 7:13 pm
Location: $(#wx)\src

Post by computerquip » Wed Mar 11, 2009 9:35 pm

To add to that, there are TONS and TONS and TONS of libraries that you can choose from to do this. One of the more popular open source libraries is known as LZMA SDK. 7 Zip uses this compression library and I believe it has a few functions that could help you out. But they're are a lot more than that, just look around. wxWidgets built in widget works too. :D

rrcn
Earned a small fee
Earned a small fee
Posts: 14
Joined: Mon Nov 17, 2008 4:57 pm

Post by rrcn » Thu Mar 12, 2009 1:05 pm

JimFairway wrote:Hi,

Check the reference code here: http://docs.wxwidgets.org/stable/wx_wxa ... xarccreate

Hope that helps,

Jim
But if the file I want to put inside the zip archive isn't a text file does it works?

computerquip
Experienced Solver
Experienced Solver
Posts: 72
Joined: Fri Feb 20, 2009 7:13 pm
Location: $(#wx)\src

Post by computerquip » Thu Mar 12, 2009 1:31 pm

Please tell me your joking...

Muetdhiver
Super wx Problem Solver
Super wx Problem Solver
Posts: 323
Joined: Sun Jun 08, 2008 11:59 am
Location: Bordeaux, France

Post by Muetdhiver » Thu Mar 12, 2009 1:35 pm

Off course it works ! Zip compression can match every file formats ! Here is an example but try to search a bit in the wxWidgets documentation :

Code: Select all

wxFFileOutputStream loc_o_file( _S( par_o_file ) );
wxZipOutputStream loc_o_zip( loc_o_file );
wxDataOutputStream loc_o_data( loc_o_zip );

// Create a new entry (ie a new file) into your archive
loc_o_zip.PutNextEntry( wxT("test.bin") );

// Write your bin data here
loc_o_data << YOUR_BINARY_DATA_HERE ;

I made this simple piece of code but I did no try to build it. Hope it will help you. Also have a look to the inheritence diagram on the stream management (wxDataOutputStream accepts a wxDataStream object reference). According to this diagram, you can guess all objects that derive from wxDataStream and then use them :

http://docs.wxwidgets.org/trunk/classwx ... tream.html
(I think this diagram very useful...).

Good luck.
OS: Ubuntu 11.10
Compiler: g++ 4.6.1 (Eclipse CDT Indigo)
wxWidgets: 2.9.3

rrcn
Earned a small fee
Earned a small fee
Posts: 14
Joined: Mon Nov 17, 2008 4:57 pm

Post by rrcn » Thu Mar 12, 2009 4:29 pm

Muetdhiver wrote:Off course it works ! Zip compression can match every file formats ! Here is an example but try to search a bit in the wxWidgets documentation :

Code: Select all

wxFFileOutputStream loc_o_file( _S( par_o_file ) );
wxZipOutputStream loc_o_zip( loc_o_file );
wxDataOutputStream loc_o_data( loc_o_zip );

// Create a new entry (ie a new file) into your archive
loc_o_zip.PutNextEntry( wxT("test.bin") );

// Write your bin data here
loc_o_data << YOUR_BINARY_DATA_HERE ;

I made this simple piece of code but I did no try to build it. Hope it will help you. Also have a look to the inheritence diagram on the stream management (wxDataOutputStream accepts a wxDataStream object reference). According to this diagram, you can guess all objects that derive from wxDataStream and then use them :

http://docs.wxwidgets.org/trunk/classwx ... tream.html
(I think this diagram very useful...).

Good luck.
I tried this, but the compressed file isn't made equal as the input file:

Code: Select all

void MainWindow::BufferedCopy(wxTextInputStream& inStream, wxTextOutputStream& outStream)
{

	wxString str;
	for(;;) {
		str = inStream.ReadLine();
		if(str == wxEmptyString)
			break;
		outStream << str;
	}
}

void MainWindow::exportCollection(const wxString& from, const wxString& to)
{

	wxFFileInputStream in(from);
	wxTextInputStream inStream(in);

	wxFFileOutputStream out(to);
	wxZipOutputStream zip(out);
	wxTextOutputStream outStream(zip);
	zip.PutNextEntry(wxT("collection.col"));


	BufferedCopy(inStream, outStream);
	
}

xin.songtao
Experienced Solver
Experienced Solver
Posts: 86
Joined: Wed Apr 18, 2007 6:10 am
Location: Shanghai China

Post by xin.songtao » Mon Mar 16, 2009 11:02 am

I compress the files into "a images.zip", for reference only.:D :D

Code: Select all

bool CreateImageZip(const wxString &ressoureDir, const wxString &destPath)
{
	wxString sep(wxFileName::GetPathSeparator()); 

	wxArrayString files; 

	wxDir::GetAllFiles(ressoureDir,&files);//the dir contented all the files need to compress. 

	wxFFileOutputStream  fileout(destPath+_T("images.zip")); 

	wxZipOutputStream zipout (fileout); 

	wxFFileInputStream *in = NULL;	
	wxZipEntry *entry=NULL; 
	wxFileName *zipname = NULL; 

	for (size_t i = 0;i != files.GetCount(); ++i) 
	{ 
		if (in!=NULL)
		{
			delete in;
			in = NULL;
		}
		if (zipname!=NULL)
		{
			delete zipname;
			zipname = NULL;
		}

		in = new wxFFileInputStream(files[i]); 
		zipname = new wxFileName (files[i]); 
		if(zipname->MakeRelativeTo(ressoureDir)) 
		{ 
			entry =  new wxZipEntry(wxT("images/")+zipname->GetFullPath()); 
			zipout.PutNextEntry(entry); 
			zipout.Write(*(in)); 
		}
		else
		{
			return false;
		}
	}
	zipout.CloseEntry(); 
	zipout.Close(); 
	fileout.Close(); 
	if (in!=NULL)
	{
		delete in;
		in = NULL;
	}
	if (zipname!=NULL)
	{
		delete zipname;
		zipname = NULL;
	}
	return true;
}

Post Reply