GZipHelper includefile

Share your advanced PureBasic knowledge/code with the community.
User avatar
kenmo
Addict
Addict
Posts: 2043
Joined: Tue Dec 23, 2003 3:54 am

GZipHelper includefile

Post by kenmo »

This may be useful... :)

Here is an includefile of helper functions for compressing to / decompressing from GZip format (DEFLATE but wrapped in different header/footer than ZLib).

The source data can be a file, string, buffer (AllocateMemory), or arbitrary memory range.
It can compress to a GZip file, buffer, or arbitrary memory range.
It can decompress/uncompress/"UnGZip" back to those four original formats.

So that's 24 different conversions available... 4 data sources x 3 GZip destinations x 2 directions

Uses only PB's "Zip" and "CRC32"! No imports required! Enjoy.


GitHub link: https://github.com/kenmo-pb/includes/bl ... Helper.pbi

Direct PBI link: https://raw.githubusercontent.com/kenmo ... Helper.pbi

EDIT:

Compress procedures

Code: Select all

Procedure.i GZip_MemoryToMemory(*Source, SourceBytes.i, *Destination, DestinationBytes.i)
Procedure.i GZip_MemoryToBuffer(*Source, SourceBytes.i)
Procedure.i GZip_BufferToBuffer(*Source)
Procedure.i GZip_BufferToMemory(*Source, *Destination, DestinationBytes.i)
Procedure.i GZip_StringToMemory(String.s, *Destination, DestinationBytes.i, Format.i = #PB_Default, IncludeNull.i = #False)
Procedure.i GZip_StringToBuffer(String.s, Format.i = #PB_Default, IncludeNull.i = #False)
Procedure.i GZip_FileToMemory(File.s, *Destination, DestinationBytes.i, RangeStart.q = #GZipHelper_StartOfFile, RangeSize.q = #GZipHelper_RestOfFile)
Procedure.i GZip_FileToBuffer(File.s, RangeStart.q = #GZipHelper_StartOfFile, RangeSize.q = #GZipHelper_RestOfFile)
Procedure.i GZip_FileToFile(SourceFile.s, GZipFile.s, RangeStart.q = #GZipHelper_StartOfFile, RangeSize.q = #GZipHelper_RestOfFile)
Procedure.i GZip_MemoryToFile(*Source, SourceBytes.i, File.s)
Procedure.i GZip_BufferToFile(*GZip, File.s)
Procedure.i GZip_StringToFile(String.s, File.s, Format.i = #PB_Default, IncludeNull.i = #False)
Uncompress / Decompress procedures

Code: Select all

Procedure.i UnGZip_MemoryToMemory(*GZip, GZipBytes.i, *Destination, DestinationBytes.i)
Procedure.i UnGZip_MemoryToBuffer(*GZip, GZipBytes.i)
Procedure.i UnGZip_BufferToBuffer(*GZip)
Procedure.i UnGZip_BufferToMemory(*GZip, *Destination, DestinationBytes.i)
Procedure.s UnGZip_MemoryToString(*GZip, GZipBytes.i, Format.i = #PB_Default)
Procedure.s UnGZip_BufferToString(*GZip, Format.i = #PB_Default)
Procedure.i UnGZip_MemoryToFile(*GZip, GZipBytes.i, File.s)
Procedure.i UnGZip_BufferToFile(*GZip, File.s)
Procedure.i UnGZip_FileToMemory(GZipFile.s, *Destination, DestinationBytes.i)
Procedure.i UnGZip_FileToBuffer(GZipFile.s)
Procedure.i UnGZip_FileToFile(GZipFile.s, DestinationFile.s)
Procedure.s UnGZip_FileToString(GZipFile.s, Format.i = #PB_Default)
Misc procedures

Code: Select all

Procedure.i GZip_GetUncompressedSizeFromMemory(*GZip, GZipBytes.i)
Procedure.i GZip_GetUncompressedSizeFromBuffer(*GZip)
Procedure.i GZip_GetUncompressedCRC32FromMemory(*GZip, GZipBytes.i)
Procedure.i GZip_GetUncompressedCRC32FromBuffer(*GZip)
Procedure.i GZip_GetRequiredBufferSize(*Source, SourceBytes.i)
Last edited by kenmo on Sat Jun 21, 2025 7:53 pm, edited 1 time in total.
User avatar
idle
Always Here
Always Here
Posts: 5889
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: GZipHelper includefile

Post by idle »

very useful thanks.
Axolotl
Addict
Addict
Posts: 832
Joined: Wed Dec 31, 2008 3:36 pm

Re: GZipHelper includefile

Post by Axolotl »

Great Job as always. Thanks for sharing.
BTW: You really love the use of parentheses, don't you? :)
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
User avatar
kenmo
Addict
Addict
Posts: 2043
Joined: Tue Dec 23, 2003 3:54 am

Re: GZipHelper includefile

Post by kenmo »

Updated original post with lists of the Procedures and their params. So you don't need to go reading through all that plaintext code on GitHub.
Axolotl wrote: Fri Jun 20, 2025 11:20 am BTW: You really love the use of parentheses, don't you? :)
Yes :lol: I have my reasons.
Post Reply