d5c0e9777d37876a20162e3ac92e56f8e87b46b3
[dh-make-perl] / dev / arm / libarchive-zip-perl / libarchive-zip-perl-1.18 / examples / selfex.pl
1 #/usr/bin/perl -w
2 #
3 # Shows one way to write a self-extracting archive file.
4 # This is not intended for production use, and it always extracts to a
5 # subdirectory with a fixed name.
6 # Plus, it requires Perl and A::Z to be installed first.
7 #
8 # In general, you want to provide a stub that is platform-specific.
9 # You can use 'unzipsfx' that it provided with the Info-Zip unzip program.
10 # Get this from http://www.info-zip.org .
11 #
12 # $Revision: 1.6 $
13 #
14 use strict;
15
16 use Archive::Zip;
17 use IO::File;
18
19 # Make a self-extracting Zip file.
20
21 die "usage: $0 sfxname file [...]\n" unless @ARGV > 1;
22
23 my $outputName = shift();
24
25 my $zip = Archive::Zip->new();
26
27 foreach my $file (@ARGV)
28 {
29         $zip->addFileOrDirectory($file);
30 }
31
32 my $fh = IO::File->new( $outputName, O_CREAT | O_WRONLY | O_TRUNC, 0777 )
33   or die "Can't open $outputName\: $!\n";
34 binmode($fh);
35
36 # add self-extracting Perl code
37
38 while (<DATA>)
39 {
40         $fh->print($_)
41 }
42
43 $zip->writeToFileHandle($fh);
44
45 $fh->close();
46
47 # below the __DATA__ line is the extraction stub:
48 __DATA__
49 #!/usr/local/bin/perl
50 # Self-extracting Zip file extraction stub
51 # Copyright (C) 2002 Ned Konz
52
53 use Archive::Zip qw(:ERROR_CODES);
54 use IO::File;
55 use File::Spec;
56
57 my $dir = 'extracted';
58 my $zip = Archive::Zip->new();
59 my $fh = IO::File->new($0) or die "Can't open $0\: $!\n";
60 die "Zip read error\n" unless $zip->readFromFileHandle($fh) == AZ_OK;
61
62 (mkdir($dir, 0777) or die "Can't create directory $dir\: $!\n") unless -d $dir;
63
64 for my $member ( $zip->members )
65 {
66         $member->extractToFileNamed( File::Spec->catfile($dir,$member->fileName) );
67 }
68 __DATA__