Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- CCTar - the ComputerCraft archiver
- Copyright (C) 2013 Yingtong Li (RunasSudo)
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- --]]
- local function readString(tarHandle, length)
- local str = ""
- for i = 1, length do
- local byte = tarHandle.read()
- if byte > 0 then
- str = str .. string.char(byte)
- end
- end
- return str
- end
- local function skip (tarHandle, bytes)
- for i = 1, bytes do
- tarHandle.read()
- end
- end
- local function numChunks (length)
- if length % 512 == 0 then
- return math.floor(length / 512)
- end
- return math.floor(length / 512) + 1
- end
- local function readTar(tarHandle, folderName)
- while true do
- local firstByte = tarHandle.read()
- if firstByte <= 0 then
- return
- end
- local fileName = string.char(firstByte) .. readString(tarHandle, 99)
- print("Extracting " .. fileName)
- skip (tarHandle, 8) -- Mode
- skip (tarHandle, 8) -- Owner
- skip (tarHandle, 8) -- Group
- local fileSizeString = readString(tarHandle, 12):gsub(" ", "")
- local fileSize = tonumber(fileSizeString, 8)
- --print("Size: " .. fileSize .. " bytes")
- skip (tarHandle, 12) -- Last Modified
- skip (tarHandle, 8) -- Checksum
- skip (tarHandle, 1) -- Link Indicator
- skip (tarHandle, 100) -- Linked File
- skip (tarHandle, 512 - 257) -- Rest of header (NUL)
- local isFolder = (fileName:sub(fileName:len(), fileName:len()) == "/")
- if isFolder then
- fs.makeDir(fs.combine(folderName, fileName))
- else
- local fHandle = fs.open(fs.combine(folderName, fileName), "wb")
- for i = 1, fileSize do
- --write(string.char(tarHandle.read()))
- fHandle.write(tarHandle.read())
- end
- fHandle.close()
- end
- skip (tarHandle, (numChunks(fileSize) * 512) - fileSize);
- end
- end
- local tArgs = {...}
- local tarHandle = fs.open(fs.combine(shell.dir(), tArgs[1]), "rb")
- --local folderName = fs.combine(shell.dir(), tArgs[2])
- local folderName = shell.dir()
- fs.makeDir(folderName)
- print("Extracting...")
- readTar(tarHandle, folderName)
- print("Finished!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement