Results 1 to 6 of 6
I need to compress file(s) ..i want it to be tar.bz2 format.
How to do this in a C program?
any idea on this?
I'm think about something like this,
...
- 07-31-2008 #1
Simple C Program for compression?
I need to compress file(s) ..i want it to be tar.bz2 format.
How to do this in a C program?
any idea on this?
I'm think about something like this,
tar -cf - filenames.txt | bzip2 >archivefile.tar.bz2
Try and implement above command by combining fork() and exec() and pipe() system call.
Is there any simple way to achieve this?- Lakshmipathi.G
-------------------
FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
-------------------
- 07-31-2008 #2
Will this work for you? Warning: I haven't actually tested this code.
You can get more information from these commands at the shell prompt:Code:#include <stdlib.h> int return_status; return_status=system("tar -cf - filenames.txt | bzip2 >archivefile.tar.bz2"); if(return_status==-1) { fprintf(stderr,"the system() call failed completely"); exit(1); } if(WIFEXITED(return_status)) { if(WEXITSTATUS(return_status)) { fprintf(stderr,"the system() call returned with bad status %d\n",WEXITSTATUS(return_status)); exit(1); } } if(WIFSIGNALED(return_status)) { fprintf(stderr,"the system() call caught signal %d\n",WTERMSIG(return_status)); exit(1); } #ifdef WCOREDUMP if(WCOREDUMP(return_status)) { fprintf(stderr,"a core dump was produced\n"); } else { fprintf(stderr,"a core dump was not produced\n"); } #endif if(WIFSTOPPED(return_status)) { fprintf(stderr,"the system() call was interrupted with signal %d\n",WSTOPSIG(return_status)); exit(2); } if(WIFCONTINUED(return_status)) { fprintf(stderr,"the system() call was resumed. you should never see this.\n"); exit(3); }
Code:man 3 system man 2 wait
--
Bill
Old age and treachery will overcome youth and skill.
- 08-06-2008 #3
thanks wje_lf it works !!
I got another query , is it possible to use tar api in c program rather than using tar as command.
something like this :
tar_compress(char *filename);
Have you come across such stuff?- Lakshmipathi.G
-------------------
FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
-------------------
- 08-06-2008 #4tar, no. bz2, yes.Have you come across such stuff?--
Bill
Old age and treachery will overcome youth and skill.
- 08-07-2008 #5
I tried Googling for "libtar", and I found a library that appears to be rather old, but it might work.
Another option is to use popen(). It's not using a tar API, but because tar can read and write from/to stdin/stdout, you can actually just open a pipe right to it and write your data directly into the pipe:
Code:char *data; ... FILE *tar = popen("tar czf tarball.tar -", "w"); fputs(data, tar);DISTRO=Arch
Registered Linux User #388732
- 08-07-2008 #6
Thanks wje_lf I'll check it out the bz2 link...
Thanks Cabhan,I think pipe() will be enough for me
....I'll try both bz2 and pipe with tar and get back here with results...
- Lakshmipathi.G
-------------------
FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
-------------------


Reply With Quote