Passing Data to Zlib

Everything else that doesn't fall into one of the other PB categories.
Akuma no Houkon
User
User
Posts: 77
Joined: Sun Nov 02, 2003 1:47 pm
Location: Washington
Contact:

Passing Data to Zlib

Post by Akuma no Houkon »

I have a function I am porting from both VB and PB (powerbasic) where it works flawless. In PureBasic however, I get nothing but errors, and I assume its probably from the way the data is being passed TO zlib.

Here is the function as it stands.

Code: Select all

Procedure LoadCompressedFile(FileName.s)
OpenFile(1,FileName)   
    BufferSize.l = 1048576 + (1048576 * 0.01) + 12
    ComSize.l = ReadLong()
    Dim TestArray.b(ComSize)
    ReadData(@TestArray(0),ComSize)
    Dim FinalArray.b(BufferSize)           
    Result =  CallFunction(#ZLIB_LIB,"uncompress", @FinalArray(0), @BufferSize, @TestArray(0), ComSize)   
    Msgbox(Str(Result))
CloseFile(1) 
EndProcedure
If anyone knows what I am doing wrong, please post here. Sugestions are welcome.

The file is 1048576 bytes long uncompressed, it is ComSize compressed.
AMD 2600+, nVidia Geforce FX 5200 128 MB DDR, 512 DDR, RedHat 9
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Post by Pupil »

I suggest you review how the file you're trying to read is built up, from what i can see the procedure should work as it is now, provided the data you read into ComSize is valid and that the predetermined buffer size is also valid. Anyhow this code works for me:

Code: Select all

; Compress using zlib.dll
If OpenLibrary(0, "zlib.dll")
  file.s = OpenFileRequester("File to compress", "", "All files|*.*", 0)
  If file <> ""
    If ReadFile(0, file)
      SourceLen.l = Lof()
      DestLen.l = SourceLen + SourceLen/1000 + 16
      If AllocateMemory(0, SourceLen) And AllocateMemory(1, DestLen)
        ReadData(UseMemory(0), SourceLen)
        result.l = CallFunction(0, "compress", UseMemory(1), @DestLen, UseMemory(0), SourceLen)
        If result = 0
          If CreateFile(1, file+".zp")
            WriteLong(SourceLen)
            WriteLong(DestLen)
            WriteData(UseMemory(1), DestLen)
            CloseFile(1)
          Else
            Debug "unable to create output file!"
          EndIf
        Else
          Debug "compression failed, returned with error: "+Str(result)
        EndIf
      Else
        Debug "unable to allocate memory"
      EndIf
      CloseFile(0)
    Else
      Debug "Unable to open file."
    EndIf
  Else
    Debug "no file selected"
  EndIf
  CloseLibrary(0)
Else
  Debug "Failed to open dll"
EndIf



; Decompress using zlib.dll
If OpenLibrary(0, "zlib.dll")
  file.s = OpenFileRequester("File to decompress", "", "zpak|*.zp", 0)
  If file <> ""
    If ReadFile(0, file)
      DestLen.l = ReadLong()
      SourceLen.l = ReadLong()
      DestLen.l = DestLen + DestLen/1000 + 16
      If AllocateMemory(0, SourceLen) And AllocateMemory(1, DestLen)
        ReadData(UseMemory(0), SourceLen)
        result.l = CallFunction(0, "uncompress", UseMemory(1), @DestLen, UseMemory(0), SourceLen)
        If result = 0
          If CreateFile(1, file+".unzp")
            WriteData(UseMemory(1), DestLen)
            CloseFile(1)
          Else
            Debug "unable to create output file!"
          EndIf
        Else
          Debug "decompression failed, returned with error: "+Str(result)
        EndIf
      Else
        Debug "unable to allocate memory"
      EndIf
      CloseFile(0)
    Else
      Debug "Unable to open file."
    EndIf
  Else
    Debug "no file selected"
  EndIf
  CloseLibrary(0)
Else
  Debug "Failed to open dll"
EndIf
Akuma no Houkon
User
User
Posts: 77
Joined: Sun Nov 02, 2003 1:47 pm
Location: Washington
Contact:

Post by Akuma no Houkon »

The file being loaded is always that specific amount of bytes, so the buffersize is always fine.

The data likewise is fine, as with the same function in VB and PowerBasic, my data is loaded just fine and zlib gives me the "a ok" code.

Your function seems to work IF I use purebasic to compress the data. But doesnt seem to work at all on data compressed with other languages. Although it should be the same because its the same API call, the same data, the same dll version, the same function.

I tried your function yet still it gives me error code -5 Z_BUF_ERROR Somethings not right...the data decompresses in two other languages using the same dll api call, but doesnt decompress in purebasic, yet the decompress and compress obviously work because your function works.
AMD 2600+, nVidia Geforce FX 5200 128 MB DDR, 512 DDR, RedHat 9
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Post by Pupil »

yes, there's something fishy going on, you think you can post the VB version where you compress and save the file?
Akuma no Houkon
User
User
Posts: 77
Joined: Sun Nov 02, 2003 1:47 pm
Location: Washington
Contact:

Post by Akuma no Houkon »

Well thanks to your code (seeing it actually work properly) so I could be sure of the errors, I figured out that because I was using the SourceLen to read from the file, the number I was saving was the UBOUND() of the array, rather than the # of elements in the array (since ubound is usually 1 less than the number of elements because it starts at 0 I was getting a buffer error. Just adding 1 to the ReadLong() for the SourceLen and I get the A_OK. My VB/PowerBasic zlib wrapper automatically added that one during the proccess so it skipped my mind.

Thanks for your help and time.
AMD 2600+, nVidia Geforce FX 5200 128 MB DDR, 512 DDR, RedHat 9
Seldon
Enthusiast
Enthusiast
Posts: 405
Joined: Fri Aug 22, 2003 7:12 am
Location: Italia

Post by Seldon »

Great code, thank you. Is it possible to use that DLL to "unzip" a real ZIP archive and have all the files extracted ? I wonder if there is some code to unzip a ZIP archive without using a DLL :?: I'd like to convert that code for a PB user-lib.
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Post by Pupil »

Check out this topic:
viewtopic.php?t=7594

i think it's a PB lib and not a dll-wrapper, i haven't tried it myself so i don't know for sure though..
Post Reply