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

sfx - Self-extracting archive

Started by baturinsky, 24 November 2012 - 12:13 PM
baturinsky #1
Posted 24 November 2012 - 01:13 PM
This program makes a self-extracting archive from a directory, to make sharing by pastebin easier.

For example:

sfx disk disk.sfx
pastebin put disk.sfx

Then on some other computer on some other server:

pastebin get D0YXE0qX disk.sfx
disk.sfx disk
to get exact copy of the first disk


http://pastebin.com/rxnjypsW

Spoiler

tArgs = { ... }

if #tArgs ~= 2 then
	print( "Usage= sfx <directory> <archivename>" )
	return
end

local sfxFiles = ""
local sfxDirs = ""

root = tArgs[1] .. "/"

function serDir(path)
  print("dir  " .. root .. path)
  local files = fs.list(root .. path)
  for _,file in ipairs( files ) do
	local fpath = path .. file	
	if fs.isDir(root .. fpath) then
	  sfxDirs = sfxDirs .. "\"" .. fpath .. "\",\n"
	  serDir(fpath .. "/")
	else
	  serFile(fpath)
	end
  end  
end

function serFile(fpath)
  sleep(0.05)
  print("file " .. root .. fpath)  
  if fs.getSize(root .. fpath) > 1000000 then
	print (root .. fpath .. " is too long, skipping")
  else  
	file = fs.open(root .. fpath, "r")
	local text = file.readAll()
	file.close()
	sfxFiles = sfxFiles .. "\"" .. fpath .. "\",\n\[=====\[" .. text .. "\]=====\]" .. ",\n"
  end
end

serDir("")

local sfx = [[
local tArgs = { ... }

if #tArgs ~= 1 then
	print( "Usage= blabla.sfx <dir>" )
	return
end

local root = tArgs[1] .. "/"

local files = {
]] .. sfxFiles .. [[
}

local dirs = {]] .. sfxDirs .. [[}

if not fs.isDir(tArgs[1]) then
  fs.makeDir(tArgs[1])
end

for _,dir in ipairs(dirs) do
  local fpath = root .. dir
  print("dir " .. fpath)
  if not fs.isDir(fpath) then
	fs.makeDir(fpath)
  end
end

i = 1
while i<#files do
  sleep(0.05)
  local fpath = root .. files[i]
  local data = files[i+1]
  print("file " .. fpath)
  local file = fs.open(fpath, "w")
  file.write(data)
  file.close()
  i = i+2
end]]

local file = fs.open(tArgs[2],"w")
file.write(sfx)
file.close()
Mitchfizz05 #2
Posted 25 November 2012 - 09:59 PM
Wow, impressive. :D/>/>
bjornir90 #3
Posted 26 November 2012 - 03:05 AM
Sorry for my nuub question, but what is the difference with the native copy program ?? No offence it's just that I'm french and don't understand what this do…
Espen #4
Posted 26 November 2012 - 12:07 PM
@baturinsky: I didn't test your code, but I'm sure the code will fail at the following line:
function serDir(path)
  print("dir  " .. root .. fpath)  -- <<< This line here
  files = fs.list(root .. path)
'fpath' is not defined at that point and thus will be nil there.
And since string and nil cannot be concatenated the code will fail.

I think you probably wanted to write 'path' instead of 'fpath'?
Just wanted to let you know in case you didn't already see it yourself.
baturinsky #5
Posted 28 November 2012 - 12:19 AM
Thanks for feedback, fixed bug with fpath and updated text to make intent of program more clear.
bjornir90 #6
Posted 28 November 2012 - 04:40 AM
Thanks for feedback, fixed bug with fpath and updated text to make intent of program more clear.
Thanks you, I understand now :P/>
bjornir90 #7
Posted 29 November 2012 - 05:44 AM
Just tested out, and this is absolutely awesome !!! Good work !!!
Tiin57 #8
Posted 29 November 2012 - 10:26 AM
Excellent job here.
tommyroyall #9
Posted 30 November 2012 - 12:56 PM
Nice job man, this and a graphical file browser should be standard for CC ;)/>.
FuuuAInfiniteLoop(F.A.I.L) #10
Posted 02 December 2012 - 02:44 PM
Im making a program downloader for computercraft like cc-get and i want to know if you can give me your permission to modify your program for using it for my packages
baturinsky #11
Posted 03 December 2012 - 02:39 AM
Im making a program downloader for computercraft like cc-get and i want to know if you can give me your permission to modify your program for using it for my packages
Sure, all my CC code is BSD licensed.