Repository navigation
Expand file tree
/
Copy pathzipclean
More file actions
executable file
·40 lines (39 loc) · 1.38 KB
/
Copy pathzipclean
File metadata and controls
executable file
·40 lines (39 loc) · 1.38 KB
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
#!/usr/bin/env php
<?php
if($argc<2 || ($argv[1][0]=='-' && $argv[1]!='-d')): echo <<<EOB
Usage: $argv[0] [-d] <filename.zip>
Cleans up after an unzip has littered files all over your current directory.
It does a filesize and mtime sanity check to try to make sure you are deleting the right files
-d deletes created directories as well
EOB;
else:
$deldir = ($argv[1]=='-d') ? true:false;
$skip = true;
$fd = popen('unzip -l '.escapeshellarg(end($argv)), 'r');
while(!feof($fd)) {
$line = fgets($fd);
if(!$skip) {
if($line[0]=='-') break;
$line = preg_replace('/\s+/', ' ', trim($line));
list($size, $ts1, $ts2, $fn) = explode(' ',$line,4);
$ts = strtotime($ts1.' '.$ts2);
if(file_exists($fn)) {
$stat = stat($fn);
if(is_dir($fn)) $dirs[$fn] = strlen($fn);
else if($size == $stat['size'] && abs($ts-$stat['mtime'])<60) {
echo "Deleting $fn\n";
system("rm ".escapeshellarg($fn));
} else {
echo "Not removing $fn because size or ts doesn't match\n";
}
}
}
if($line[0] == '-') $skip = false;
}
arsort($dirs);
if(!$deldir) echo "Dirs from zip file which were not deleted:\n";
foreach($dirs as $dir=>$len) {
if($deldir) { echo "Deleting $dir\n"; system("rmdir ".escapeshellarg($dir)); }
else echo $dir."\n";
}
endif;