Jump to content

Create Self Extracting 7zip Exe with Batch File and Configuration File


GokAy
 Share

Recommended Posts

Hey there AutoIt community,

I am not exactly sure where to post this, here seems to be the best option. Moderators can move to a more appropriate forum if not I guess.

This post won't be specifically about AutoIt, but I couldn't find a comprehensive tutorial online about the topic. I would like to share what I learned after several hours (might be a bit more :) ) of searching/reading forums, and a lot of trial and error. I hope you will find it informative.

My challenge:

I am working on an MS Excel xlsm file that doesn't require anything special other than a certain folder structure with certain files (only if you would like to use certain features) inside. However, the management might become cumbersome for certain aspects. Therefore, I used AutoIt (for the first time about couple of weeks ago) and developed some helper scripts which are completely optional. One of those scripts is my installer script. Basically lets you choose install location, has options for "Use AutoIt Scripts" - "Add Registry Association" and "Extension Type" ( I won't go into detail here as it is not the topic of this post). 

"CH_Install" is the working directory for the project, will be referred as Installer folder.

"CH_Install\Charter" holds the folder structure necessary (which will be copied to the user selected install folder).

"CH_Install\Scripts" holds AutoIt3.exe and the a3x script files. This folder is a result of how I wanted to install stuff according to the user selected options during installation time, and is not a necessity.

"CH_Install\CH Install.7z" is the zip file containing the files/folders that I want to be included in the installation. (In this case Charter and Scripts folders.

image.png.3d5ef19d28972f282de9b81d70a62876.png

First I tried using FileInstall option of AutoIt which worked alright, however the exe files created was getting flagged as malware (even managed to get some 100% machine learning malware!). So I searched for other options, and creating a self extracting zip file (7zip in this case) along with using a3x files using AutoIt3.exe as the interpreter seemed reasonable. Zipping and creating sfx exe is a straightforward process, no extra knowledge is needed. However, each time I add/remove/change something, either in folder structure or any file including the scripts, I had to manually create the zip file, and then make it an executable, which in itself became time consuming. So I searched some more. (I will list some of the pages I found worthy to save for future reference at the end)

The following is what I have come up with, using my project as an example. Modify variables with your own setup.

Requirements:

1. 7zip (can be portable version, no installation needed) - https://www.7-zip.org/download.html

2. 7zip SDK (only need 7zSD.sfx, resides in "\lzma1900\bin" subfolder) - https://www.7-zip.org/sdk.html

3. Batch file for the commands to run

4. A text file containing files/folders to exclude from the zip file, can also be an include list instead (I did not pursue this option as exclusion seemed better for my need)

5. A configuration file to use with 7zSD.sfx while creating the self extracting 7zip executable.

6. And ofcourse your own Folder Structure/Files.

Batch File: "_Create Self Extracting Exe.bat" (name as you wish)

Just change the variables according to your setup. For more options, check out the references at the end.

@cls

@REM Set variables for 7z file to be included in Self Extracting Exe
@set "SevenZipPath=D:\Apps\00-Charter\Portable\7-ZipPortable\App\7-Zip"
@set "InstallerPath=%~dp0."     If batch file is located under Installer Folder
@REM @set "InstallerPath=C:\Users\gkhna\Desktop\CH_Install"     If batch file is located elsewhere, also change ExclusionList file accordingly 
@set "ZipName=CH Install.7z"
@set "ExclusionList=_Exclude List.txt"

@REM Set variables for creating the Self Extracting Exe
@set "SevenZipSDKPath=D:\Apps\00-Charter\7z SDK\lzma1900\bin"
@set "SelfExeName=LazyD Charter Installer.exe"
@set "ConfigurationFile=_Self Extracting Exe Configuration.txt"

@REM Delete old archieve contents (as deleted stuff remain in the archieve w/o this step)
"%SevenZipPath%\7z.exe" d "%InstallerPath%\%ZipName%" *

@REM Add current contents with an Exclusion List
"%SevenZipPath%\7z.exe" a -x@"%InstallerPath%\%ExclusionList%" "%InstallerPath%\%ZipName%" "%InstallerPath%\*"

@REM Create Self Extracting Exe
copy /b "%SevenZipSDKPath%\7zSD.sfx" + "%InstallerPath%\%ConfigurationFile%" + "%InstallerPath%\%ZipName%" "%InstallerPath%\%SelfExeName%"

Exclusion List: "_Exclude List.txt" (Modify Batch File also if named differently)

List of files/folders/subfolders whatever you want to exclude from zip file (not the final self extracting zip).

In my case it would look like this. Essentially the bat, configuration file, exclusion list, any previously created zip and sfx files. Add to list if you have more stuff that you don't want to end up inside the zip. 

If Batch File is under Installer Folder use this format, if not remove ".\" here

C:\Users\gkhna\Desktop\CH_Install\.\_Create Self Extracting Exe.bat
C:\Users\gkhna\Desktop\CH_Install\.\_Self Extracting Exe Configuration.txt
C:\Users\gkhna\Desktop\CH_Install\.\_Exclude List.txt
C:\Users\gkhna\Desktop\CH_Install\.\LazyD Charter Installer.exe
C:\Users\gkhna\Desktop\CH_Install\.\CH Install.7z

Configuration File: "_Self Extracting Exe Configuration.txt" (Modify both batch and exclusion list if named differently))

