How to compress with zlib

Just starting out? Need help? Post your questions and find answers here.
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: How to compress with zlib

Post by JHPJHP »

Thunder93 your Structure works great, but so did all the others... moob is using PureBasic 32bit; we also had partial success when he compressed a text file - so the problems now are just confusing?

Did you test the file he posted... what were your results?
Last edited by JHPJHP on Mon Oct 21, 2013 6:13 am, edited 2 times in total.
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: How to compress with zlib

Post by Thunder93 »

moob said he is using external zlib.dll ?
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
moob
User
User
Posts: 68
Joined: Mon Aug 01, 2011 6:16 pm

Re: How to compress with zlib

Post by moob »

JHPJHP wrote:Thunder93 your Structure works great, but so did all the other ones... moob is using PureBasic 32bit; we also had partial success when he compressed a text file - so the problems now are confusing?
the version of zlib in PB 5.00 is the same of PB 5.20???

this code here in multi examples you have post work with any version of zlib???

the problem is not because this code only work with last version of zlib and not work with old zlib version on PB 5.00.

I don't know i try figure out what happens
Last edited by moob on Sun Oct 20, 2013 9:19 pm, edited 2 times in total.
moob
User
User
Posts: 68
Joined: Mon Aug 01, 2011 6:16 pm

Re: How to compress with zlib

Post by moob »

Thunder93 wrote:moob said he is using external zlib.dll ?

No not now, I have test with external zlib.dll too and not work either
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: How to compress with zlib

Post by Thunder93 »

zlib 1.2.8 - April 28, 2013 release

Version 5.20 of PB included then the updated / latest zlib

17 September 2013 : Version 5.20
- Updated: zlib to 1.2.8
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
moob
User
User
Posts: 68
Joined: Mon Aug 01, 2011 6:16 pm

Re: How to compress with zlib

Post by moob »

Thunder93 wrote:zlib 1.2.8 - April 28, 2013 release

Version 5.20 of PB included then the updated / latest zlib

17 September 2013 : Version 5.20
- Updated: zlib to 1.2.8

How can I see the version of zlib in PB 5.00??
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: How to compress with zlib

Post by Thunder93 »

Zlib version info use the following...

Code: Select all

#ENABLE_GZIP = 16
#Z_NULL = 0
#ZLIB_VERSION = "1.2.8"
#Z_FINISH = 4
#Z_DEFAULT_COMPRESSION = -1
#Z_DEFLATED = 8
#MAX_MEM_LEVEL = 9
#Z_DEFAULT_STRATEGY = 0

Structure Z_STREAM
  *next_in.Byte
  avail_in.l
  total_in.l
  *next_out.Byte
  avail_out.l
  total_out.l
  *msg.Byte
  *state
  zalloc.l     
  zfree.l  
  opaque.l
  
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    PB_Alignment1.b[4]
  CompilerEndIf
  
  data_type.i
  adler.l
  reserved.l
  
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    PB_Alignment2.b[8]
  CompilerEndIf  
EndStructure

ImportC "zlib.lib"
  zlibVersion()
  inflateInit2_(*strm, windowBits.i, Version.s, strm_size)
  inflate(*strm, flush.i)
  inflateEnd(*strm)
  deflateInit2_(*strm, level.i, method.i, windowBits.i, memlevel.i, strategy.i, Version.s, strm_size)
  deflateBound(*strm, sourceLen)
  deflate(*strm, flush.i)
  deflateEnd(*strm)
EndImport

