-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompressor.php
122 lines (122 loc) · 3.59 KB
/
compressor.php
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/php
<?php
// Max Base - MaxBase.org
// https://github.com/BaseMax/MiniPHPArchiveCompressor
/*
Tested on: Linux base 5.3.0-40-generic
Run: $ php compressor.php
Run: $ ./compressor.php
Using: $ ./compressor.php c input.txt output.x
$ ./compressor.php d output.x input.txt
max@base:~/compress$ ./compressor.php c i.txt o.txt
Mode: c, Level: 9
Input File: "i.txt"
Output File: "o.txt"
Input file size: 4275
Total input bytes: 5
Total output bytes: 2107
Done.
max@base:~/compress$ ./compressor.php d o.txt oi.txt
Mode: d, Level: 9
Input File: "o.txt"
Output File: "oi.txt"
Input file size: 2107
Total input bytes: 5
Total output bytes: 4275
Done.
*/
function compress($string="", $level=9) {
$compressed=c($string, $level);
$compressed=c($compressed, $level);
return $compressed;
}
function decompress($string="", $level=9) {
$decompressed=d($string);
$decompressed=d($decompressed);
return $decompressed;
}
function starts($haystack, $needle) {
$length = strlen($needle);
return(substr($haystack, 0, $length) === $needle);
}
function ends($haystack, $needle) {
$length = strlen($needle);
if($length === 0) {
return true;
}
return(substr($haystack, -$length) === $needle);
}
function d($string="") {return gzinflate($string);}
function c($string="", $level=9) {return gzdeflate($string, $level);}
if($argc < 4) {
printf("mini (June 4th 2020).\n");
printf("Usage: mini [options] [mode:c or d] inputFile outputFile\n");
printf("\nModes:\n");
printf("c - Compresses file inputFile to a stream in file outputFile\n");
printf("d - Decompress stream in file inputFile to file outputFile\n");
printf("\nOptions:\n");
printf("-l[1-9] - Compression level, higher values are slower.\n");
printf("\nMax Base - MaxBase.org\n");
exit(1);
}
$i=1;
$level=9;
if(starts($argv[$i], "-l")) {
$level=mb_substr($argv[$i++], 2);
$level=trim($level);
if($level !== "" and is_numeric($level)) {
$level=(int)$l;
if(($level < 0) || ($level > 10)) {
printf("Invalid level!\n");
exit(1);
}
}
else {
printf("Invalid level format!\n");
exit(1);
}
}
$mode=$argv[$i++];
if(($mode === "c") || ($mode === "C")) {
$mode=1;
}
else if(($mode === "d") || ($mode === "D")) {
$mode=2;
}
else {
printf("Invalid mode!\n");
exit(1);
}
$input=$argv[$i++];
if(!file_exists($input)) {
printf("Failed opening input file!\n");
exit(1);
}
if(!is_file($input)) {
printf("Input file is not a file!\n");
exit(1);
}
$string=file_get_contents($input);
$output=$argv[$i++];
if(file_exists($output) and !is_writable($output)) {
printf("Cannot write output file!\n");
exit(1);
}
printf("Mode: %s, Level: %d\n", $mode === 1 ? "c" : "d", $level);
printf("Input File: \"%s\"\n", $input);
printf("Output File: \"%s\"\n", $output);
$inputSize=filesize($input);
printf("Input file size: %d\n", $inputSize);
$inputBytes=strlen($input);
printf("Total input bytes: %d\n", $inputBytes);
if($mode === 1) {
$data=compress($string, $level);
}
else if($mode === 2) {
// print $string."\n";
$data=decompress($string, $level);
}
$outputBytes=strlen($data);
printf("Total output bytes: %d\n", $outputBytes);
file_put_contents($output, $data);
printf("Done.\n");