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 ); |
b90ab6b2 |
31 | |
32 | Archive::Tar->create_archive( $file, $compress, @files ); |
291d3373 |
33 | exit; |
b90ab6b2 |
34 | } |
291d3373 |
35 | |
36 | my $tar = Archive::Tar->new($file, $compress); |
37 | |
38 | if( $opts->{t} ) { |
b90ab6b2 |
39 | print map { $_->full_path . $/ } $tar->get_files; |
291d3373 |
40 | |
b90ab6b2 |
41 | } elsif( $opts->{x} ) { |
291d3373 |
42 | print map { $_->full_path . $/ } $tar->get_files |
43 | if $verbose; |
44 | Archive::Tar->extract_archive($file, $compress); |
b90ab6b2 |
45 | } |
291d3373 |
46 | |
47 | |
48 | |
49 | sub usage { |
50 | qq[ |
b90ab6b2 |
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 |
b90ab6b2 |
55 | |
291d3373 |
56 | ptar is a small, tar look-alike program that uses the perl module |
b90ab6b2 |
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 | |
b90ab6b2 |
75 | =head1 NAME |
76 | |
77 | ptar - a tar-like program written in perl |
78 | |
79 | =head1 DESCRIPTION |
80 | |
81 | ptar is a small, tar look-alike program that uses the perl module |
82 | Archive::Tar to extract, create and list tar archives. |
83 | |
84 | =head1 SYNOPSIS |
85 | |
86 | ptar -c [-v] [-z] [-f ARCHIVE_FILE] FILE FILE ... |
87 | ptar -x [-v] [-z] [-f ARCHIVE_FILE] |
88 | ptar -t [-z] [-f ARCHIVE_FILE] |
89 | ptar -h |
90 | |
91 | =head1 OPTIONS |
92 | |
93 | x Extract from ARCHIVE_FILE |
94 | c Create ARCHIVE_FILE from FILE |
95 | t List the contents of ARCHIVE_FILE |
96 | f Name of the ARCHIVE_FILE to use. Default is './default.tar' |
97 | z Read/Write zlib compressed ARCHIVE_FILE (not always available) |
98 | v Print filenames as they are added or extraced from ARCHIVE_FILE |
99 | h Prints this help message |
100 | |
101 | =head1 SEE ALSO |
102 | |
103 | tar(1), L<Archive::Tar>. |
104 | |
105 | =cut |