开发者

Ruby zlib::Gzip not working properly

I was in the middle of creating an inport/export system that could encode a costume data structure in an xml tree and then read it back and recreate the object. I got the xml part to work fine but when I discovered that the xml file was 1.5mb when the original ruby::Marshal file was only 105kb I decided that it would be a good idea to compress the file. so I did this

require "rexml/document"
require "zlib"
include REXML

tilesetfile = File.new( "tilesets.rmpy", "w+" )
buffer = ""
tilesetgz = Zlib::GzipWriter.new(tilesetfile)
puts "Compressing output for: tilesets.rxdata ..."
tilesetdoc.write(buffer, 0)
tilesetgz.write(buffer)
tilesetgz.close

then I tried to get the buffer string back so that I could phrase it as xml again like so

require "rexml/document"
require "zlib"
include REXML

tilesetfile = File.open("tilesets.rmpy", "r")
tilesetgz = Zlib::GzipReader.new(tilesetfile)
testfile = File.new("importtest.txt", "w")
tilesetdoc = Document.new tilesetgz

it should be noted that neither of these spinets contains the entire system, just the require header and the last few lines that do the compression

but I get a phrasing error because the xml document has bee corrupted some how

this is the out put of the import scrupt before I Gziped it (tracated of course the file is 1.5mb after all)

<tilesetdata>
<tileset>
<id>
1
</id>
<022-Roof01/>
<tileset_name>
019-DesertTown01
</tileset_name>
<autotile_names>
<item>
015-Sa_Water0开发者_运维百科1
</item>
<item>
016-Sa_Shadow01
</item>
<item>
018-Sa_Ground01
</item>
<item>
019-Sa_Grass02
</item>
<item>
020-Sa_Grass03
</item>
<item>
021-Sa_Road01
</item>
<item>
022-Roof01
</item>
</autotile_names>
<panorama_name>
</panorama_name>
<panorama_hue>
0
</panorama_hue>
<fog_name>
</fog_name>
<fog_hue>
0
</fog_hue>
<fog_opacity>
64
</fog_opacity>
<fog_blend_type>
0
</fog_blend_type>
<fog_zoom>
200
</fog_zoom>
<fog_sx>
0
</fog_sx>
<fog_sy>
0
</fog_sy>
<battleback_name>
</battleback_name>
<passages>
<item>
15
</item>
<item>
15
</item>
<item>
15
</item>
<item>
15
</item>
<item>
15
</item>
<item>
15
</item>

on the import side I intercepted the the unGziped file strangely enough the file is 1.3mb this time

<tilesetdata>
<tileset>
<id>
 1
</id>
<022-Roof01/>
<tileset_name>
019-DesertTown01
</tileset_name>
<autotile_names>
<item>
015-Sa_Water01
</item>
<item>
016-Sa_Shadow01
</item>
<item>
018-Sa_Ground01
</item>
<item>
019-Sa_Grass02
</item>
<item>
020-Sa_Grass03
</item>
<item>
021-Sa_Road01
</item>
<item>
022-Roof01
</item>
</autotile_names>
<panorama_name>
</panorama_name>
<panorama_hue>
0
</panorama_hue>
<fog_name>
</fog_name>
<fog_hue>
0
</fog_hue>
<fog_opacity>
64
</fog_opacity>
<fog_blend_type>
0
</fog_blend_type>
<fog_zoom>
200
</fog_zoom>
<fog_sx>
0
</fog_sx>
<fog_syy>
0
</fog_sx>
<fog_syy>
0
</fog_sx>
<fog_syy>
0
</fog_sx>
<fog_syy>
0
</fog_sx>
<fog_syy>
0
</fog_sx>

the corruption of the original only gets worse from here on

to clarify I ran the import script and it generated the file tilesets.rmpy (at 18kb) and the i ran the import to test the system and discovered this.

any idea what whent wrong? or if not how fix it, an alternative?


It seems that Gzip only works properly with files open in binary mode

require "rexml/document"
require "zlib"
include REXML

tilesetfile = File.new( "tilesets.rmpy", "wb" )
buffer = ""
tilesetgz = Zlib::GzipWriter.new(tilesetfile)
puts "Compressing output for: tilesets.rxdata ..."
tilesetdoc.write(buffer, 0)
tilesetgz.write(buffer)
tilesetgz.close

and

require "rexml/document"
require "zlib"
include REXML

tilesetfile = File.open("tilesets.rmpy", "rb")
tilesetgz = Zlib::GzipReader.new(tilesetfile)
tilesetdoc = Document.new tilesetgz.read.to_s

worked with out any problems

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