Jump to content




[SOLVED] Problems with archiving API

api

3 replies to this topic

Poll: "Compact" API Poll

Should I release Compact?

You cannot see the results of the poll until you have voted. Please login and cast your vote to see the results of this poll.
Vote Guests cannot vote

#1 Windows10User

  • Members
  • 62 posts
  • LocationC:\Minecraft\saves\stuff\computer

Posted 28 April 2018 - 03: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 by Windows10User, 01 May 2018 - 09:48 AM.


#2 SquidDev

    Frickin' laser beams

  • Members
  • 1,404 posts
  • LocationDoes anyone put something serious here?

Posted 28 April 2018 - 03: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.

#3 Windows10User

  • Members
  • 62 posts
  • LocationC:\Minecraft\saves\stuff\computer

Posted 28 April 2018 - 03: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


#4 Windows10User

  • Members
  • 62 posts
  • LocationC:\Minecraft\saves\stuff\computer

Posted 28 April 2018 - 05: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[i] fixed the problem.

EDIT 2: OMG, I completed the API! Both compression and decompression do their thing!

Edited by Windows10User, 01 May 2018 - 09:47 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users