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 = {}; |
178aef9a |
9 | getopts('dcvzthxf:I', $opts) or die usage(); |
291d3373 |
10 | |
11 | ### show the help message ### |
12 | die usage() if $opts->{h}; |
13 | |
14 | ### enable debugging (undocumented feature) |
178aef9a |
15 | local $Archive::Tar::DEBUG = 1 if $opts->{d}; |
16 | |
17 | ### enable insecure extracting. |
18 | local $Archive::Tar::INSECURE_EXTRACT_MODE = 1 if $opts->{I}; |
291d3373 |
19 | |
20 | ### sanity checks ### |
21 | unless ( 1 == grep { defined $opts->{$_} } qw[x t c] ) { |
22 | die "You need exactly one of 'x', 't' or 'c' options: " . usage(); |
23 | } |
24 | |
25 | my $compress = $opts->{z} ? 1 : 0; |
26 | my $verbose = $opts->{v} ? 1 : 0; |
27 | my $file = $opts->{f} ? $opts->{f} : 'default.tar'; |
28 | my $tar = Archive::Tar->new(); |
29 | |
178aef9a |
30 | |
291d3373 |
31 | if( $opts->{c} ) { |
32 | my @files; |
33 | find( sub { push @files, $File::Find::name; |
34 | print $File::Find::name.$/ if $verbose }, @ARGV ); |
b90ab6b2 |
35 | |
36 | Archive::Tar->create_archive( $file, $compress, @files ); |
291d3373 |
37 | exit; |
b90ab6b2 |
38 | } |
291d3373 |
39 | |
40 | my $tar = Archive::Tar->new($file, $compress); |
41 | |
42 | if( $opts->{t} ) { |
b90ab6b2 |
43 | print map { $_->full_path . $/ } $tar->get_files; |
291d3373 |
44 | |
b90ab6b2 |
45 | } elsif( $opts->{x} ) { |
291d3373 |
46 | print map { $_->full_path . $/ } $tar->get_files |
47 | if $verbose; |
48 | Archive::Tar->extract_archive($file, $compress); |
b90ab6b2 |
49 | } |
291d3373 |
50 | |
51 | |
52 | |
53 | sub usage { |
54 | qq[ |
b90ab6b2 |
55 | Usage: ptar -c [-v] [-z] [-f ARCHIVE_FILE] FILE FILE ... |
56 | ptar -x [-v] [-z] [-f ARCHIVE_FILE] |
57 | ptar -t [-z] [-f ARCHIVE_FILE] |
291d3373 |
58 | ptar -h |
b90ab6b2 |
59 | |
291d3373 |
60 | ptar is a small, tar look-alike program that uses the perl module |
b90ab6b2 |
61 | Archive::Tar to extract, create and list tar archives. |
62 | |
291d3373 |
63 | Options: |
64 | x Extract from ARCHIVE_FILE |
65 | c Create ARCHIVE_FILE from FILE |
66 | t List the contents of ARCHIVE_FILE |
67 | f Name of the ARCHIVE_FILE to use. Default is './default.tar' |
68 | z Read/Write zlib compressed ARCHIVE_FILE (not always available) |
69 | v Print filenames as they are added or extraced from ARCHIVE_FILE |
70 | h Prints this help message |
178aef9a |
71 | I Enable 'Insecure Extract Mode', which allows archives to extract |
72 | files outside the current working directory. (Not advised). |
291d3373 |
73 | |
74 | See Also: |
75 | tar(1) |
76 | Archive::Tar |
77 | |
78 | \n] |
79 | } |
80 | |
b90ab6b2 |
81 | =head1 NAME |
82 | |
83 | ptar - a tar-like program written in perl |
84 | |
85 | =head1 DESCRIPTION |
86 | |
87 | ptar is a small, tar look-alike program that uses the perl module |
88 | Archive::Tar to extract, create and list tar archives. |
89 | |
90 | =head1 SYNOPSIS |
91 | |
92 | ptar -c [-v] [-z] [-f ARCHIVE_FILE] FILE FILE ... |
93 | ptar -x [-v] [-z] [-f ARCHIVE_FILE] |
94 | ptar -t [-z] [-f ARCHIVE_FILE] |
95 | ptar -h |
96 | |
97 | =head1 OPTIONS |
98 | |
99 | x Extract from ARCHIVE_FILE |
100 | c Create ARCHIVE_FILE from FILE |
101 | t List the contents of ARCHIVE_FILE |
102 | f Name of the ARCHIVE_FILE to use. Default is './default.tar' |
103 | z Read/Write zlib compressed ARCHIVE_FILE (not always available) |
104 | v Print filenames as they are added or extraced from ARCHIVE_FILE |
105 | h Prints this help message |
106 | |
107 | =head1 SEE ALSO |
108 | |
109 | tar(1), L<Archive::Tar>. |
110 | |
111 | =cut |