forked from LuaDist/lzlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gzip.lua
83 lines (70 loc) · 1.79 KB
/
gzip.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
--[===================================================================[
TODO:
- add a nice header with license and stuff..
- detect plain text files
- proper testing
- improve implementation?
- check gzopen flags for consistency (ex: strategy flag)
--]===================================================================]
local io = require 'io'
local zlib = require 'zlib'
local error, assert, setmetatable, tostring = error, assert, setmetatable, tostring
local _M = {}
function _M.open(filename, mode)
mode = mode or 'r'
local r = mode:find('r', 1, true) and true
local w = mode:find('w', 1, true) and true
local level = -1
local lstart, lend = mode:find('%d')
if (lstart and lend) then
level = mode:sub(lstart, lend)
end
if (not (r or w)) then
error('file open mode must specify read or write operation')
end
local f, z
local mt = {
__index = {
read = function(self, ...)
return z:read(...)
end,
write = function(self, ...)
return z:write(...)
end,
seek = function(self, ...)
error 'seek not supported on gzip files'
end,
lines = function(self, ...)
return z:lines(...)
end,
flush = function(self, ...)
return z:flush(...) and f:flush()
end,
close = function(self, ...)
return z:close() and f:close()
end,
},
__tostring = function(self)
return 'gzip object (' .. mode .. ') [' .. tostring(z) .. '] [' .. tostring(f) .. ']'
end,
}
if r then
f = assert(io.open(filename, 'rb'))
z = assert(zlib.inflate(f))
else
f = assert(io.open(filename, 'wb'))
z = assert(zlib.deflate(f, level, nil, 15 + 16))
end
return setmetatable({}, mt)
end
function _M.lines(filename)
local gz = _M.open(filename, 'r')
return function()
local line = gz and gz:read()
if line == nil then
gz:close()
end
return line
end
end
return _M