Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local fls = require("filesystem")
- local shl = require("shell")
- local b32 = require("bit32")
- local args = shl.parse(...)
- frompath, topath = args[1], args[2]
- VALUE = 0 LEN = 0
- function AddBite(value, len)
- VALUE = b32.lshift(VALUE, len) + value
- LEN = LEN + len
- while LEN > 7 do
- file:write(string.char(b32.rshift(VALUE, LEN - 8)))
- VALUE = VALUE % (2 ^ (LEN - 8))
- LEN = LEN - 8
- end
- end
- function inflate()
- OUTPUT = {}
- local dict = {}
- for i=0, 255 do
- dict[string.char(i)] = i
- end
- INDEX = 255
- file = io.open(frompath, "r")
- STR = file:read("*a")
- file:close()
- file = io.open(topath, "wb")
- word = STR:sub(1, 1)
- for i=2, #STR do
- if dict[word] == nil then
- OUTPUT[#OUTPUT + 1] = dict[word:sub(1, -2)]
- AddBite(OUTPUT[#OUTPUT], math.ceil(math.log(INDEX + 1, 2)))
- dict[word] = INDEX + 1
- INDEX = INDEX + 1
- word = word:sub(-1)..STR:sub(i, i)
- else
- word = word..STR:sub(i, i)
- end
- end
- if dict[word] == nil then
- OUTPUT[#OUTPUT + 1] = dict[word:sub(1, -2)]
- AddBite(OUTPUT[#OUTPUT], math.ceil(math.log(INDEX + 1, 2)))
- dict[word] = INDEX + 1
- INDEX = INDEX + 1
- word = word:sub(-1)
- end
- OUTPUT[#OUTPUT + 1] = dict[word]
- AddBite(OUTPUT[#OUTPUT], math.ceil(math.log(INDEX + 1, 2)))
- if LEN ~= 0 then
- file:write(string.char(b32.lshift(VALUE, 8 - LEN)))
- end
- file:close()
- end
- function PutBite() local len = math.ceil(math.log(INDEX, 2))
- while LEN < len do
- if POS > #STR1 then return false
- end
- VALUE = VALUE * 256 + STR1:byte(POS)
- POS = POS + 1
- LEN = LEN + 8
- end
- LEN = LEN - len
- local tmp = b32.rshift(VALUE, LEN)
- VALUE = VALUE % (2 ^ LEN)
- return tmp
- end
- function deflate()
- dict1 = {}
- for i=0, 255 do
- dict1[i] = string.char(i)
- end
- INDEX = 256
- INPUT = ""
- VALUE, POS, LEN = 0, 1, 0
- file = io.open(topath, "rb")
- STR1 = file:read("*a")
- file:close()
- local TEMP = PutBite()
- INPUT = dict1[TEMP]
- dict1[INDEX] = dict1[TEMP]
- INDEX = INDEX + 1
- i = 2
- while true do
- TEMP = PutBite()
- if TEMP == false then break end
- dict1[INDEX - 1] = dict1[INDEX - 1]..dict1[TEMP]:sub(1, 1)
- INPUT=INPUT..dict1[TEMP]
- dict1[INDEX]=dict1[TEMP]
- INDEX = INDEX + 1
- i = i + 1
- end
- -- INPUT = INPUT..dict1[INDEX - 1]
- end
- inflate()
- deflate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement