jesusthekiller

jZip one download

Jun 24th, 2013
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.78 KB | None | 0 0
  1. local parta = [[
  2. if not http then
  3.     error("HTTP required!", 0)
  4. end
  5.  
  6. local function decode(text)
  7.     local s = ""
  8.     for k, char in ipairs({ text:byte(1, #text) }) do
  9.         s = s..string.char(char+8)
  10.     end
  11.     return s
  12. end
  13.  
  14. local oerror = error
  15. local function error(str, mode)
  16.     return oerror("[ZIP API] "..str, mode)
  17. end
  18.  
  19. local function getDir(path)
  20.     local dir, file, tab = "", "", {}
  21.    
  22.     for t in path:gmatch("[^/]+") do
  23.         table.insert(tab, t)
  24.     end
  25.    
  26.     file = table.remove(tab, #tab)
  27.    
  28.     for k, v in ipairs(tab) do
  29.         dir = dir.."/"..v
  30.     end
  31.    
  32.     return file, (dir == "") and "/" or dir
  33. end
  34.  
  35. local function confirm(text)
  36.     print(text)
  37.    
  38.     local e, k
  39.     repeat
  40.         e, k = os.pullEvent("key")
  41.     until k == keys.y or k == keys.n
  42.    
  43.     coroutine.yield()
  44.    
  45.     return (k == keys.y) and true or false
  46. end
  47.  
  48. local function unzip(file)
  49.     local f = fs.open(file, "r") or error("File '"..file.."' does not exist!", 0)
  50.    
  51.     -- Read file
  52.     local c = f.readAll()
  53.    
  54.     -- Close
  55.     f.close()
  56.    
  57.     -- Split into table and decode
  58.     local t = {}
  59.     for token in decode(c):gmatch("[^\n]+") do
  60.         table.insert(t, token)
  61.     end
  62.    
  63.     -- Check if it is zip file
  64.     h = table.remove(t, 1)
  65.     if h ~= "`ZIP`" then
  66.         error("File is not ZIP file or it is corrupted", 0)
  67.     end
  68.    
  69.     -- Print name
  70.     print("Unpacking "..tostring(table.remove(t, 1)))
  71.    
  72.     -- Loop unpacking process
  73.     while true do
  74.         -- Raw full path
  75.         local rawfullpath = table.remove(t, 1)
  76.        
  77.         -- Break if end of file
  78.         if not rawfullpath then
  79.             break
  80.         end
  81.        
  82.         -- Get and print file path
  83.         local fullpath = shell.dir().."/"..rawfullpath
  84.         print("  File "..fullpath)
  85.        
  86.         -- Resolve path
  87.         local file, dir = getDir(fullpath)
  88.        
  89.         -- Create dir
  90.         if fs.exists(dir) and not fs.isDir(dir) then
  91.             if not confirm("File "..dir.." exists. Overwrite? [y/n]") then error("This file has to be overwritten to unpack this zip!", 0) end
  92.         end
  93.        
  94.         if not fs.exists(dir) then
  95.             fs.makeDir(dir)
  96.         end
  97.        
  98.         -- Open file
  99.         if fs.exists(fullpath) then
  100.             if not confirm("File "..fullpath.." exists. Overwrite? [y/n]") then error("This file has to be overwritten to unpack this zip!", 0) end
  101.             fs.delete(fullpath) -- It it's dir
  102.         end
  103.        
  104.         local f = fs.open(fullpath, "w")
  105.        
  106.         -- Unpack file
  107.         local line = table.remove(t, 1)
  108.         while line ~= "`STOP`" and line do
  109.             f.writeLine(line)
  110.             line = table.remove(t, 1)
  111.         end
  112.        
  113.         f.close()
  114.     end
  115.    
  116.     print("Done!")
  117. end
  118. ]]
  119.  
  120. local partb = [[
  121. unzip("tmp_install_zip")
  122. ]]
  123.  
  124. local args = {...}
  125.  
  126. if #args ~= 2 then
  127.     print("Usage: ziptoexe <zip> <output>")
  128.     return
  129. end
  130.  
  131. if not http then
  132.     error("HTTP API required!", 0)
  133. end
  134.  
  135. -- COPY FROM PASTEBIN PART
  136.     -- Upload a file to pastebin.com
  137.     -- Determine file to upload
  138.     local sCode
  139.     local sFile = args[1]
  140.     local sPath = shell.resolve( sFile )
  141.     if not fs.exists( sPath ) or fs.isDir( sPath ) then
  142.         print( "No such file" )
  143.         return
  144.     end
  145.    
  146.     -- Read in the file
  147.     local sName = fs.getName( sPath )
  148.     local file = fs.open( sPath, "r" )
  149.     local sText = file.readAll()
  150.     file.close()
  151.    
  152.     -- POST the contents to pastebin
  153.     print( "Uploading zip..." )
  154.     local key = "0ec2eb25b6166c0c27a394ae118ad829"
  155.     local response = http.post(
  156.         "http://pastebin.com/api/api_post.php",
  157.         "api_option=paste&"..
  158.         "api_dev_key="..key.."&"..
  159.         "api_paste_format=lua&"..
  160.         "api_paste_name="..textutils.urlEncode(sName).."&"..
  161.         "api_paste_code="..textutils.urlEncode(sText)
  162.         )
  163.    
  164.     if response then
  165.         print( "[UPLOAD] Success." )
  166.        
  167.         local sResponse = response.readAll()
  168.         response.close()
  169.                
  170.         sCode = string.match( sResponse, "[^/]+$" )
  171.  
  172.     else
  173.         print( "[UPLOAD] Failed." )
  174.         return
  175.     end
  176. -- END OF IT
  177.  
  178. local f = fs.open(args[2], "w")
  179.  
  180. f.writeLine(parta)
  181. f.writeLine("shell.run('pastebin', 'get', '"..sCode.."', 'tmp_install_zip')")
  182. f.writeLine(partb)
  183. f.close()
  184.  
  185. print("Done!")
Add Comment
Please, Sign In to add comment