Commit | Line | Data |
291d3373 |
1 | #!/usr/bin/perl |
2 | use strict; |
3 | |
4 | use Getopt::Std; |
5 | use Archive::Tar; |
6 | use File::Find; |
7 | |
8 | my $opts = {}; |
9 | getopts('dcvzthxf:', $opts) or die usage(); |
10 | |
11 | ### show the help message ### |
12 | die usage() if $opts->{h}; |
13 | |
14 | ### enable debugging (undocumented feature) |
15 | local $Archive::Tar::DEBUG = 1 if $opts->{d}; |
16 | |
17 | ### sanity checks ### |
18 | unless ( 1 == grep { defined $opts->{$_} } qw[x t c] ) { |
19 | die "You need exactly one of 'x', 't' or 'c' options: " . usage(); |
20 | } |
21 | |
22 | my $compress = $opts->{z} ? 1 : 0; |
23 | my $verbose = $opts->{v} ? 1 : 0; |
24 | my $file = $opts->{f} ? $opts->{f} : 'default.tar'; |
25 | my $tar = Archive::Tar->new(); |
26 | |
27 | if( $opts->{c} ) { |
28 | my @files; |
29 | find( sub { push @files, $File::Find::name; |
30 | print $File::Find::name.$/ if $verbose }, @ARGV ); |
81a5970e |
31 | |
32 | Archive::Tar->create_archive( $file, $compress, @files ); |
291d3373 |
33 | exit; |
81a5970e |
34 | } |
291d3373 |
35 | |
36 | my $tar = Archive::Tar->new($file, $compress); |
37 | |
38 | if( $opts->{t} ) { |
81a5970e |
39 | print map { $_->full_path . $/ } $tar->get_files; |
291d3373 |
40 | |
81a5970e |
41 | } elsif( $opts->{x} ) { |
291d3373 |
42 | print map { $_->full_path . $/ } $tar->get_files |
43 | if $verbose; |
44 | Archive::Tar->extract_archive($file, $compress); |
81a5970e |
45 | } |
291d3373 |
46 | |
47 | |
48 | |
49 | sub usage { |
50 | qq[ |
81a5970e |
51 | Usage: ptar -c [-v] [-z] [-f ARCHIVE_FILE] FILE FILE ... |
52 | ptar -x [-v] [-z] [-f ARCHIVE_FILE] |
53 | ptar -t [-z] [-f ARCHIVE_FILE] |
291d3373 |
54 | ptar -h |
81a5970e |
55 | |
291d3373 |
56 | ptar is a small, tar look-alike program that uses the perl module |
81a5970e |
57 | Archive::Tar to extract, create and list tar archives. |
58 | |
291d3373 |
59 | Options: |
60 | x Extract from ARCHIVE_FILE |
61 | c Create ARCHIVE_FILE from FILE |
62 | t List the contents of ARCHIVE_FILE |
63 | f Name of the ARCHIVE_FILE to use. Default is './default.tar' |
64 | z Read/Write zlib compressed ARCHIVE_FILE (not always available) |
65 | v Print filenames as they are added or extraced from ARCHIVE_FILE |
66 | h Prints this help message |
67 | |
68 | See Also: |
69 | tar(1) |
70 | Archive::Tar |
71 | |
72 | \n] |
73 | } |
74 | |