extracting compressed file with boost::iostreams - c++

I'm searching for a way to extract a file in c++ by using the boost::iostreams classes.
There is an example in the boost documentation. But it outputs the content of the compressed file to std::cout.
I'm looking for a way to extract it to a file structure.
Does anybody know how to do that?
Thanks!

Boost.IOStreams does not support compressed archives, just single compressed files. If you want to extract a .zip or .tar file to a directory tree, you'll need to use a different library.

The example in the documentation shows how to decompress the file and push the result to another stream.
If you want the output to be directed to an in-memory array instead, you can use a stream of type boost::iostreams::stream<boost::iostreams::array_source>instead.
That is basically a stream-wrapper around an array.
I'm not sure what you mean when you say you want the output in "a file structure" though.

Looks to me like the call to boost::iostreams::copy takes an ostream as the second parameter. Have you tried creating an ofstream with your output file name and using that?

You probably don't want that library. You might want to look around for some others.
E.g. zziplib

Related

Writing and reading ADTF3 Files

I am using ADTF Libraries to write a structure data. I need to verify whether the data is being written properly. How can i do this?
I am assuming you are writing structured data to a .dat/.adtfdat file. In that case, you can always convert a .dat/.adtfdat file into a csv to verify. See examples on how to do so.
If you have access to MATLAB, then the easiest way would be using a simple function in MATLAB : adtffilereader
Alternatively, there are these tools that help in extracting data out of a dat file.

How do I input and output various file types in c++

I've seen a lot of examples of i/o with text files I'm just wondering if you can do the same with other file types like mp3's, jpg's, zip files, etc..?
Will iostream and fstream work for all of these or do I need another library? Do I need a new sdk?
It's all binary data so I'd think it would be that simple. But I've been unpleasently surprised before.
Could I convert all files to text or binary?
It depend on what you mean by "work"
You can think of those files as a book written in Greek.
If you want to just mess with binary representation (display text in Greek on screen) then yes, you can do that.
If you want to actually extract some info: edit video stream, remove voice from audio (actually understand what is written), then you would need to either parse file format yourself (learn Greek) or use some specialized library (hire a translator).
Either way, filestreams are suited to actually access those files data (and many libraries do use them under the hood)
You can work on binary streams by opening them with openmode binary :
ifstream ifs("mydata.mp3", ios_base::binary);
Then you read and write any binary content. However, if you need to generate or modify such content, play a video or display a piture, the you you need to know the inner details of the format you are using. This can be exremely complex, so a library would be recomended. And even with a library, advanced programming skills are required.
Examples of open source libraries: ffmpeg for usual audio/video format, portaudio for audio, CImg for image processing (in C++), libpng for png graphic format, lipjpeg for jpeg. Note that most libraries offer a C api.
Some OS also supports some native file types (example, windows bitmaps).
You can open these files using fstream, but the important thing to note is you must be intricately aware of what is contained within the file in order to process it.
If you just want to open it and spit out junk, then you can definitely just start at the first line of the file and exhaustively push all data into your console.
If you know what the file looks like on the inside, then you can process it just as you would any other file.
There may be specific libraries for processing specific files, but the fstream library will allow you to access any file you'd like.
All files are just bytes. There's nothing stopping you from reading/writing those bytes however you see fit.
The trick is doing something useful with those bytes. You could read the bytes from a .jpg file, for example, but you have to know what those bytes mean, and that's complicated. Usually it's best to use libraries written by people who know about the format in question, and let them deal with that complexity.

Extracting a gz file content with zlib and save it

I want to uncompress a gz file (e.g. "MyFile.gz") and saving its content in a specified path using zlib in C++.
In other words, if the path is "C:\StoredData\", I would like to write a C++ function that creates the file "C:\StoredData\MyFile"
How can I write it?
I do not understand what is the problem here.
Once the data is uncompressed, you can write it anywhere you want using the File Stream.
If you are facing any issues, you can edit your post.
Sorry to post as answer, as i can't add comments.
Read the documentation in zlib.h. The gz* functions will do what you want on the uncompression side. Then you just write the uncompressed data wherever you like.

Opening an existing .doc file using ofstream in C++

Assuming I have a file with .doc extension in Windows platform, how can I open the the file for outputting its contents on the screen using the ofstream object in C++? I am aware that the object can be used to open files in text and binary modes. But I would like to know if a .doc (or even .pdf) file can be opened and its contents read.
I've never actually done this before, but after reading up on it, I think I might have a suggestion. The .docx format is actually just XML that is zipped up. After unzipping, the file is located at word/document.xml. Doing this in a program is where it gets fun.
Two options: If you're using C++ CLR (.NET) then Microsoft has an SDK for you. It should make it pretty easy to open Office documents.
Otherwise if you're just using regular C++, you might have to do some extra work.
Open the file and unzip it using a library like zlib
Find the document.xml file inside
Parse the XML document. You'll probably want to use some kind of XML parsing library for this. You'll have to look up the specs for the XML to figure out how to get the text you want.
C++ std library has ifstream class that can be used to read simple text files, and for read binary files too.
It is up to you to interpret these bytes in the file. To proper interpret the binary file you need to know the format of the file.
If you think of MS Word files then I would start from here: http://en.wikipedia.org/wiki/Office_Open_XML to understand MS Word 2007 format.
You might find the Boost Iostreams library ( http://www.boost.org/doc/libs/1_52_0/libs/iostreams/doc/home.html ) somehow useful if you want to make some filter by yourself.

iostream to zlib and files with C++?

.NET has spoiled me and made me realize how simple certain things can be :(
With C++ i'd like to use either fopen or ostream/istream to push data to either zlib directly or to some kind of memory buffer (then zlib) then proceed to dump it to a file. I'd like something similar to load it back in.
I looked at zlibs example and while it looks simple it isnt an iostream or file and i need to use buffers. Does anyone know of a existing solution?
You need to use boost IOstream API to read the zipped files and create an iostream. I wrote code for this based on Boost documentation. The code and a related question on how to read the zipped file line -by-line is available here:
How can I read line-by-line using Boost IOStreams' interface for Gzip files?