9b2901b5c6e883ba3fdfbc622ee8abe172c22a83
[p5sagit/p5-mst-13.2.git] / lib / Archive / Tar / bin / ptar
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:I', $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 ### enable insecure extracting.
18 local $Archive::Tar::INSECURE_EXTRACT_MODE  = 1 if $opts->{I};
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
30
31 if( $opts->{c} ) {
32     my @files;
33     find( sub { push @files, $File::Find::name;
34                 print $File::Find::name.$/ if $verbose }, @ARGV );
35
36     Archive::Tar->create_archive( $file, $compress, @files );
37     exit;
38 }
39
40 my $tar = Archive::Tar->new($file, $compress);
41
42 if( $opts->{t} ) {
43     print map { $_->full_path . $/ } $tar->get_files;
44
45 } elsif( $opts->{x} ) {
46     print map { $_->full_path . $/ } $tar->get_files
47         if $verbose;
48     Archive::Tar->extract_archive($file, $compress);
49 }
50
51
52
53 sub usage {
54     qq[
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]
58         ptar -h
59
60     ptar is a small, tar look-alike program that uses the perl module
61     Archive::Tar to extract, create and list tar archives.
62
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
71     I   Enable 'Insecure Extract Mode', which allows archives to extract
72         files outside the current working directory. (Not advised).
73
74 See Also:
75     tar(1)
76     Archive::Tar
77
78     \n]
79 }
80
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