Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- VARIABLES --
- sSeparator = "!SPLIT!" -- The string that separates stored data in an archive.
- ---------------
- -- Gets the desired function to be performed from the user.
- -- Extraction or packaging.
- function getMode()
- write('\n' .. "Extract or package?: ")
- local sMode = string.lower(read())
- if sMode == "extract" or sMode == "package" then
- return sMode
- else
- return false
- end
- end
- function getZipPath()
- local sMode = getMode()
- if sMode == "extract" then
- write("Zip to extract path: ")
- local sZipPath = read()
- if fs.exists(sZipPath) and not fs.isDir(sZipPath) then
- write('\n' .. "Extraction path: ")
- local sExtractionPath = read()
- if not fs.exists(sExtractionPath) then
- if extractZip(sZipPath, sExtractionPath) then
- print("Extraction successful.")
- else
- print("Extraction failure.")
- end
- else
- error('\n' .. "Extraction path already exists.")
- end
- else
- error('\n' .. "Zip not found.")
- end
- elseif sMode == "package" then
- write("Directory to package path: ")
- local sDirPath = read()
- if fs.isDir(sDirPath) and #(fs.list(sDirPath)) > 0 then
- write('\n' .. "Packaged zip path: ")
- local sPackagePath = read()
- if not fs.exists(sPackagePath) then
- if packageDirectory(sDirPath, sPackagePath) then
- print("Packaing successful.")
- else
- print("Packaging failure.")
- end
- else
- error("Package path already exists.")
- end
- else
- error('\n' .. "Directory not found, or insufficient file count.")
- end
- else
- error("Unrecognized mode.")
- end
- end
- -- Takes a directory, reads all of the files into a table,
- -- then writes the files into a new file (.zip) in the following format:
- -- <filename>!*S*!<fileContents>
- function packageDirectory(sDirPath, sPackagePath)
- -- Make sure that the directory exists and has files in it.
- if fs.isDir(sDirPath) and #(fs.list(sDirPath)) > 0 then
- -- Get a list of all of the files, and store their contents into a table.
- -- Zip table format: tNewZip[n] = {sName (string), sContents (string)}
- local tNewZip = {} -- The table that will store the name and contents of each file. tNewZip[n] = {sName (string), sContents (string)}
- local tFiles = fs.list(sDirPath) -- All of the files that exist within the directory to be zipped.
- local fileHandle = nil -- The file handle of the current file being zipped.
- local sFileContents = "" -- The contents of the current file being zipped.
- -- Run through all of the files in the directory and extract the contents and name into tNewZip.
- for nFileIndex, sFileName in ipairs(tFiles) do
- -- Get a handle on the current file, get its contents, then close the handle.
- fileHandle = fs.open(sDirPath .. '/' .. sFileName, 'r')
- sFileContents = fileHandle.readAll()
- fileHandle.close()
- -- Add a new entry to the zip table.
- table.insert(tNewZip, {sName = sFileName, sContents = sFileContents})
- end
- -- Now that all of the files and their respective contents have been put into a table,
- -- package the table into a .zip file.
- -- If the zip archive was generated successfuly, then return a successful package.
- if generateZipArchive(tNewZip, sPackagePath) then
- return true
- -- If the zip archive was not generated successfuly, then return a failed package.
- else
- return false
- end
- -- If the directory provided doesn't exist, or doesn't have any files to package.
- else
- return false -- Report a failure.
- end
- end
- -- Takes a zip formatted table: tZip[n] = {sName (string), sContents (string)},
- -- then puts each entry in a file with a .zip extension.
- -- The file is structured like this:
- -- <filename>!*S*!<fileContents> and so on.
- function generateZipArchive(tZip, sZipName)
- -- Make sure that the zip table given is not nil, so we won't have any exceptions.
- if tZip then
- -- Get a file handle on the zip file that we are creating.
- -- Make sure that the file doesn't already exist.
- if not fs.exists(sZipName .. '.zip') then
- local zipFileHandle = fs.open('/' .. sZipName .. '.zip' , 'w') -- Get the file handle.
- -- Validate the file handle.
- if zipFileHandle then
- local sZippedContents = "" -- The string that will contain the properly formatted zip file contents.
- -- Loop through the table, writing each entry as:
- -- <filename>!*S*!<fileContents> in the file.
- for nFileIndex, tFileEntry in ipairs(tZip) do
- -- Concatenate the file name, then the separation string, then the file contents, then the separation string again.
- sZippedContents = sZippedContents .. tFileEntry.sName .. sSeparator .. tFileEntry.sContents .. sSeparator
- end
- -- Now that we have the properly formatted zipped file contents in a string, simply write
- -- the string to the file, then close the file handle.
- zipFileHandle.write(sZippedContents)
- zipFileHandle.close()
- -- Report a successful zip generation.
- return true
- -- If the zip file handle is not valid, return a failure.
- else
- return false
- end
- -- If the file we're creating already exists, either as a file or a directory, return a failure.
- else
- return false
- end
- -- If the zip table passed is non-existant, then return a failure.
- else
- return false
- end
- end
- -- Takes a formatted, extracted zip archive and places its contents into a directory.
- function createExtratedDirectory(tZip, sDirPath)
- -- Create the directory then secure an identifier for our file handles.
- fs.makeDir(sDirPath)
- local fileHandle = nil
- -- Loop through the zip table and create a file for each entry.
- for nFileIndex, tFile in ipairs(tZip) do
- -- Get a handle on the current file, write its contents to aforementioned file, then close it.
- fileHandle = fs.open(sDirPath .. '/' .. tFile.sName, 'w')
- -- Make sure that the file handle is valid.
- if fileHandle then
- fileHandle.write(tFile.sContents)
- fileHandle.close()
- -- If the file handle is not valid, return a failed creation.
- else
- return false
- end
- end
- -- Report a successful creation.
- return true
- end
- -- Takes a string and a pattern, then returns a table that contains
- -- entries of substrings that are separated by the passed pattern.
- -- Borrowed from: http://lua-users.org/wiki/SplitJoin
- -- Compatibility: Lua-5.1
- function split(str, pat)
- local t = {} -- NOTE: use {n = 0} in Lua-5.0
- local fpat = "(.-)" .. pat
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(t,cap)
- end
- last_end = e+1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap)
- end
- return t
- end
- -- Takes a zip file and extracts its contents into a directory.
- function extractZip(sZipPath, sDirPath)
- -- Make sure that the zip given exists.
- if fs.exists(sZipPath) and not fs.isDir(sZipPath) then
- -- Make sure the directory path to extract the zip to does not exist.
- if not fs.exists(sDirPath) then
- -- Get a handle on the zip file, get its contents, then close the file handle.
- local zipFileHandle = fs.open(sZipPath, 'r')
- local sZipContents = zipFileHandle.readAll()
- zipFileHandle.close()
- -- Now that we have the contents of the zip, unformat it into a table.
- -- tUnzipped[n] = {sName (string), sContents (string)}
- local tUnzipped_Raw = split(sZipContents, sSeparator) -- The raw split of the file.
- local tUnzipped = {} -- This will be the final, formatted version of the unzipped file.
- -- Loop through the raw split zip and format it into the final table.
- -- Odd entries are the name of the file, while the even entries are the contents of the file.
- for nIndex = 1, #tUnzipped_Raw, 2 do
- table.insert(tUnzipped, {sName = tUnzipped_Raw[nIndex], sContents = tUnzipped_Raw[nIndex + 1]})
- end
- -- Now that we have the properly formatted table, create the directory.
- -- If the unzipped archive was properly put into a directory, return a successful extraction.
- if createExtratedDirectory(tUnzipped, sDirPath) then
- return true
- -- If the unzipped archive was not properly put into a directory, return a failed extraction.
- else
- return false
- end
- -- If the directory path to extract the zip already exists, return a failed extraction.
- else
- return false
- end
- -- If the zip given does not exist, or is a directory, return a failed extraction.
- else
- return false
- end
- end
- getZipPath()
Advertisement
Add Comment
Please, Sign In to add comment