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

[SOLVED] Problems with archiving API

Started by Windows10User, 28 April 2018 - 02:21 PM
Windows10User #1
Posted 28 April 2018 - 04:21 PM
So, for the love of God, I made a decision I never really thought I'd make: making an archiving API (for folders and folders ONLY). Did the basic compression script. Now, the API is supposed to compress in the shape of a table like this (it's serialized):


{
   files = {
	  "1.lua",
	  "2.lua",
	  "3.lua",
   },

   filedata = {
	  "1.lua" = "file 1",
	  "2.lua" = "file 2",
	  "3.lua" = "file 3",
   },
}

stored in an "archive" (just a file containing this table), so I did it. Trying to compress a folder w/ the exact same files, I get this error: "compact:19: attempt to index ? (a nil value)" when inserting the file data into the "filedata" subtable. I inserted a print(textutils.serialize(archive)) (serializes the archive table) and read() (so the code continues execution ONLY when I press enter) to make sure the table is in the right shape for the job. This is the output:


{
   files = {
	  "1.lua",
   },
   filedata = {
	  "1.lua",
   },
}

which looks OK imo, so idk why is it not working. Anyway, the compression function:

function compress(dir, archive)
   if fs.isDir(dir) then
	  filelist = fs.list(dir)
	  local archive = {
		 files = {

		 },

		 filedata = {

		 }
	  }
	  for i=1, #filelist do
		 local filehandle = fs.open(filelist[i], "r")
		 table.insert(archive.files, filelist[i])
		 table.insert(archive.filedata, filelist[i])
		 print(textutils.serialize(archive))
		 read()
		 archive.filedata.filelist[i] = filehandle.readAll() --the erroring line
		 filehandle.close()
	  end
	  local fArchive = fs.open(archive, "w")
	  fArchive.write(textutils.serialize(archive))
	  fArchive.close()
	  return true
   else
	  return false
   end
end

Any help is appreciated!
Edited on 01 May 2018 - 08:48 AM
SquidDev #2
Posted 28 April 2018 - 04:33 PM
You've currently got this:

table.insert(archive.filedata, filelist[i])
archive.filedata.filelist[i] = filehandle.readAll() --the erroring line

This effectively takes the file data, converts it into { "1.lua" }, and then attempts to index the filelist field (which doesn't exist, hence the later error). You instead want to do this:

archive.filedata[filelist[i]] = filehandle.readAll() --the erroring line
Notice we've removed the table.insert call and put extra [] around the filedata access - meaning you're indexing on the the filename instead.
Windows10User #3
Posted 28 April 2018 - 04:47 PM
Doesn't work. Same error but on line 16.

Updated code: (btw had no time 2 indent so sry)

function compress(dir, archive)
if fs.isDir(dir) then
filelist = fs.list(dir)
local archive = {
files = {

},

filedata = {

}
}
for i=1, #filelist do
local filehandle = fs.open(filelist[i], "r")
table.insert(archive.files, filelist[i])
archive.filedata[filelist[i]] = filehandle.readAll() --the erroring line
filehandle.close()
end
local fArchive = fs.open(archive, "w")
fArchive.write(textutils.serialize(archive))
fArchive.close()
return true
else
return false
end
end
Windows10User #4
Posted 28 April 2018 - 06:06 PM
BTW, tried putting the filehandle.readAll() call into a variable, and it's a nil. So, we have the REAL problem…

EDIT: Found it out, the paths given by fs.list were relative… A dir.."/"..filelist fixed the problem.

EDIT 2: OMG, I completed the API! Both compression and decompression do their thing!
Edited on 01 May 2018 - 08:47 AM