Jump to content

Is There a Way to Compress a File in AutoIt?


Recommended Posts

Hi

I need to Compress a file in AutoIt,

but I cannot know that the user has any 3rd party program for compression, like WinZIP, WinRAR, etc..

Does AutoIt contain any such functionality to compress a file?

(and also uncompress too)

Thank you

Zohar

Link to comment
Share on other sites

Try using these functions by trancexx, they use windows api to compress and decompress stuff.

#NoTrayIcon

$sString = "111111111122222222233333333333334444444444445555555555556666666666666"
ConsoleWrite("Before compression: " & Binary($sString) & @CRLF)

$Compressed = _LZNTCompress($sString)
ConsoleWrite("Compressed: " & $Compressed & @CRLF)

$Decompressed = _LZNTDecompress($Compressed)
ConsoleWrite("Decompressed: " & $Decompressed & @CRLF)

; #FUNCTION# ;===============================================================================
;
; Name...........: _LZNTDecompress
; Description ...: Decompresses input data.
; Syntax.........: _LZNTDecompress ($bBinary)
; Parameters ....: $vInput - Binary data to decompress.
; Return values .: Success - Returns decompressed binary data.
; - Sets @error to 0
; Failure - Returns empty string and sets @error:
; |1 - Error decompressing.
; Author ........: trancexx
; Related .......: _LZNTCompress
; Link ..........; http://msdn.microsoft.com/en-us/library/bb981784.aspx
;
;==========================================================================================
Func _LZNTDecompress($bBinary)

$bBinary = Binary($bBinary)

Local $tInput = DllStructCreate("byte[" & BinaryLen($bBinary) & "]")
DllStructSetData($tInput, 1, $bBinary)

Local $tBuffer = DllStructCreate("byte[" & 16 * DllStructGetSize($tInput) & "]") ; initially oversizing buffer

Local $a_Call = DllCall("ntdll.dll", "int", "RtlDecompressBuffer", _
"ushort", 2, _
"ptr", DllStructGetPtr($tBuffer), _
"dword", DllStructGetSize($tBuffer), _
"ptr", DllStructGetPtr($tInput), _
"dword", DllStructGetSize($tInput), _
"dword*", 0)

If @error Or $a_Call[0] Then
Return SetError(1, 0, "") ; error decompressing
EndIf

Local $tOutput = DllStructCreate("byte[" & $a_Call[6] & "]", DllStructGetPtr($tBuffer))

Return SetError(0, 0, DllStructGetData($tOutput, 1))

EndFunc ;==>_LZNTDecompress



; #FUNCTION# ;===============================================================================
;
; Name...........: _LZNTCompress
; Description ...: Compresses input data.
; Syntax.........: _LZNTCompress ($vInput [, $iCompressionFormatAndEngine])
; Parameters ....: $vInput - Data to compress.
; $iCompressionFormatAndEngine - Compression format and engine type. Default is 2 (standard compression). Can be:
; |2 - COMPRESSION_FORMAT_LZNT1 | COMPRESSION_ENGINE_STANDARD
; |258 - COMPRESSION_FORMAT_LZNT1 | COMPRESSION_ENGINE_MAXIMUM
; Return values .: Success - Returns compressed binary data.
; - Sets @error to 0
; Failure - Returns empty string and sets @error:
; |1 - Error determining workspace buffer size.
; |2 - Error compressing.
; Author ........: trancexx
; Related .......: _LZNTDecompress
; Link ..........; http://msdn.microsoft.com/en-us/library/bb981783.aspx
;
;==========================================================================================
Func _LZNTCompress($vInput, $iCompressionFormatAndEngine = 2)

If Not ($iCompressionFormatAndEngine = 258) Then
$iCompressionFormatAndEngine = 2
EndIf

Local $bBinary = Binary($vInput)

Local $tInput = DllStructCreate("byte[" & BinaryLen($bBinary) & "]")
DllStructSetData($tInput, 1, $bBinary)

Local $a_Call = DllCall("ntdll.dll", "int", "RtlGetCompressionWorkSpaceSize", _
"ushort", $iCompressionFormatAndEngine, _
"dword*", 0, _
"dword*", 0)

If @error Or $a_Call[0] Then
Return SetError(1, 0, "") ; error determining workspace buffer size
EndIf

Local $tWorkSpace = DllStructCreate("byte[" & $a_Call[2] & "]") ; workspace is needed for compression

Local $tBuffer = DllStructCreate("byte[" & 16 * DllStructGetSize($tInput) & "]") ; initially oversizing buffer

