This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
MysticT's profile picture

Package Api

Started by MysticT, 26 August 2013 - 02:09 PM
MysticT #1
Posted 26 August 2013 - 04:09 PM
Hello everyone.
This is a little api I wrote that lets you save a directory to a single file. It's like a zip/rar/whatever file, but without compression (for now), so in most cases the file will be larger than the directory.
The package is a binary file, so you can include any binary files and they should pack/unpack correctly.

I wrote it while working on something bigger, but I thought that someone might find this usefull (probably not, but whatever :P/>).
So, here it is:

Download:
pastebin get gx10mzbk

Functions:

package.pack(path, outPath) -- pack directory "path" to file "outPath"
--[[
Packs a directory
Parameters:
  - path: the path to the directory to pack
  - outPath: the path to save the package file
Return Values:
  - result: boolean indicating if the operation was successful
  - error: error message in case of error
--]]

package.load(path)
--[[
Loads a package file
Parameters:
  - path: the path to the package file
Return Values:
  - entries: table containing the package entries. nil in case of error
  - error: error message in case of error
--]]

package.unpack(path, outPath)
--[[
Unpacks a package file
Parameters:
  - path: the path to the package file
  - outPath: the path to save the directory
Return Values:
  - result: boolean indicating if the operation was successful
  - error: error message in case of error
--]]

Examples:
Pack:
Spoiler

local tArgs = {...}
if #tArgs ~= 2 then
  print("Usage: pack <intput path> <output path>")
  return
end

if not package then
  os.loadAPI("package")
  if not package then
    printError("Error loading package API")
    return
  end
end

local ok, err = package.pack(tArgs[1], tArgs[2])
  if not ok then
  printError(err)
end
Unpack:
Spoiler

local tArgs = {...}
if #tArgs ~= 2 then
  print("Usage: unpack <intput path> <output path>")
  return
end

if not package then
  os.loadAPI("package")
  if not package then
    printError("Error loading package API")
    return
  end
end

local ok, err = package.unpack(tArgs[1], tArgs[2])
  if not ok then
  printError(err)
end

Any feedback and bug reports are welcome.
Mitchfizz05 #2
Posted 27 August 2013 - 09:20 AM
This is quite fancy!
I recommend that you make a packaging program (like the one in the example) and release it in the programs section.
MysticT #3
Posted 27 August 2013 - 10:40 AM
This is quite fancy!
I recommend that you make a packaging program (like the one in the example) and release it in the programs section.
Thanks.
I want to add some sort of compression before. Otherwise, there's not much use for this.
Zudo #4
Posted 27 August 2013 - 02:15 PM
This is useful, because I like APIs, and this one is good :)/>