Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Project info:
- Name: jZip
- Creator: Jesusthekiller
- Language: Lua (CC)
- Website: None
- License: GNU GPL
- License file can be fount at www.jesusthekiller.com/license-gpl.html
- Version: 1.2
- ]]--
- --[[
- Changelog:
- 1.0:
- Initial Release
- 1.1:
- jZIP API
- 1.2:
- jZIP API fix
- ]]--
- --[[
- LICENSE:
- jZip
- Copyright (c) 2013 Jesusthekiller
- 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 usage()
- print("Usage:")
- print(" zip <u|unzip> <zip file>")
- print(" zip <z|zip> <name> <output file> <file 1> [file 2 [...]]")
- print(" zip loadapi")
- print(" It will load zip API - use shell.run('zip', 'loadapi') instead of os.loadAPI('zip').")
- end
- -- API:
- local function decode(text)
- local s = ""
- for k, char in ipairs({ text:byte(1, #text) }) do
- s = s..string.char(char+8)
- end
- return s
- end
- local function code(text)
- local s = ""
- for k, char in ipairs({ text:byte(1, #text) }) do
- s = s..string.char(char-8)
- end
- return s
- end
- local oerror = error
- local function error(str, mode)
- return oerror("[ZIP API] "..str, mode)
- end
- local function getDir(path)
- local dir, file, tab = "", "", {}
- for t in path:gmatch("[^/]+") do
- table.insert(tab, t)
- end
- file = table.remove(tab, #tab)
- for k, v in ipairs(tab) do
- dir = dir.."/"..v
- end
- return file, (dir == "") and "/" or dir
- end
- local function confirm(text)
- print(text)
- local e, k
- repeat
- e, k = os.pullEvent("key")
- until k == keys.y or k == keys.n
- coroutine.yield()
- return (k == keys.y) and true or false
- end
- local function unzip(file)
- local f = fs.open(file, "r") or error("File '"..file.."' does not exist!", 0)
- -- Read file
- local c = f.readAll()
- -- Close
- f.close()
- -- Split into table and decode
- local t = {}
- for token in decode(c):gmatch("[^\n]+") do
- table.insert(t, token)
- end
- -- Check if it is zip file
- h = table.remove(t, 1)
- if h ~= "`ZIP`" then
- error("File is not ZIP file or it is corrupted", 0)
- end
- -- Print name
- print("Unpacking "..tostring(table.remove(t, 1)))
- -- Loop unpacking process
- while true do
- -- Raw full path
- local rawfullpath = table.remove(t, 1)
- -- Break if end of file
- if not rawfullpath then
- break
- end
- -- Get and print file path
- local fullpath = shell.dir().."/"..rawfullpath
- print(" File "..fullpath)
- -- Resolve path
- local file, dir = getDir(fullpath)
- -- Create dir
- if fs.exists(dir) and not fs.isDir(dir) then
- if not confirm("File "..dir.." exists. Overwrite? [y/n]") then error("This file has to be overwritten to unpack this zip!", 0) end
- end
- if not fs.exists(dir) then
- fs.makeDir(dir)
- end
- -- Open file
- if fs.exists(fullpath) then
- if not confirm("File "..fullpath.." exists. Overwrite? [y/n]") then error("This file has to be overwritten to unpack this zip!", 0) end
- fs.delete(fullpath) -- It it's dir
- end
- local f = fs.open(fullpath, "w")
- -- Unpack file
- local line = table.remove(t, 1)
- while line ~= "`STOP`" and line do
- f.writeLine(line)
- line = table.remove(t, 1)
- end
- f.close()
- end
- print("Done!")
- end
- local function fileRead(path)
- local f = fs.open(path, "r") or error("Can not open file "..path, 0)
- local s = f.readAll()
- f.close()
- return code(s)
- end
- local function file(fout, path, out)
- -- Code write func
- local function codeWrite(str)
- fout.write(code(str)..string.char(2))
- end
- if fs.exists(path) then
- if fs.isDir(path) and (not string.find(path, "/rom")) then
- -- Loop trough all dirs
- for k, v in ipairs(fs.list(path)) do
- file(fout, path.."/"..v, out)
- end
- elseif (not string.find(path, "/"..out)) and (not string.find(path, "/rom")) then
- print(" Doing "..path)
- codeWrite(path)
- fout.write(fileRead(path)..string.char(2))
- codeWrite("`STOP`")
- print(" File "..path.." done!")
- else
- print("###WARNING### TRIED TO COMPRESS OUTPUT/BANNED FILE/FOLDER (?) - "..path)
- end
- else
- error("File "..path.." does not exist!", 0)
- end
- end
- local function fzip(...)
- -- Error checks
- local pars = {...}
- local name = table.remove(pars, 1) or error("No name specified!", 0)
- local out = table.remove(pars, 1) or error("No output file specified!", 0)
- if fs.exists(out) then
- if not confirm("File "..out.." exists. Overwrite? [y/n]") then
- return
- end
- fs.delete(out)
- end
- -- Open out file
- local f = fs.open(out, "w")
- -- Code write func
- local function codeWrite(str)
- f.write(code(str)..string.char(2))
- end
- -- Write headers
- codeWrite("`ZIP`")
- codeWrite(name)
- for k, v in ipairs(pars) do
- local fullpath = shell.dir().."/"..v or error("It should have not happened...", 0)
- if fs.exists(fullpath) then
- file(f, fullpath, out)
- else
- f.close()
- error("File "..fullpath.." does not exist!", 0)
- end
- end
- f.close()
- print("Done!")
- end
- local args = {...}
- args[1] = args[1] or ""
- args[1] = args[1]:lower()
- if args[1] == "u" or args[1] == "unzip" then
- assert(args[2], "No file specified!")
- unzip(args[2])
- elseif args[1] == "z" or args[1] == "zip" then
- table.remove(args, 1)
- fzip(unpack(args))
- elseif args[1] == "loadapi" then
- -- Globals
- zip = {}
- zip.zip = fzip
- zip.unzip = unzip
- else
- usage()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement