On my Linux remote server, I put in an archive a file a.exe (around 5mb), and convert it to .bz2 with the command 'bzip2 -f a.exe', then I get back the compressed version (a.exe.bz2) on my Windows version. Decompressed it via Winrar, perfect, hash has proven it's exacly the same file, however when I use the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
std::ofstream strm;
strm.open("a.exe");
// Open the file
boost::iostreams::file_source myCprdFile ("a.exe.bz2", std::ios_base::in | std::ios_base::binary);
// Uncompress the file with the BZ2 library and its Boost wrapper
boost::iostreams::filtering_istream bunzip2Filter;
bunzip2Filter.push (boost::iostreams::bzip2_decompressor());
bunzip2Filter.push (myCprdFile);
// Open the file to be read
std::string itReadLine;
while (std::getline (bunzip2Filter, itReadLine)) {
// Initialise the parser
strm << itReadLine << std::endl;
}
strm.close();
}
The outcome is larger than what the file actually was before being sent on Linux. Is there something wrong in what has been done ?
You correctly opened the source file in binary mode, but you opened the destination file in text mode, so you'll read in '\n' characters to memory as '\n', but then you'll write the decompressed '\n' characters as "\r\n".
Also, using std::getline() on a binary stream is kind of weird. To copy from a stream to another you'd normally do
1 2 3 4 5 6
while (src){
char buffer[BUFFER_SIZE];
src.read(buffer, BUFFER_SIZE);
auto bytes_read = src.gcount();
dst.write(buffer, bytes_read);
}
PS: Please remove the empty [code] tags in your post. Firefox users are unable to reply to this thread because of a bug in the site's code.