LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-30-2012, 04:02 PM   #1
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Colombia
Distribution: Kubuntu, Debian, Knoppix
Posts: 1,982
Blog Entries: 1

Rep: Reputation: 83
c programming - examples of opening a tgz file


Hi!

Are there simple examples out there of handling of tgz files in c? Specifically, opening the file, list file contents (which files are included in the tgz) and extract a given file.

Thanks in advance.
 
Old 07-30-2012, 05:05 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
Google is your friend:
http://www.google.com/search?q=C+lib...ient=firefox-a

I think you can also access all the common utilities from C using the system() function.
 
1 members found this post helpful.
Old 07-30-2012, 11:37 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,926

Rep: Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524
As far as I know there is no 'libtar' (there is 'libz' though), so the best way is invoking 'tar' via system/popen.
 
Old 07-31-2012, 10:46 AM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
There will be two distinct stages to the process, once you've got to the point of opening the file. The first will be decompressing the file, which will probably involve the use of libz, as NevenTeve has already pointed out. Once the decompression has been done, unrolling the tarball can be done with relatively straightforward code. I've done it in Perl, and although I don't recall many details, I know that it wasn't hard to find the details online, and the actual code was straightforward. There are at least a couple of different tar formats, and the specific format used is embedded in a header in the tarball. I'm not sure if it is practical to unroll the compressed tarball into a data stream without using any additional temporary disk storage. Probably depends on the API exposed by zlib.
As pixellany points out, you can always do it with a system() call, but that's kind of cheating if you want to do it in C. Especially true if you want to stream the tar data into some other part of your application.
--- rod.
 
Old 07-31-2012, 12:56 PM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,541

Rep: Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878
Quote:
Originally Posted by NevemTeve View Post
As far as I know there is no 'libtar' (there is 'libz' though), so the best way is invoking 'tar' via system/popen.
First result from pixellany's suggested search is libtar.
 
1 members found this post helpful.
Old 08-01-2012, 10:12 AM   #6
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Colombia
Distribution: Kubuntu, Debian, Knoppix
Posts: 1,982
Blog Entries: 1

Original Poster
Rep: Reputation: 83
I'm using libtar. At the moment I'm using a fd previously opened. Now, when I try to do th_read() I get a Invalid argument error.

Code is more or less like this:

Code:
FILE * inputFile = fopen(argv[1], "rb");
.
.
.
TAR *t;
if(tar_fdopen(&t, fileno(inputFile), NULL, O_RDONLY, 0, TAR_VERBOSE, TAR_GNU) == 0) {
        fprintf(stderr, "File successfully opened by tar\n");
} else {
        fprintf(stderr, "Couldn't open tgz file\n");
        fprintf(stderr, "tar_open(): %s\n", strerror(errno));
        return 1;
}
// list file content
fprintf(stderr, "List of files\n");
int i;
while ((i = th_read(t)) == 0) {
        th_print_long_ls(t);

        if (TH_ISREG(t) && tar_skip_regfile(t) != 0) {
                fprintf(stderr, "tar_skip_regfile(): %s\n",
                        strerror(errno));
                return -1;
        }
}
if (i == 1) {
        fprintf(stderr, "Reached EOF\n");
} else if (i == -1) {
        fprintf(stderr, "th_read(): %s\n", strerror(errno));
        return -1;
}
The tar_fdopen returns 0 so I go ahead with th_read and I get "Invalid argument" as the error message. Any clues?
 
Old 08-01-2012, 10:20 AM   #7
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Colombia
Distribution: Kubuntu, Debian, Knoppix
Posts: 1,982
Blog Entries: 1

Original Poster
Rep: Reputation: 83
Oh, I think I got it.... till now.... the problem is that the file is a tgz and libtar will handle tar files. I have to decompress it first, I guess. Is there an easy trick to handle tgz files directly with libtar?
 
Old 08-01-2012, 10:53 AM   #8
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 609

Rep: Reputation: 180Reputation: 180
As already said above by TheNbomr, you should look into libz stuff to decompress your archive.
 
Old 08-01-2012, 11:41 AM   #9
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,541

Rep: Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878
Quote:
Originally Posted by eantoranz View Post
Oh, I think I got it.... till now.... the problem is that the file is a tgz and libtar will handle tar files. I have to decompress it first, I guess. Is there an easy trick to handle tgz files directly with libtar?
From the libtar web page:

Quote:
Allows user-specified read() and write() functions, such as zlib's gzread() and gzwrite().
 
Old 08-02-2012, 06:36 AM   #10
segmentation_fault
Member
 
Registered: Sep 2008
Location: Ioannina, Greece
Distribution: Gentoo
Posts: 332

Rep: Reputation: 55
@OP: A little off-topic, you should copy the errno to a temp var, and not use it directly in eg strerror(errno). If something goes wrong with strerror itself, the errno will change so you will not get what you wanted.
 
  


Reply

Tags
extracting, gcc, opening, tgz


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BSD Sockets programming in C with examples nhydra LinuxAnswers Discussion 0 02-18-2007 07:45 PM
newb question - opening *.tgz files willyweedle Linux - Newbie 3 01-27-2007 05:50 PM
tgz file doesn't end in .tgz? detpenguin Slackware 4 05-15-2004 08:13 PM
opening .tgz files tuxfood Red Hat 1 01-08-2004 02:23 PM
Starnge error when opening a tgz file wtih MC rmocius@auste.e Slackware 4 12-01-2003 03:10 AM


All times are GMT -5. The time now is 04:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration