Find the answer to your Linux question:
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, ...
  1. #1
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Thumbs up 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
    -------------------

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Will this work for you? Warning: I haven't actually tested this code.
    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);
    }
    You can get more information from these commands at the shell prompt:
    Code:
    man 3 system
    man 2 wait
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Exclamation

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

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Have you come across such stuff?
    tar, no. bz2, yes.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  5. #5
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    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

  6. #6
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Smile

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...