我正在尝试将压缩字符串保存到文件中并稍后加载它们以在游戏中使用。加载数据备份以供使用时,我不断收到“in 'finish': buffer error”错误。我想出了这个:
require "zlib"
def deflate(string)
zipper = Zlib::Deflate.new
data = zipper.deflate(string, Zlib::FINISH)
end
def inflate(string)
zstream = Zlib::Inflate.new
buf = zstream.inflate(string)
zstream.finish
zstream.close
buf
end
setting = ["nothing","nada","nope"]
taggedskills = ["nothing","nada","nope","nuhuh"]
File.open('testzip.txt','wb') do |w|
w.write(deflate("hello world")+"\n")
w.write(deflate("goodbye world")+"\n")
w.write(deflate("etc")+"\n")
w.write(deflate("etc")+"\n")
w.write(deflate("Setting: name "+setting[0]+" set"+(setting[1].class == String ? "str" : "num")+" "+setting[1].to_s)+"\n")
w.write(deflate("Taggedskill: "+taggedskills[0]+" "+taggedskills[1]+" "+taggedskills[2]+" "+taggedskills[3])+"\n")
w.write(deflate("etc")+"\n")
end
File.open('testzip.txt','rb') do |file|
file.each do |line|
p inflate(line)
end
end
它在“Taggedskill:”点抛出错误。我不知道它是什么,但是尝试将其更改为“Skilltag:”、“Skillt:”等会继续引发缓冲区错误,而“Setting:”或“Thing:”之类的东西可以正常工作,而将设置行更改为“Taggedskill:”继续正常工作。这里发生了什么?