Page 1 of 1

How to put a file inside a zip file

Posted: Wed Mar 11, 2009 4:49 pm
by rrcn
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!

Posted: Wed Mar 11, 2009 7:36 pm
by JimFairway
Hi,

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

Hope that helps,

Jim

Posted: Wed Mar 11, 2009 9:35 pm
by computerquip
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

Posted: Thu Mar 12, 2009 1:05 pm
by rrcn
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?

Posted: Thu Mar 12, 2009 1:31 pm
by computerquip
Please tell me your joking...

Posted: Thu Mar 12, 2009 1:35 pm
by Muetdhiver
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.

Posted: Thu Mar 12, 2009 4:29 pm
by rrcn
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);
	
}

Posted: Mon Mar 16, 2009 11:02 am
by xin.songtao
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;
}