Local $a_Call = DllCall("ntdll.dll", "int", "RtlCompressBuffer", _
"ushort", $iCompressionFormatAndEngine, _
"ptr", DllStructGetPtr($tInput), _
"dword", DllStructGetSize($tInput), _
"ptr", DllStructGetPtr($tBuffer), _
"dword", DllStructGetSize($tBuffer), _
"dword", 4096, _
"dword*", 0, _
"ptr", DllStructGetPtr($tWorkSpace))

If @error Or $a_Call[0] Then
Return SetError(2, 0, "") ; error compressing
EndIf

Local $tOutput = DllStructCreate("byte[" & $a_Call[7] & "]", DllStructGetPtr($tBuffer))

Return SetError(0, 0, DllStructGetData($tOutput, 1))

EndFunc ;==>_LZNTCompress

You can search the forum to find her post here where she explains how to use it.

Link to comment
Share on other sites

Thank you, will check it.

Another trick I thought of,

is, since Windows XP, Windows has a built in ZIP+UnZIP ability - the one used in Windows Explorer.

Can that be used too?

Is there some Shell command that ZIPs and another one that UnZIPs?

Link to comment
Share on other sites

Yeah, there's lots, there's even some scripts that make use of the winrar unrar.dll.

Search it up.

But I prefer the method above as you can add data to your compiled scripts resource and decompress the hell out of it when needed

Link to comment
Share on other sites

Hi

Scripts that make use of the WinRAR's unrar.dll will not be good for me,

since they expect WinRAR to be installed on the user's machine.

I need a solution that does not expect any 3 party compression program to exist on the machine.

Only Windows :D

Will try to search, thank you.

Edited by Zohar
Link to comment
Share on other sites

Hi

Scripts that make use of the WinRAR's unrar.dll will not be good for me,

since they expect WinRAR to be installed on the user's machine.

I need a solution that does not expect any 3 party compression program to exist on the machine.

Only Windows :D

Will try to search, thank you.

You need to think out of the box a little more.

Unrar.dll is a file that has functions usable by autoit, this means you can include the dll in your script.

Link to comment
Share on other sites

I did think outof the box, and did think that I can call that DLL.

However, I assumee, that WinRAR will not like other developrs, to take DLLs from their application, and use them..

(at least not without paying them.. or some kind of licensing)

Maybe I should emphasize:

I need this, for an application that I wrote, that is not just for my own personal use,

but an application I am going to let others to download.

Edited by Zohar
Link to comment
Share on other sites

I assumee, that WinRAR will not like other developrs, to take DLLs from their application, and use them..

(at least not without paying them.. or some kind of licensing).

Ok, so you're thinking out of the box, now the only thing left to do is to escape the box that makes you make assumptions.

Unrar.dll is a "free" library.

Link to comment
Share on other sites

Unrar.dll is free as can be for distribution, I've read their licensing several times and I would recommend you do as well.

The compression tools that actually compress files are not free though.

Read the license.

The RAR Archiver
     EULA (End User License Agreement) for use and distribution

     The RAR archiver is distributed as try before you buy. This means:
1. The author and holder of the copyright of RAR and WinRAR
     is Alexander L. Roshal. The licensor of the following license
     and bearer of the worldwide exclusive usage rights to reproduce
     and distribute RAR and WinRAR is win.rar GmbH, Schumannstr. 17,
     10117 Berlin, Germany.
2. Anyone may use this software during a test period of 40 days.
     Following this test period of 40 days or less, if you wish to
     continue to use RAR, you must purchase a license.
3. There are 2 basic types of licenses issued for RAR, these are:
     a. A single computer usage license. The user purchases one license
         to use RAR archiver on one computer.
         Home users may use their single computer usage license on
         all computers which are in property of the license owner.
         Business users require one license per computer RAR is
         installed on.
     b. A multiple usage license. The user purchases a number of usage
         licenses for use, by the purchaser or the purchaser's employees
         on the same number of computers.
         In a network (server/client) environment you must purchase
         a license copy for each separate client (workstation)
         on which RAR is installed, used, or accessed. A separate
         license copy for each client (workstation) is needed regardless
         of whether the clients (workstations) will use RAR simultaneously
         or at different times. If for example you wish to have
         9 different clients (workstations) in your network with access
         to RAR, you must purchase 9 license copies.
     A user who purchased a RAR license, is granted a non-exclusive
     right to use RAR on as many computers as defined by the licensing
     terms above according to the number of licenses purchased,
     for any legal purpose. The licensed RAR software may not be rented
     or leased, but may be permanently transferred, in it's entirety,
     if the person receiving it agrees to the terms of this license.
     If the software is an update, the transfer must include the update
     and all previous versions.
