-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.lua
77 lines (64 loc) · 1.57 KB
/
test.lua
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
local brotli = require "brotli"
local bit = require "bit"
local buffer_size = 2 ^ 16
local f_name = "test.css"
local f = io.open(f_name)
local f_size = f:seek("end")
f:close()
local f = io.open(f_name)
if not f then
error("no such file")
end
local contents = string.rep("0", f_size)
local compressor = brotli.compressor:new({ quality = 11 })
local result = ""
local cursor = 0
local i = 1
while true do
local chunk = f:read(buffer_size)
-- local chunk = string.sub(contents, cursor, cursor + buffer_size)
local compressed_chunk
if not chunk then
compressed_chunk = compressor:finish()
else
compressed_chunk = compressor:compress(chunk)
end
result = result .. compressed_chunk
if not chunk then
break
end
print(i, cursor, "/", f_size)
if #compressed_chunk > 0 then
print(cursor, #compressed_chunk)
end
cursor = cursor + #chunk
i = i + 1
end
print("#result: ", #result)
f:close()
local o = io.open(f_name .. ".br", "wb")
if not o then
error("no such file")
end
o:write(result)
o:close()
-- print(brotli:decompress(result))
-- os.execute("sleep 5")
local o = io.open(f_name .. ".br", "rb")
local decompressor = brotli.decompressor:new()
result = ""
while true do
local compressed_chunk = o:read(buffer_size)
local chunk
if compressed_chunk then
chunk = decompressor:decompress(compressed_chunk)
else
chunk = decompressor:finish()
end
result = result .. chunk
if not compressed_chunk then
break
end
end
o:close()
print(#result)