Use the boost: iostreams library to compress and decompress data

Source: Internet
Author: User

Use the boost: iostreams library to compress and decompress data.-Bo Cheng's blog-blog channel-csdn. net

Use the boost: iostreams library to compress and decompress data Category: boost 92 people reading Comment (0) Favorites Report Boostboostiostreams

In today's project, we are going to use gzip to compress the data and find the boost: iostreams library. Therefore, we checked the relevant information. Below we will record some of our understanding about this library.

Iostreams consists of two main types: Device and filter, which can be found in the source code directory. The iostreams directory contains these two directories to find related classes.

Device is like a device,It cannot be used independently. It must be used with a normal stream or stream_buffer,You can input/output data in the stream to this device, which can be divided

Source, which accesses character sequences in Read mode, such as: file_source for file input.

Sink, which accesses character sequences in write mode, such as: file_sink for file output.

Stream <file_source> is a file input stream, similar to ifilestream, and stream <file_sink> is a file output stream, similar to ofilestream.

A filter is similar to a device and cannot be used independently. It must be used with a filter stream.Filtering_streamOrFiltering_streambufTo filter the data in the stream according to one rule, which can be divided:

inputfilter, filter the input read by source. example: gzip_deco Mpressor decompress the data in the stream by gzip algorithm .
outputfilter: filters the output written to the sink. for example: gzip_compressor compresses the data in the stream according to the gzip algorithm .

HoweverFiltering_stream maintains a filter linked list, ending with device. The output filter stream filtering_ostream is used to execute the filter in sequence and then output it to Devic, for example:

Compression time

Filtering_ostream out;
Out. Push (gzip_compressor ()); // Gzip outputfilter

Out. Push (bzip2_compressor (); // Bzip2 outputfilter
Out. Push (boost: iostreams: file_sink ("test.txt"); // end with file_sink Device

This will first compress the data to the GZIP file, and then compress the data to the text.txt file.


Decompress

Filtering_istream in;
In. Push (gzip_decompressor ();/gzip inputfilter

In. Push (gzip_compressor ();/Bzip2 inputfilter
In. Push (file_source ("test.txt "));

In this case, read the data in the test.txt file, decompress Bzip2, decompress gzip, and store the data in the in stream. This is exactly the reverse compression order.


ImplementationCode

# Include <boost/iostreams/filtering_stream.hpp>
# Include <boost/iostreams/device/file_descriptor.hpp>
# Include <boost/iostreams/device/file. HPP>
# Include <iostream>
# Include <sstream>
Int main ()
{
  Try {
  STD: String DEST;
Boost: iostreams: filtering_ostream out;
Out. Push (boost: iostreams: gzip_compressor ());
// Out. Push (boost: iostreams: file_sink ("test.txt "));
Out. Push (boost: iostreams: back_inserter (DEST ));
// Boost: iostreams: Write (Out, "hello", 5 );
Boost: iostreams: Copy (STD: stringstream ("hello"), OUT );
STD: cout <"DEST:" <DEST <STD: Endl;
Boost: iostreams: filtering_istream in;
In. Push (boost: iostreams: gzip_decompressor ());
// In. Push (boost: iostreams: file_source ("test.txt "));
STD: stringstream SS (DEST );
In. Push (SS );
Char text [100] = {0 };
Boost: iostreams: Read (in, text, 4 );
// Boost: iostreams: Copy (in, STD: stringstream (text ));
STD: cout <"text:" <text <STD: Endl;
}
Catch (STD: exception & E)
{
STD: cout <"exception:" <E. What () <STD: Endl;
}
Catch (...)
{
STD: cout <"unknown exception." <STD: Endl;
}
System ("pause ");
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.