-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompress-static.pl
executable file
·54 lines (48 loc) · 1.17 KB
/
compress-static.pl
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
#!/usr/local/bin/perl
use warnings;
use strict;
# Set to a true value to print messages as it operates.
my $verbose = 1;
use File::Find;
# Find all the files under the current directory.
find (\& compress, ("."));
exit;
# Compress files which match a particular pattern.
sub compress
{
# See if the file name ends in ".html", ".css", etc.
if (! /(.*\.(?:html|css|js|txt|pl|c|pod)$)/) {
if ($verbose) {
print "Rejecting $_\n";
}
# We don't need to compress this file (for example image files).
return;
}
my $gz = "$_.gz";
if (-f $gz) {
# A compressed version of this file exists.
if (mdate ($_) <= mdate ($gz)) {
if ($verbose) {
print "$_ is older than $gz: don't need to compress it.\n";
}
goto delete_file;
}
}
if ($verbose) {
print "Compressing $_\n";
}
system ("gzip --best --force $_");
delete_file:
# Delete the uncompressed version of the file.
if ($verbose) {
print "Deleting $_\n";
}
unlink $_ or warn "Could not remove $_: $!";
}
# Returns the last modification date of the file in epoch time.
sub mdate
{
my ($file) = @_;
my @stat = stat $file;
return $stat[9];
}