Re: Script to automate extraction of file from compressed archives

Discussion in 'Perl' started by Michael Wehner, May 21, 2006.

  1. Peter Thorne wrote:
    > I am looking for a script that will recursively extract a single file
    > from each of these archives e.g. the file INSTALL, for the extracted
    > file to be moved to a different location and renamed to the name of
    > the archive itself, but keeping the same directory structure;


    This should be quite straightforward, especially if you intend to use an
    external program like `tar' to unarchive your files.

    You should need only one function that accepts as its sole argument a
    directory name. This function is recursive, in that it calls itself,
    passing along the name of whatever subdirectory it's currently looking
    at. This function is just called from one starting point in your
    program, and should be passed the initial directory name (perhaps from a
    command-line switch).

    The code might look like:

    #!/usr/bin/perl

    my $tar_path = '/bin/tar'; # Path to the tar program
    my $starting_dir = shift; # Starting point for extraction

    extract_dir($starting_dir);

    exit;

    sub extract_dir {
    my $current_dir = shift; # Current directory level

    # Get a filehandle for this directory
    opendir my $DIR, $current_dir;

    # Go through each file in this directory
    for my $filename (readdir $DIR) {
    # Check to see if this is a regular file
    if (-f "$current_dir/$filename") {
    # Extract the `INSTALL' file
    system $tar_path, '-xf', $filename, 'INSTALL';
    }

    # Check to see if this is a subdirectory
    if (-d "$current_dir/$filename") {
    # Go through this directory
    extract_dir("$current_dir/$filename");
    }
    }

    return;
    }


    And of course, you'll want to add additional logic to make sure that the
    current file it's iterating over isn't the same directory or a parent
    directory (`.' and `..') or some cyclical symlink, and that if it's a
    regular file, that it's an archive you actually want to extract. Given
    your cited example, you'll probably want to also check the type of
    archive in order to pass `tar' the appropriate arguments (since of
    course, my example only works on a basic uncompressed archive).

    Hope this gives some direction.

    - Michael Wehner
     
    Michael Wehner, May 21, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ad
    Replies:
    6
    Views:
    3,224
  2. robert
    Replies:
    8
    Views:
    475
    robert
    Mar 13, 2006
  3. flebber
    Replies:
    9
    Views:
    523
    John Machin
    Jun 10, 2007
  4. Qu0ll
    Replies:
    5
    Views:
    2,873
    Qu0ll
    Jul 9, 2009
  5. David Karr
    Replies:
    2
    Views:
    509
    J. Gleixner
    Jun 1, 2012
Loading...

Share This Page