Holds information about the following window you see upon running sfx file, and the program to run after stuff gets unzipped into a random named folder inside AppData\Local\Temp.

image.png.2fd152c79c013701b361f4821b881105.png

;!@Install@!UTF-8!
Title="LazyD Charter Installer v1.0.0.0"
BeginPrompt="Do you want to install LazyD Charter v1.0.0.0?"
Directory=".\\Scripts\\"
RunProgram="AutoIt3.exe %%T\\Scripts\\CH_Installer.a3x"
;!@InstallEnd@!

"Directory" is only needed if RunProgram is in another folder, in this case inside "Scripts" which is relative to temp unzip folder.

"RunProgram" name of the exe (and any parameters to pass to it, in this case the a3x script name)

"%%T" is used as an abbreviation to the temp folder the contents are unzipped.

You need to use double "\\" for folders as "\" used as an escape character by 7z.

There are some other options available, check out the references if you like.

 

Finally (only for AutoIt users), Aut2Exe is set to compile into Scripts folder, so after compiling any new/updated scripts, I just double click the .bat and my updated installer exe is ready.

I hope all is clear, but if not let me know and I will do my best to explain further. And before you ask, "No, I don't know all the details about 7z parameters/switches or batch files in general 😛 )

References:

https://superuser.com/questions/42788/is-it-possible-to-execute-a-file-after-extraction-from-a-7-zip-self-extracting-a

https://superuser.com/questions/330005/7zip-how-to-exclude-files-not-file-types-using-an-exclude-list-file

https://olegscherbakov.github.io/7zSFX/switches.html

http://www.paraglidernc.com/utilities/sfxsetup.htm

https://www.dotnetperls.com/7-zip-examples

https://thedeveloperblog.com/7-zip-examples

https://superuser.com/questions/641686/how-can-i-zip-or-rar-files-listed-in-a-text-file-from-the-windows-command-line (include list)

Link to comment
Share on other sites

Hey Confuzzled,

You are right, I could have written most with VBA, just would take a bit longer. I couldn't have written the video player part I believe, idk. UPX is off, compression at 0. All consolewrite's removed from the code. I have employed everything that I could, learnt from posts here on AutoIt forums. I already had a vb.net video player written but even that was/ and still getting flagged by Google (and all it does is sending the received messages to mci). My dumb AV is Windows Defender (coupled with MBAM), so it will most probably be what people will be having. Chrome denies to download it from Google Drive still...

By the way, you should get that head checked, it seems to hurt too often ;)

image.png.db6400d7e4d7f4c125bff9d3e5b5fd71.png

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...