Advertisement
MeXaN1cK

WinRar(Alexc)

Mar 23rd, 2017
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.10 KB | None | 0 0
  1. local serialization = require('serialization')
  2. local c = require("computer")
  3. local cmp = require('component')
  4. local fs = require("filesystem")
  5. local bit32 = require("bit32")
  6. local data = cmp.data
  7. local term = require("term")
  8. local unicode = require('unicode')
  9. local shell = require("shell")
  10. local WinRar = {}
  11.  
  12. function Integer_to_Byte(integer, byte)
  13.   local str = ""
  14.   for i = 1, byte do
  15.     str = string.char(integer % 256)..str
  16.     integer = bit32.rshift(integer, 8)
  17.   end
  18.   return str
  19. end
  20.  
  21. function Byte_to_Integer(str)
  22.   local integer = 0
  23.   for i=1, string.len(str) do
  24.     integer = bit32.lshift(integer, 8)  + string.byte(str, i)
  25.   end
  26.   return integer
  27. end
  28.  
  29. function WinRar.deflate(arr, limit)
  30.   local lsegment = math.floor(#arr / limit)
  31.   local out = ""
  32.   local bgn = 1
  33.   local tmp
  34.   for i = 1, lsegment do
  35.     str = string.sub(arr, bgn, bgn+limit-1)
  36.     tmp = data.deflate(str)
  37.     while tmp == nil do tmp = data.deflate(str) end
  38.     out = out..Integer_to_Byte(#tmp, 2)..tmp
  39.     bgn = bgn + limit
  40.   end
  41.   if bgn <= #arr then
  42.     str = string.sub(arr, bgn, #arr)
  43.     tmp = data.deflate(str)
  44.     while tmp == nil do tmp = data.deflate(str) end
  45.     out = out..Integer_to_Byte(#tmp, 2)..tmp
  46.   end
  47.   return out
  48. end
  49.  
  50. function WinRar.inflate(arr)
  51.   local left = 1
  52.   local right = 1
  53.   local str = ""
  54.   local tmp
  55.   local lenght
  56.   local out
  57.   while true do
  58.     lenght = Byte_to_Integer(string.sub(arr, right, right+1))
  59.     left = right + 2
  60.     right = left + lenght
  61.     out = string.sub(arr, left, right-1)
  62.     tmp = data.inflate(out)
  63.     while tmp == nil do tmp = data.inflate(out) end
  64.     str = str..tmp
  65.     if right > #arr then break end
  66.   end
  67.   return str
  68. end
  69.  
  70. function WinRar.compress(filename, newfilename, limit)
  71.   f2 = io.open(newfilename, "wb")
  72.   f1 = io.open(filename, "rb")
  73.   f2:write(WinRar.deflate(f1:read("*a") ,limit))
  74.   f1:close()
  75.   f2:close()
  76. end
  77.  
  78. function WinRar.decompress(filename, newfilename)
  79.   f2 = io.open(newfilename, "wb")
  80.   f1 = io.open(filename, "rb")
  81.   f2:write(WinRar.inflate(f1:read("*a")))
  82.   f1:close()
  83.   f2:close()  
  84. end
  85.  
  86. return WinRar
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement