LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   c programming - examples of opening a tgz file (http://www.linuxquestions.org/questions/programming-9/c-programming-examples-of-opening-a-tgz-file-4175419418/)

eantoranz 07-30-2012 03:02 PM

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.

pixellany 07-30-2012 04:05 PM

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.

NevemTeve 07-30-2012 10:37 PM

As far as I know there is no 'libtar' (there is 'libz' though), so the best way is invoking 'tar' via system/popen.

theNbomr 07-31-2012 09:46 AM

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.

ntubski 07-31-2012 11:56 AM

Quote:

Originally Posted by NevemTeve (Post 4741799)
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.

eantoranz 08-01-2012 09:12 AM

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?

eantoranz 08-01-2012 09:20 AM

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?

414N 08-01-2012 09:53 AM

As already said above by TheNbomr, you should look into libz stuff to decompress your archive.

ntubski 08-01-2012 10:41 AM

Quote:

Originally Posted by eantoranz (Post 4743209)
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().

segmentation_fault 08-02-2012 05:36 AM

@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.


All times are GMT -5. The time now is 01:24 PM.