Jump to content

zLib (Deflate/Inflate/GZIP) UDF


Ward
 Share

Recommended Posts

To use zLib in AutoIt, the easiest way is download the DLL, and then just use it by DllCall, or warp it by my

But now I convert zLib to machine code version UDF, because "just for fun", or because I can.

There is another good reason: to minimize the UDF size (because there are some functions I don't need in the DLL file).

The best thing of this library is that it can compress/decompress the gzip format.

So this is also a "GZIP UDF" in pure script.

Example:

; -----------------------------------------------------------------------------
; Zlib Compression Library (Deflate/Inflate) Machine Code UDF Example
; Purpose: Provide The Machine Code Version of Zlib Library In AutoIt
; Author: Ward
; Zlib Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler
; -----------------------------------------------------------------------------

#Include "ZLIB.au3"

; Test _ZLIB_Compress And _ZLIB_Uncompress
Dim $Original = BinaryFileRead(@AutoItExe)
Dim $Compressed = _ZLIB_Compress($Original, 9)
Dim $Decompressed = _ZLIB_Uncompress($Compressed)
Dim $Result = 'Original Size: ' & BinaryLen($Original) & @CRLF
$Result &= 'Compressed Size: ' & BinaryLen($Compressed) & @CRLF
$Result &= 'Decompress Succeed: ' & ($Decompressed = $Original)
MsgBox(0,'Deflate/Inflate Test', $Result)


; Test _ZLIB_GZCompress And _ZLIB_GZUncompress
Dim $Original = BinaryFileRead(@AutoItExe)
Dim $Compressed = _ZLIB_GZCompress($Original, 9)
Dim $Decompressed = _ZLIB_GZUncompress($Compressed)
Dim $Result = 'Original Size: ' & BinaryLen($Original) & @CRLF
$Result &= 'Compressed Size: ' & BinaryLen($Compressed) & @CRLF
$Result &= 'Decompress Succeed: ' & ($Decompressed = $Original)
MsgBox(0,'Gzip Test', $Result)


; Test _ZLIB_FileCompress And _ZLIB_FileUncompress
Dim $OriginalPath = @AutoItExe
Dim $CompressedPath = @TempDir & "\test.z"
Dim $DecompressedPath = @TempDir & "\test.unz"
_ZLIB_FileCompress($OriginalPath, $CompressedPath, 9)
_ZLIB_FileUncompress($CompressedPath, $DecompressedPath)
Dim $Result = 'Original Size: ' & FileGetSize($OriginalPath) & @CRLF
$Result &= 'Compressed Size: ' & FileGetSize($CompressedPath) & @CRLF
$Result &= 'Decompress Succeed: ' & (BinaryFileRead($DecompressedPath) = BinaryFileRead($OriginalPath))
FileDelete($CompressedPath)
FileDelete($DecompressedPath)
MsgBox(0,'Deflate/Inflate File Test', $Result)


; Test _ZLIB_GZFileCompress And _ZLIB_GZFileUncompress
Dim $OriginalPath = @AutoItExe
Dim $CompressedPath = @TempDir & "\test.gz"
Dim $DecompressedPath = @TempDir & "\test.ungz"
_ZLIB_GZFileCompress($OriginalPath, $CompressedPath, 9)
_ZLIB_GZFileUncompress($CompressedPath, $DecompressedPath)
Dim $Result = 'Original Size: ' & FileGetSize($OriginalPath) & @CRLF
$Result &= 'Compressed Size: ' & FileGetSize($CompressedPath) & @CRLF
$Result &= 'Decompress Succeed: ' & (BinaryFileRead($DecompressedPath) = BinaryFileRead($OriginalPath))
FileDelete($CompressedPath)
FileDelete($DecompressedPath)
MsgBox(0,'Gzip File Test', $Result)

Exit

Func BinaryFileRead($Path)
    Local $File = FileOpen($Path, 16)
    Local $Data = FileRead($File)
    FileClose($File)
    Return $Data
EndFunc

For other compression method, see my "

ZLIB.zip

Edited by Ward

新版 _ArrayAdd 的白痴作者,不管是誰,去死一死好了

 

Link to comment
Share on other sites

  • 3 weeks later...

Great work Ward!

I found it really interesting to be able to deflate and/or inflate in raw mode, ie without header and footer. Just put $Level = -15 (windowBits) and you're able to compress/uncompress in raw mode. Thanks once again.

Joakim

Link to comment
Share on other sites

  • 1 year later...
  • 1 month later...

I tried to do this, but didn't work:

#Include "ZLIB.au3"

$Path = (@scriptdir & "test.exe")

; Test _ZLIB_Compress And _ZLIB_Uncompress
Dim $Original = BinaryFileRead(@ScriptDir & "test.exe")
Dim $Compressed = _ZLIB_Compress($Original, 9)

Func BinaryFileRead($Path)
Local $File = FileOpen($Path, 16)
Local $Data = FileRead($File)
FileClose($File)
Return $Data
EndFunc
Link to comment
Share on other sites

Legend,

I know nothing about this UDF. But if you want to get help from anyone you need to be more specific. "Doesn't work" isn't.

Do you get any error messages? Is @error set? Does the script crash? ...

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

If you're referring specifically to your test script... well, what are you doing with $Compressed? As far as I can see you're doing nothing with it. Your script and the functions don't compress any data in place, so what do you expect the end result to be?

Link to comment
Share on other sites

  • 7 months later...

I tried to do this, but didn't work:

 

#Include "ZLIB.au3"

$Path = (@scriptdir & "test.exe")

; Test _ZLIB_Compress And _ZLIB_Uncompress
Dim $Original = BinaryFileRead(@ScriptDir & "test.exe")
Dim $Compressed = _ZLIB_Compress($Original, 9)

Func BinaryFileRead($Path)
Local $File = FileOpen($Path, 16)
Local $Data = FileRead($File)
FileClose($File)
Return $Data
EndFunc

 

I know this topic is VERY old but I couldn't resist.  Also, this may help.....

;$Path = (@scriptdir & "test.exe")
$Path = (@scriptdir & "\test.exe"); * Don't forget the backslash..

;Dim $Original = BinaryFileRead(@ScriptDir & "test.exe")
Dim $Original = BinaryFileRead($Path)
Edited by NewPlaza
Link to comment
Share on other sites

  • 5 months later...

well the good news is i learned a lot today, i changed one of my headers to: "Accept-Encoding: deflate" and it automatically deflates for me. i also know a lot more about certain subjects in general. VERY PRODUCTIVE DAY! Jos i owe you one man. a friend is paying me for my current project lol - you saved me some troubles

Link to comment
Share on other sites

  • Developers

well the good news is i learned a lot today, i changed one of my headers to: "Accept-Encoding: deflate" and it automatically deflates for me. i also know a lot more about certain subjects in general. VERY PRODUCTIVE DAY! Jos i owe you one man. a friend is paying me for my current project lol - you saved me some troubles

Good for you ;)

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • 2 years later...
  • 2 years later...