#ZLIB_OK = 0
#ZLIB_VERSION_ERROR = -6
Procedure.s ZLIB_Version()
  
  VersionBuffer.i = zlibVersion()
  
  If VersionBuffer <> #Null
    Version.s = Space(32)
    Version = PeekS(VersionBuffer, 32, #PB_Ascii)
    ZLIB_LAST_ERROR = #ZLIB_OK
  Else
    Version = ""
    ZLIB_LAST_ERROR = #ZLIB_VERSION_ERROR
  EndIf
  
  ProcedureReturn Version
EndProcedure 
Debug "Zlib Version: "+ZLIB_Version()



Procedure InflatePayload(*Payload, windowBits.i)
  LengthToRead = MemorySize(*Payload)
  LengthToWrite = FileSize("test.dds")
  *Output = AllocateMemory(LengthToWrite)
  strm.Z_STREAM
  strm\next_in = *Payload
  strm\avail_in = LengthToRead
  strm\next_out = *Output
  strm\avail_out = LengthToWrite
  strm\zalloc = #Z_NULL
  strm\zfree = #Z_NULL
  strm\opaque = #Z_NULL
  inflateInit2_(@strm, windowBits, #ZLIB_VERSION, SizeOf(Z_STREAM))
  inflate(@strm, #Z_FINISH)
  inflateEnd(@strm)
  ProcedureReturn *Output
EndProcedure

Procedure DeflatePayload(*Input, windowBits.i)
  LengthToRead = MemorySize(*Input)
  strm.Z_STREAM
  strm\next_in = *Input
  strm\avail_in = LengthToRead
  strm\zalloc = #Z_NULL
  strm\zfree = #Z_NULL
  strm\opaque = #Z_NULL
  deflateInit2_(@strm, #Z_DEFAULT_COMPRESSION, #Z_DEFLATED, windowBits, #MAX_MEM_LEVEL, #Z_DEFAULT_STRATEGY, #ZLIB_VERSION, SizeOf(Z_STREAM))
  LengthToWrite = deflateBound(@strm, LengthToRead) / 2
  *Payload = AllocateMemory(LengthToWrite)
  strm\next_out = *Payload
  strm\avail_out = LengthToWrite
  deflate(@strm, #Z_FINISH)
  deflateEnd(@strm)
  ProcedureReturn *Payload
EndProcedure

If OpenFile(0, "test.dds")
  LengthToRead = FileSize("test.dds")
  *Input = AllocateMemory(LengthToRead)
  ReadData(0, *Input, LengthToRead)
  CloseFile(0)
  *Payload = DeflatePayload(*Input, 15 | #ENABLE_GZIP)

  If CreateFile(0, "test.gz")
    WriteData(0, *Payload, MemorySize(*Payload))
    CloseFile(0)
  EndIf
  FreeMemory(*Payload)
EndIf

If OpenFile(0, "test.gz")
  LengthToRead = FileSize("test.gz")
  *Payload = AllocateMemory(LengthToRead)
  ReadData(0, *Payload, LengthToRead)
  CloseFile(0)
  *Output = InflatePayload(*Payload, 15 | #ENABLE_GZIP)

  If CreateFile(0, "test2.dds")
    WriteData(0, *Output, MemorySize(*Output))
    CloseFile(0)
  EndIf
  FreeMemory(*Output)
  FreeMemory(*Payload)
EndIf
moob wrote:
Thunder93 wrote:zlib 1.2.8 - April 28, 2013 release

Version 5.20 of PB included then the updated / latest zlib

17 September 2013 : Version 5.20
- Updated: zlib to 1.2.8

How can I see the version of zlib in PB 5.00??
Last edited by Thunder93 on Sun Oct 20, 2013 9:43 pm, edited 2 times in total.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: How to compress with zlib

Post by Thunder93 »

Yea, his version was corrupted.
JHPJHP wrote:Did you test the file he posted... what were your results?
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: How to compress with zlib

Post by Thunder93 »

Code: Select all

LengthToWrite = deflateBound(@strm, LengthToRead) / 2
This isn't right.. It won't work well with larger files, perfectly fine for smaller ones.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
moob
User
User
Posts: 68
Joined: Mon Aug 01, 2011 6:16 pm

Re: How to compress with zlib

Post by moob »

I think the version zlib of PB 5.00 is 1.2.5 or 1.2.6 or more older

Not version 1.2.8

has you can see here http://zlib.net/ChangeLog.txt

'#ZLIB_VERSION = "1.2.8"' this in the code is not the problem? since the version of PB 5.00 is 1.2.5 or 1.2.6 or more older???

the functions are diferent in old version??
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: How to compress with zlib

Post by skywalk »

Hi moob, purebasic v5.2 zlib version = 1.28.
Install the latest purebasic version in a separate directory and compile the supplied compress/decompress code as a dll.
Then call it from your older v5 pb code.

Or just modify your v5 wrapper code to work with v5.2.
There are not a lot of syntactical differences from v5 to v5.2.
Mainly, change *ptr.datatype to *ptr_datatype within your prototypes and procedures.
Any other errors will pop up and you can post them if you need help.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: How to compress with zlib

Post by JHPJHP »

Hi moob,

Updated Code
- zlib1.dll included
- MD5 Fingerprint Hash comparison included
- original Structure restored (I believe the DLL is 32bit only)
- unzip folder
-- run: test.pb (debug mode)

Note: LengthToWrite = DeflateBound(@strm, LengthToRead) / 20
- this setting is for testing only, you may need to modify it for your production code to work
-- check files for data loss
There is a dll version (decompression) near the top of the thread that can be modified for compression, but if you want to post some code - I'll have a look... I'm on PB 5.20 though; not sure if it's using the same zlib library?
- notice that I set the compressed size with: LengthToWrite = deflateBound(@strm, LengthToRead) / 2
-- not sure why delatebound is not optimizing compression - thus the reason for "dividing by two" (modify to whatever works)
Last edited by JHPJHP on Thu Oct 24, 2013 4:31 pm, edited 3 times in total.
moob
User
User
Posts: 68
Joined: Mon Aug 01, 2011 6:16 pm

Re: How to compress with zlib

Post by moob »

JHPJHP wrote:Hi moob,

Please try the following - it's a DLL version of the code: https://www.dropbox.com/s/c13ycz7n4fxa923/ZLib-DLL.zip
- unzip folder
- zlib1.dll included
- original Structure restored (I believe the DLL is 32bit only)
- run: test.pb

Note: LengthToWrite = DeflateBound(@strm, LengthToRead) / 20
- this setting is for testing only, you may need to modify it for your production code to work
-- check files for data loss
There is a dll version (decompression) near the top of the thread that can be modified for compression, but if you want to post some code - I'll have a look... I'm on PB 5.20 though; not sure if it's using the same zlib library?
- notice that I set the compressed size with: LengthToWrite = deflateBound(@strm, LengthToRead) / 2
-- not sure why delatebound is not optimizing compression - thus the reason for "dividing by two" (modify to whatever works)
Nop still corrupt.

I have download and try with zlib.dll version 1.2.3, 1.2.5 and 1.2.7 not work either with this all versions

I will give up compress my dds files I need them compressed because of the size but don't work and I don't know what happens.


I can only thank you all for your patience and assistance provided

special thanks to JHPJHP which is what had more patience with me.

thank you.
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: How to compress with zlib

Post by JHPJHP »

Hi moob,

Can you download it again and run it in debug mode... let me know if you get a Hash MATCH?
- I included a Hash Fingerprint comparison
- run it exactly as is, using the supplied DLL
moob
User
User
Posts: 68
Joined: Mon Aug 01, 2011 6:16 pm

Re: How to compress with zlib

Post by moob »

JHPJHP wrote:Hi moob,

Can you download it again and run it in debug mode... let me know if you get a Hash MATCH?
- I included a Hash Fingerprint comparison
- run it exactly as is, using the supplied DLL
MATCH:
8e8c5f619da1de67d67480671fc2827c
8e8c5f619da1de67d67480671fc2827c

but the dds compressed file still corrupt :cry:
Post Reply