Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Buffered zipped stream C++

Server needs to send very large zip file to client. The zipped file cannot be kept in its entirety in memory (too large) or written to disk (several files being sent concurrently may be too big for the hard disk) - it must be streamed directly to client.

Any simple approahces to this? From what I gather the zip format has a CRC in its header so you can't actually stream the zip without creating it in its entirety... If it cannot be done with zip files please suggest an alternative format. Thanks.


1 Answers

You can use Inflate/Deflate streams provided in POCO:

This stream compresses all data passing through it using zlib's deflate algorithm. After all data has been written to the stream, close() must be called to ensure completion of compression.

std::ofstream ostr("data.gz", std::ios::binary);
DeflatingOutputStream deflater(ostr, DeflatingStreamBuf::STREAM_GZIP);
deflater << "Hello, world!" << std::endl;
deflater.close();
ostr.close();

You can redirect that ostr to your TCP stream.

like image 52
masoud Avatar answered Oct 31 '25 00:10

masoud