Advertisement
WhiteFire_Sondergaar

tar for ComputerCraft

Apr 19th, 2013
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.62 KB | None | 0 0
  1. --[[
  2.     CCTar - the ComputerCraft archiver
  3.     Copyright (C) 2013  Yingtong Li (RunasSudo)
  4.  
  5.     This program is free software: you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation, either version 3 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17. --]]
  18.  
  19. local function readString(tarHandle, length)
  20.     local str = ""
  21.     for i = 1, length do
  22.         local byte = tarHandle.read()
  23.         if byte > 0 then
  24.             str = str .. string.char(byte)
  25.         end
  26.     end
  27.     return str
  28. end
  29.  
  30. local function skip (tarHandle, bytes)
  31.     for i = 1, bytes do
  32.         tarHandle.read()
  33.     end
  34. end
  35.  
  36. local function numChunks (length)
  37.     if length % 512 == 0 then
  38.         return math.floor(length / 512)
  39.     end
  40.     return math.floor(length / 512) + 1
  41. end
  42.  
  43. local function readTar(tarHandle, folderName)
  44.     while true do
  45.         local firstByte = tarHandle.read()
  46.         if firstByte <= 0 then
  47.             return
  48.         end
  49.        
  50.         local fileName = string.char(firstByte) .. readString(tarHandle, 99)
  51.         print("Extracting " .. fileName)
  52.        
  53.         skip (tarHandle, 8) -- Mode
  54.         skip (tarHandle, 8) -- Owner
  55.         skip (tarHandle, 8) -- Group
  56.        
  57.         local fileSizeString = readString(tarHandle, 12):gsub(" ", "")
  58.         local fileSize = tonumber(fileSizeString, 8)
  59.         --print("Size: " .. fileSize .. " bytes")
  60.        
  61.         skip (tarHandle, 12) -- Last Modified
  62.         skip (tarHandle, 8) -- Checksum
  63.         skip (tarHandle, 1) -- Link Indicator
  64.         skip (tarHandle, 100) -- Linked File
  65.        
  66.         skip (tarHandle, 512 - 257) -- Rest of header (NUL)
  67.        
  68.         local isFolder = (fileName:sub(fileName:len(), fileName:len()) == "/")
  69.         if isFolder then
  70.             fs.makeDir(fs.combine(folderName, fileName))
  71.         else       
  72.             local fHandle = fs.open(fs.combine(folderName, fileName), "wb")
  73.             for i = 1, fileSize do
  74.                 --write(string.char(tarHandle.read()))
  75.                 fHandle.write(tarHandle.read())
  76.             end
  77.             fHandle.close()
  78.         end    
  79.         skip (tarHandle, (numChunks(fileSize) * 512) - fileSize);
  80.     end
  81. end
  82.  
  83. local tArgs = {...}
  84.  
  85. local tarHandle = fs.open(fs.combine(shell.dir(), tArgs[1]), "rb")
  86. --local folderName = fs.combine(shell.dir(), tArgs[2])
  87. local folderName = shell.dir()
  88. fs.makeDir(folderName)
  89. print("Extracting...")
  90. readTar(tarHandle, folderName)
  91. print("Finished!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement