|
1 function gzip_file(infile_fn, outfile_fn) |
|
2 local infile, err = io.open(infile_fn); |
|
3 if not infile then |
|
4 print_err("Can't open input file for reading: "..tostring(err)); |
|
5 return; |
|
6 end |
|
7 |
|
8 local outfile, err = io.open(outfile_fn..".gzipped", "w+"); |
|
9 if not outfile then |
|
10 print_err("Can't open output file for writing: "..tostring(err)); |
|
11 return; |
|
12 end |
|
13 |
|
14 local data = infile:read("*a"); |
|
15 infile:close(); |
|
16 |
|
17 local shebang, newdata = data:match("^(#.-\n)(.+)$"); |
|
18 local code = newdata or data; |
|
19 if shebang then |
|
20 outfile:write(shebang) |
|
21 end |
|
22 |
|
23 local compressed = io.popen("gzip -c '"..infile_fn.."'"); |
|
24 code = compressed:read("*a"); |
|
25 |
|
26 local maxequals = 0; |
|
27 code:gsub("(=+)", function (equals_string) maxequals = math.max(maxequals, #equals_string); end); |
|
28 |
|
29 outfile:write(require_resource "gunzip.lua"); |
|
30 |
|
31 outfile:write[[function _gunzip(data) local uncompressed = {}; |
|
32 gunzip{input=data,output=function(b) table.insert(uncompressed, string.char(b)) end}; |
|
33 return table.concat(uncompressed, ""); end ]]; |
|
34 |
|
35 outfile:write [[return assert(loadstring(_gunzip]] |
|
36 outfile:write(string.format("%q", code)); |
|
37 --outfile:write("[", string.rep("=", maxequals+1), "[", code, "]", string.rep("=", maxequals+1), "]"); |
|
38 outfile:write(", '@", outfile_fn,"'))()"); |
|
39 outfile:close(); |
|
40 os.rename(outfile_fn..".gzipped", outfile_fn); |
|
41 end |
|
42 |
|
43 if opts.gzip then |
|
44 print_info("Gzipping "..out_fn.."..."); |
|
45 gzip_file(out_fn, out_fn); |
|
46 print_info("OK!"); |
|
47 end |