gman.zipengine Unicode

BlitzMax Forums/BlitzMax Programming/gman.zipengine Unicode

siread(Posted 2011) [#1]
I'm trying to zip a bank stream with gman's zipengine but I can't figure out how to store unicode characters. It works fine if you store a utf-8 text file from disk, but with streams the characters appear to get converted to ANSI.

Framework brl.Basic
Import gman.zipengine

' Create our zipwriter object
Local zwObject:ZipWriter = New ZipWriter
Local bank:TBank = CreateBank()
Local bankfile:TBankStream = CreateBankStream(bank)
WriteLine(bankfile, "ЯабвгдежзийклмнопрстуȻȼȽɐɑɒɓɔɕɖɗɘəɚɛɜɝɞɟɠɡɢ")

If (zwObject.OpenZip("data.zip", False))
	'zwObject.AddFile("TestData.txt")
	zwObject.AddStream(bankfile, "BankTestData.txt")
	zwObject.CloseZip()
End If


Like I say, if I put the same text into a utf-8 text file (TestData.txt) and uncomment the AddFile line the file is stored perfectly. Any idea what's going wrong?

Edit: The forum can't handle unicode either. I need extended characters like these: http://www.utf8-chartable.de/unicode-utf8-table.pl?start=384

Last edited 2011


GfK(Posted 2011) [#2]
I read somewhere that streams and Unicode are a no-go - regardless of whether you're using ZipEngine or not.

Because of that I'm currently using LoadString/SaveString for stuff that contains Unicode.


siread(Posted 2011) [#3]
Hmm, well I seem to have found a work around after adapting some old code of neilos http://www.blitzbasic.com/Community/posts.php?topic=57686.

I purely need this for my games save files so I create them with a custom TBankStream and load with a custom TStream. The beauty of this is that I can add a password to the zip and stop casual cheats editing their save file and uploading to the leaderboards. :)

' Saving
Local bank:TBank = CreateBank()
Local bankstream:TMyBankStream = TMyBankStream.Create(bank)	
' WRITE DATA TO STREAM

' Create zip version
Local zipw:ZipWriter = New ZipWriter

If (zipw.OpenZip("Save/"+filename, False))
	zipw.AddStream(bankstream, "savefile")
EndIf
		
CloseStream(file)
zipw.CloseZip()

' Loading
Local file:TMyStream = New TMyStream
file.SetStream(ReadStream("zipe::Save/"+filename+"::savefile"))

' READ DATA FROM STREAM


Type TMyBankStream Extends TBankStream 
	Function Create:TMyBankStream( bank:TBank )
		Local stream:TMyBankStream=New TMyBankStream
		stream._bank=bank
		Return stream
	End Function

	Method WriteString(str:String)
		WriteInt(str.Length)
		If str.Length = 0 Then Return
		Local shorts:Short Ptr = str.ToWString()
		WriteBytes(shorts, str.Length*2)
		MemFree(shorts)
	End Method
End Type

Type TMyStream Extends TStreamWrapper
	Method ReadString:String()
		Local ln% = ReadInt()
		If ln = 0 Then Return ""
		Local shorts:Short[ln]
		ReadBytes( shorts, ln*2 )
		Return String.FromShorts( shorts, ln )
	End Method
End Type


Last edited 2011