4. Licensing for RAR on mobile devices (U3 stick, USB stick,
     external harddrive):
     In addition to the terms stated above following licensing terms
     apply to the licensing of RAR on mobile devices.
     a. A single computer usage license. Home users may use their
         single computer usage license on all mobile devices which are
         in property of the license owner.
         Business users may use their single computer usage license
         on one computer and one mobile device.
     b. A multiple usage license. Users who own a multiple usage
         license may use that license on the same number of mobile
         devices as number of computers (clients) the license was
         purchased for.
         The number of computers/devices running RAR at any time is
         limited to the number of licenses purchased according to the
         licensing terms above.
     A licensed version of RAR on a mobile device may be used by
     the purchaser or the purchaser's employees, on several computers
     consecutively.
     There are no additional license fees, apart from the cost of
     purchasing a license, associated with the use of RAR from
     a mobile device on computers that are not owned by the owner
     of the RAR license.
5. The RAR/WinRAR unlicensed trial version may be freely distributed,
     with exceptions noted below, provided the distribution package is not
     modified in any way.
     a. No person or company may distribute separate parts of the package
         with the exception of the UnRAR components, without written
         permission of the copyright owner.
     b. The RAR/WinRAR unlicensed trial version may not be distributed
         inside of any other software package without written permission
         of the copyright owner.
     c. Hacks/cracks, keys or key generators may not be included on the
         same distribution.
6. To buy a license please see order.htm for details.
7. THE RAR ARCHIVER IS DISTRIBUTED "AS IS". NO WARRANTY OF ANY
     KIND IS EXPRESSED OR IMPLIED. YOU USE AT YOUR OWN RISK.
     NEITHER THE AUTHOR NOR THE AGENTS OF THE AUTHOR WILL BE LIABLE
     FOR DATA LOSS, DAMAGES, LOSS OF PROFITS OR ANY OTHER KIND OF LOSS
     WHILE USING OR MISUSING THIS SOFTWARE.
8. There are no additional license fees, apart from the cost of
     purchasing a license, associated with the creation and distribution
     of RAR archives, volumes, self-extracting archives or self-extracting
     volumes. Legally registered owners may use their copies of RAR/WinRAR
     to produce archives and self-extracting archives and to distribute
     those archives free of any additional RAR royalties.
9. You may not use, copy, emulate, clone, rent, lease, sell, modify,
     decompile, disassemble, otherwise reverse engineer, or transfer
     the licensed program, or any subset of the licensed program, except
     as provided for in this agreement. Any such unauthorized use shall
     result in immediate and automatic termination of this license and
     may result in criminal and/or civil prosecution.
     7zxa.dll library is copyrighted by Igor Pavlov and distributed
     under LGPL Version 3 license ( http://www.gnu.org/licenses/lgpl.html ).
     You can modify portions of 7zxa.dll and perform reverse engineering
     solely for purpose of debugging such 7zxa.dll modifications according
     to LGPL. Source code of 7zxa.dll is available on www.7-zip.org.
     Neither RAR binary code, WinRAR binary code, UnRAR source or UnRAR
     binary code may be used or reverse engineered to re-create the RAR
     compression algorithm, which is proprietary, without written
     permission of the author.
     RAR and WinRAR keyfiles may not be distributed, except as stated
     in item 3) above, outside of the area of legal control of the
     person or persons who purchased the original license, without
     written permission of the copyright holder.
10. WinRAR unregistered version can display a registration reminder dialog
     prompting a user to obtain a license. Depending on WinRAR version
     and configuration such dialog can contain either a predefined text
     and links loaded locally or a web page loaded from the internet.
     Such web page can contain licensing instructions and other materials
     according to win.rar GmbH's choice, including advertisement.
     When opening a web page, WinRAR transfers only those parameters
     which are technically required by HTTP protocol to successfully open
     a web page in a browser.
11. All rights not expressly granted in the license are reserved by
     Alexander Roshal.
     Installing and using RAR/WinRAR signifies acceptance of these terms
     and conditions of the license.
12. If you do not agree with the terms of this license you must
     remove RAR/WinRAR files from your storage devices and cease to
     use the product.
     Thank you for using the original RAR.
                                         Alexander L. Roshal

Also, check out existing scripts that work with unrar.dll, there are several.

Edited by ApudAngelorum
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...