summaryrefslogtreecommitdiff
path: root/tests/archive.pl
blob: 0a03b23efc28b841b95cd6a9c5096d5d28a79e9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/perl

use Archive::Tar;
use Archive::Zip;

my $target = shift @ARGV;
my @sources = @ARGV;

my $basedir = ($target =~ /^(.*)(\.zip|\.tar.gz|\.tgz)$/) ? "$1/" : '';

my $zip = $target =~ /\.zip$/;
my $arch = $zip ? Archive::Zip->new : Archive::Tar->new;

for $source (sort {$a cmp $b} @sources)
{
	my $contents = &readfile_contents($source);
	my $meta = &readfile_meta($source);
	my $file = $basedir . $source;

    if (-T $source)
    {
        # convert all newlines to Unix format
        $contents =~ s/\r//g;

        if ($zip)
        {
            # convert all newlines to Windows format for .zip distribution
            $contents =~ s/\n/\r\n/g;
        }
    }

	if ($zip)
	{
		my $path = $file;
		$arch->addDirectory($path) if $path =~ s/\/[^\/]+$/\// && !defined($arch->memberNamed($path));

		my $member = $arch->addString($contents, $file);

		$member->desiredCompressionMethod(COMPRESSION_DEFLATED);
		$member->desiredCompressionLevel(9);

		$member->setLastModFileDateTimeFromUnix($$meta{mtime});
	}
	else
	{
		$arch->add_data($file, $contents, $meta);
	}
}

$zip ? $arch->overwriteAs($target) : $arch->write($target, 9);

sub readfile_contents
{
	my $file = shift;

	open FILE, $file or die "Can't open $file: $!";
	binmode FILE;
	my @contents = <FILE>;
	close FILE;

	return join('', @contents);
}

sub readfile_meta
{
	my $file = shift;

    my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat($file);

	return {mtime => $mtime};
}