This repository has been archived by the owner on Nov 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
test_zlib2.lua
93 lines (80 loc) · 1.67 KB
/
test_zlib2.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
local zlib = require 'zlib'
local gzip = require 'gzip'
local data = 'abcde'
local cdata = zlib.compress(data)
local udata = zlib.decompress(cdata)
print('udata=['..udata..']')
local z = zlib.inflate(cdata)
print('z', z)
for i = 1, 5 do
print(i, z:read(1))
end
z:close()
of = gzip.open('txt.gz', "wb9")
i = assert(io.open('txt', 'w'))
for _,str in ipairs{'a', 'b', 'c'} do
local s = (str:rep(10)) ,'\n'
i:write(s)
of:write(s)
end
i:close()
of:close()
i = assert(io.open('txt', 'rb')) org = i:read('*a') i:close()
i = io.open('txt.gz', 'rb')
dup = ''
o = io.open('txt.out', 'wb')
z = zlib.inflate(i)
print('z = ', z)
repeat
local l = z:read(1024)
if not l then
break
end
dup = dup .. l
o:write(l)
until false
i:close()
o:close()
z:close()
print('result:', (org == dup), org:len(), dup:len())
i = io.open('txt.gz', 'rb')
o = io.open('txt.lines.out', 'w')
cpeek = 0
ccon = 0
z = zlib.inflate({
peek = function(self, hint)
local d = i:read(hint)
if (d ~= nil) then
i:seek('cur', -d:len())
cpeek = cpeek + d:len()
end
--print('called peek with', hint, 'got', d and d:len())
return d
end,
read = function(self, consume)
--print('called read with', consume)
ccon = ccon + consume
i:read(consume)
end
})
print('z = ', z)
for line in z:lines() do
o:write(line, '\n')
end
i:close()
o:close()
z:close()
print('stats:', cpeek, ccon)
print('z = ', z)
i = io.open('txt', 'r')
o = io.open('txt-zlib.gz', 'wb')
z = zlib.deflate(o, nil, nil, 15 + 16)
for line in i:lines() do
z:write(line, '\n')
end
z:flush('finish')
z:close()
o:close()
i:close()
os.remove("txt")
os.remove("txt.gz")