I figured it out. It is so simple but was easy for me to overlook when
focusing on learning crypto++ lib.
Short answer is you should always open the files in binary mode.
Here is the corrected code for future reference:
try{
std::ifstream orginalFile("C:\\Somefolder\\testorginal",ios_base::in |
ios_base::binary);
std::string orginalData;
FileSource orginalSource(orginalFile,true,new StringSink(orginalData));
orginalFile.close();
std::ofstream zippedOutFile("C:\\Somefolder\\testzipped",
ios_base::trunc|
ios_base::binary);
FileSink* zippedSink = new FileSink(zippedOutFile);
Gzip z(zippedSink);
z.SetDeflateLevel(Gzip::MAX_DEFLATE_LEVEL);
z.Put((byte*)(orginalData.c_str()),orginalData.size(),true);
z.MessageEnd();
zippedOutFile.close();
std::ifstream
zippedIfstreamFile("C:\\Somefolder\\testzipped",ios_base::in
| ios_base::binary);
std::string zippedData;
FileSource zippedSource(zippedIfstreamFile,true,new
StringSink(zippedData));
zippedIfstreamFile.close();
std::ofstream unzippedFile("C:\\Somefolder\\testunzipped",ios_base::out
|
ios_base::binary);
FileSink* unzippedSink = new FileSink(unzippedFile);
Gunzip uz(unzippedSink);
uz.Put((byte*)zippedData.c_str(),(zippedData.size()));
uz.MessageEnd();
unzippedFile.close();
}catch(Exception e){
MessageBox(NULL,CString(e.what()),_T(""),MB_OK);
return -1;
}
/////////////////////////////////////////////////////////////////////////////
cppi812 wrote:
>
> I am intermediate with c++ but new to the pipeline paradigm and crypto++
> lib. I am trying to zip and unzip a file. I have been able to gzip the
> file but when i try to unzip it to a filesink, the file is empty. The gzip
> produces a file half the size as the original uncompressed file but gunzip
> is giving me a file with nothing in it. I know I am doing something wrong
> here. please help.
> I'm using the the static lib crypto++ version 5.6.1 on windows 7 compiling
> with msvs 2010.
>
> Here is the code that don't work:
>
> try{
>
> std::ofstream
> zippedOutFile("C:\\Somefolder\\testzipped",std::ios_base::trunc);
> FileSink* zippedSink = new FileSink(zippedOutFile);
>
> Gzip z(zippedSink);
> z.Put((byte*)(unCompressedData.c_str()),unlength,true);
> z.MessageEnd();
> zippedOutFile.close();
>
> std::ifstream
> zippedIfstreamFile("C:\\Somefolder\\testzipped",std::ios_base::in);
> FileSource zippedSource(zippedIfstreamFile,true);
>
> std::ofstream
> unzippedFile("C:\\Somefolder\\\testunzipped",std::ios_base::out);
> FileSink* unzippedSink = new FileSink(unzippedFile);
>
> Gunzip uz(unzippedSink);
> lword maxZipData = zippedSource.TotalBytesRetrievable();
> byte* zipData = new byte[maxZipData+1];
> memset(zipData,'\0',sizeof(zipData));
> zippedSource.Get(zipData,maxZipData);
> uz.Put(zipData,sizeof(zipData),true);
> unzippedFile.close();
> zippedIfstreamFile.close();
>
> }catch(Exception e){
> MessageBox(NULL,CString(e.what()),_T(""),MB_OK);
> return -1;
> }
>
>
> thanks for your help :confused:
>
--
View this message in context:
http://old.nabble.com/crytpo%2B%2B-gunzip-a-file-that-i-compressed-with-crytpo%2B%2B-gzip-tp34992733p34997746.html
Sent from the Crypto++ Users mailing list archive at Nabble.com.
--
--
You received this message because you are subscribed to the "Crypto++ Users"
Google Group.
To unsubscribe, send an email to [email protected].
More information about Crypto++ and this group is available at
http://www.cryptopp.com.
---
You received this message because you are subscribed to the Google Groups
"Crypto++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.