Does this still work?

I'm using the code from the post 2 above me, and am trying the default example (under Win10), but only get an error code...

#Include "ZLIB.au3"

; Test _ZLIB_Compress And _ZLIB_Uncompress
Dim $Original = BinaryFileRead(@AutoItExe)
Dim $Compressed = _ZLIB_Compress($Original, 9)
Dim $Decompressed = _ZLIB_Uncompress($Compressed)
Dim $Result = 'Original Size: ' & BinaryLen($Original) & @CRLF
$Result &= 'Compressed Size: ' & BinaryLen($Compressed) & @CRLF
$Result &= 'Decompress Succeed: ' & ($Decompressed = $Original)
MsgBox(0,'Deflate/Inflate Test', $Result)

Exit

Func BinaryFileRead($Path)
    Local $File = FileOpen($Path, 16)
    Local $Data = FileRead($File)
    FileClose($File)
    Return $Data
EndFunc

This comes up after Autoit tries to execute the _ZLIB_Compress function:

!>14:11:15 AutoIt3.exe ended.rc:-1073741819
Link to comment
Share on other sites

9 hours ago, Frescard said:

Does this still work?

I'm using the code from the post 2 above me, and am trying the default example (under Win10), but only get an error code...

This comes up after Autoit tries to execute the _ZLIB_Compress function:

!>14:11:15 AutoIt3.exe ended.rc:-1073741819

Add #AutoIt3Wrapper_UseX64 = no in first line ^^

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...