Topic: Bug in addstream?

Hi, firstly thanks a lot for this very handy mod smile

Now, when adding a stream that consists of a series of png files it doesn't seem to add it properly, almost like it doesn't recognise the compression of the png's., but I'm no expert on zip files... Any way I have created an example that illustrates the problem. It adds the stream twice to the zip file, once using addstream, and the other using addfile after saving the file to disk first. I can read the file back fine, but not the stream. and if you look into the zip file you can see that the stream is compressed to a much smaller size so something is wrong. Here's the code:

SuperStrict

Framework brl.basic
Import pub.zipengine
Import brl.pixmap
Import brl.pngloader

Local zip:ZipWriter = New ZipWriter
zip.OpenZip("test.zip", False)

'Create 10 pixmaps and save into a stream in the format:
'Int (size of png)
'Png file
'Int (size of png)
'Png file
'Int (size of png)
'Png file
'.. etc

Local iconbank:TBank = CreateBank()
Local tempstream:TStream = WriteStream(iconbank)
For Local c:Int = 1 To 10
    Local pixmap:TPixmap = CreatePixmap(32, 32, PF_RGBA8888)
    pixmap.WritePixel(c, c, $FFFFFFFF)
    Local tempbank:TBank = CreateBank()
    SavePixmapPNG(pixmap, tempbank)
    tempstream.WriteInt(tempbank.Size())
    WriteBank(tempbank, tempstream, 0, tempbank.Size())
Next

SaveBank(iconbank, "testfile")

'add the stream and file to the zip
zip.AddStream(tempstream, "teststream")
zip.AddFile("testfile")
zip.CloseZip()

'try and load them both back in again

Local ZipFile:ZipReader = New ZipReader
ZipFile.OpenZip("test.zip")
tempstream = ZipFile.ExtractFile("teststream")
DebugLog "Trying to load teststream"
For Local c:Int = 1 To 10
    Local size:Int = tempstream.ReadInt()
    If size
        Local tempbank:TBank = CreateBank(size)
        ReadBank(tempbank, tempstream, 0, size)
        Local pixmap:TPixmap = LoadPixmapPNG(tempbank)
        If pixmap
            DebugLog "Pixmap loaded ok!"
        Else
            DebugLog "Failed to load pixmap!"
        End If
    Else
        DebugLog "Failed to load pixmap!"
    End If
Next
tempstream.Close

tempstream = ZipFile.ExtractFile("testfile")
DebugLog "Trying to load testfile"
For Local c:Int = 1 To 10
    Local size:Int = tempstream.ReadInt()
    If size
        Local tempbank:TBank = CreateBank(size)
        ReadBank(tempbank, tempstream, 0, size)
        Local pixmap:TPixmap = LoadPixmapPNG(tempbank)
        If pixmap
            DebugLog "Pixmap loaded ok!"
        Else
            DebugLog "Failed to load pixmap!"
        End If
    Else
        DebugLog "Failed to load pixmap!"
    End If
Next
tempstream.Close
ZipFile.CloseZip()
End

Obviously my workaround is to simply save the file to disk first and then add that but it seems a little messy smile

Thanks!

Re: Bug in addstream?

greetings smile  thank you for the bug report and fantastic sample to replicate.  it very much helps in tracking things down.  the issue is AddStream not seeking to the front before it wrote.  you can either download the new mod (please note its now gman.ZipEngine), add this line to AddStream() just below the If (inFile) Then line:

' reset to beginning
SeekStream(inFile, 0)

or in your code you can manually call SeekStream() on the stream before passing it into the AddStream() method.

Re: Bug in addstream?

Great! Thanks a lot smile