how do I decompress tar.gz file using c++ program - c++

In my linux program, I want to decompress a tar.gz file contents to a specific directory. Is there any system call or any C++ class available in C/C++ to extract file contents from tar.gz file?

There is excellent library libarchive, which supports accessing multiple archive formats using consistent API. You can follow these examples on how to use it.
If you are on Ubuntu, you can easily install this library using command sudo apt-get install libarchive-dev. On other platforms, you may need to download source code and compile this library yourself.
One advantage of using libarchive vs. using system() calls is not depending on system utilities, and also it should work faster because it does not fork.

You can use several libs like libtar.
Or you can use a system call like you already mentioned:
system("tar -zxf /your/file.tar.gz")

Related

Getting a library to work (QuadProg++ )

I'm trying to use the Quadprog++ library (http://quadprog.sourceforge.net/). I don't understand the instructions though.
To build the library simply go through the ./configure; make; make
install cycle.
In order to use it, you will be required to include in your code file
the "Array.hh" header, which contains a handy C++ implementation of
Vector and Matrices.
There are some "configure", and "MakeFile" files, but they have no extension and I have no idea what to do with them. There are also some ".am", ".in" and ".ac" extensions in the folder.
Does this look familiar to anyone? What do I do with this?
(Edit: On Windows.)
This package is built using the autotools. These files you talk to (*.am, *.in...) are because of the tools automake, and autoconf.
Autotools is a de-facto standard in the GNU/Linux world. Not everybody uses it, but if they do you ease the work of package and distribution managers. Actually they should be portable to any POSIX system.
That said, I'm guessing that you are using a non-unix machine, such as Windows, so the configure script is not directly runable in your system. If you insist in keep using Windows, wich you probably will, your options are:
Use MinGW and MSYS to get a minimal build enviroment compatible with autotools.
Use Cygwin and create a POSIX like environment in your Windows.
Create a VS project, add all the source of the library in there, compile and debug the errors they may arise, as if the code had been written by you.
Search for someone that already did the work and distributes a binary DLL, or similar.
(My favourite!) Get a Linux machine, install a cross-compiler environment to build Windows binaries, and do configure --host i686-mingw32 ; make.
This instruction say how can be build an program delivered like a tarball in Linux. To understand take a look on Why always ./configure; make; make install; as 3 separate steps?.
This can be confusing at first, but here you go. Type these in as shown below:
cd <the_directory_with_the_configure_file>
./configure
At this point, a bunch of stuff will roll past on the screen. This is Autoconf running (for more details, see http://www.edwardrosten.com/code/autoconf/index.html)
When it's done, type:
make
This initiates the build process. (To learn more about GNU make, check out Comprehensive gnu make / gcc tutorial). This will cause several build messages to be printed out.
When this is done, type:
sudo make install
You will be asked for the root password. If this is not your own machine (or you do not have superuser access), then contact the person who administers this computer.
If this is your computer, type in the root password and the library should install in /usr/local/lib/ or something similar (watch the screen closely to see where it puts the .so file).
The rest of it (include the .hh file) seems self-explanatory.
Hope that helps!

C/C++ Windows+Linux ZIP Library for only unpacking?

Continuation of:
Standalone Cross Platform (Windows/Linux)) File Compression for C/C++?
After many attempts on ZLIB ZZLIB LIBZIP MINIZIP I always get many problems at the compilation stage. Many google searches turned out OS-specific libraries and I can't really find anything that fit my 'simple' needs.
I reduced my needs for the library (Or wrapper?) to this:
Works on both Windows and Linux OR 2 separate libraries; one which works on Windows and the other one on Linux, I can make 2 separate projects for Windows and Linux if it is really neccesary
Unpack file from zip to specified directory
Check if file exists in zip file
C OR C++ OR Mixed (yeah, that doesn't matter)
Preferably Very Simple to include into any project
(eg 5 c/cpp files and 1-3 header files? anyway not tons files, when I open all the libzip and zlib archives I have something like: "O my ..")
I've checked many stackoveflow threads too with the words "Windows Linux ZIP C C++" but all the results seem so have libraries which I OR don't know how to compile OR is too difficult to use OR it has too many 'needed stuff' for just simple zip extract and check if file exists.
I had put that project away for a later date and begun it now, and all those compilation errors came up (especially that VC++2010 doesn't have the C-99 inttypes.h)
I have had very good experience with Zipstream C++ library which gives you a nice OOP way of handling zip files.
If your project already uses some of the bigger libs like Boost , then you could try to use the boost::iostreams with the gzip filter, however the functionality is somehow limited.
Or if you happen to use Poco take a look at they're implementation Poco::Zip

Making flex.exe on windows

I have downloaded the source code for flex from the Flex repository. The file is flex version 2.5.35 in tar.gz. Now how can I make flex.exe from this source code?
Just download the binary packages instead, they are available here: http://gnuwin32.sourceforge.net/packages/flex.htm alternatively you need to use the makefile the source comes with to compile it (by passing as a parameter to gcc's make.exe).
You have two options:
Download this instead and follow the directions in the link to download the installer and dependencies.
Use something like 7-Zip to extract the files and follow the included instructions for compilation (much more difficult).
The former sounds like the right answer for you unless you need a feature of the latest version and have the GNU toolchain available on your platform. I don't know if flex will compile with the MS compiler without lots of pain.
If you really want to build it from source by yourself on a Windows machine (which may or may not work), you will want to install Cygwin first.
You can try this: http://sourceforge.net/projects/winflexbison/
This is latest flex-bison tools ported to windows without extra dependencies (like m4.exe or Cygwin dlls)

Installation Script for simple c++ command-line tool, tutorial?

I'm working on a simple command line tool in c++. Half as a fun learning-process thing, and half to distribute to friends/colleagues etc.
I assume the easiest way to make it distributable is just packaging the source code with an installation script---can anyone point me to a good tutorial for setting that up?
In other words, what must a script include to compile the program, put the files in good places*, and make it executable from any directory from the command line?
E.g. I know the compiled binary should go in /usr/local/bin/ , but if I'm writing-to and accessing a text file (for instance), where should that go? What about a file that stores settings/configuration-parameters?
I'm on mac osx, so that would be the starting point, but portability to windows, linux, etc would be great.
You can use CMake to make a cross platform build system, and you can use it's CPack (Wiki here) feature in order to generate binary only packages. First you create a build script that runs and installs on each platform (which CMake makes as easy as can be expected). You then run CPack to generate a package which just includes your binaries.
There is a good tutorial that covers the basic cmake process (including install commands) here.
CMake is generally considered simpler then autoconf (and has better windows support), but each has it's own strengths.
Do not assume that the user installing the program has root access. Prompt, or provide a command-line option, like --install-prefix=/home/user/apps, to specify where to install.
I HATE programs that install shit in /usr/local. If you do that, you'd best wrap it up in an .rpm or .deb or whatever the platform package is so that your app can be cleanly uninstalled.
I would suggest checking out autoconf

Extract a tar.xz in C/C++

I am writing a program that downloads tar.xz files from a server and extracts them in a certain place. I am struggling to find a away of extracting the tar.xz file in the certain place. I am using Qt so a more Qt-way of doing it would be useful, but I don't really mind.
There is no support for archives in Qt. You can either have a look at the KDE library which offers support for virtual file systems or you can use QProcess to call tar directly. Use -C <dir> (uppercase C) to specify the directory to extract to.
[EDIT] There also is libtar (BSD license).