Jump to content

FileInstall doesn't extract .zip or .rar files - (Moved)


Recommended Posts

What i'm trying to achieve is to bundle a few folders within the executable script and from what I've read it can be done with:

#include <FileConstants.au3>

FileInstall(src, dest, $FC_OVERWRITE)

I know that src must be a literal string and cant be a variable or macro. However, I also learned that I cant just install a folder but there is a workaround that uses 7zip or winrar so that FileInstall extracts the folder to the desired location (e.g. to dest) so I wend and tried both variants: 

  1. I zipped a single folder just for testing.
  2. I winrared a single folder just for testing.

Keep in mind that both the .zip and .rar are the same folder with the same content.

The problem I ran into is that FileInstall simply moves the .rar or .zip folder to the desired location but it doesn't extract it!

#RequreAdmin

#include <FileConstants.au3>

Func installESPLibraries()
    Local Const $sLibrariesPath = @LocalAppDataDir & "\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries"

    FileInstall("esp-libraries\ESP32-ADXL345.rar", $sLibrariesPath & "\", $FC_OVERWRITE)
EndFunc   

installESPLibraries()

The result I'm left with is that ESP32-ADXL345.rar just gets moved to $sLibrariesPath (so it does kinda work). The result I want is to have ESP32-ADXL345.rar extracted to the same location so that I have only a folder with the name ESP32-ADXL345 without any winrar files. Is there a way to achieve such a thing ? (because i'm sure there is).

NOTE: I already did search the forum for such a solution:

  1. how to use fileinstall
  2. want to bundle my exe foldersfiles
  3. embedding images folders in exe

However, They all show practical examples with files and none with folders (the third link mentions folders but has no examples) so I am at a loss as to how it should be done.

 

EDIT:

Thanks to @Musashi and @Nine for pointing out what I was missing! Also, I would like to add my solution:

#RequreAdmin

#include <FileConstants.au3>

; Create a local constant string to hold a path to the folder where we will be installing the libraries
Local Const $sLibrariesPath = @LocalAppDataDir & "\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\"

; Unpack libraries.rar to the path we defined
FileInstall("libraries.rar", $sLibrariesPath, $FC_OVERWRITE)

; Run the batch script to extract libraries.rar at the path we defined
RunWait("unrar.bat " & $sLibrariesPath & " libraries.rar", "", @SW_HIDE)

; Deletes libraries.rar
FileDelete($sLibrariesPath & "libraries.rar")

I attached the .bat file I wrote to make it work. The reasoning for this solution is to show how its done cleanly with .rar files as I didn't see anyone do it this way. Hope it helps people in the future!

unrar.bat

Edited by DanielRossinsky
A solution has been found
Link to comment
Share on other sites

The function name FileInstall can, in my opinion, be a bit misleading. As you have already discovered yourself, FileInstall will embed a file into the .exe and copy it to the specified destination directory when the .exe get's started.

To unzip the file (let's take a .zip as an example), you need an additional step. One possibility would be to additionally embed the standalone command line variant of 7-Zip (7za.exe) into your script and use it to extract the .zip. 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

You will need to unzip the the files/folders from the zip file after FileInstall.  You can Run the extractor or use Shell.Application COM object to extract the .zip without external application. 

Edit : Also remember that there is a size limit of 2GB on FileInstall

Edited by Nine
Link to comment
Share on other sites

4 minutes ago, DanielRossinsky said:

I thought that FileInstall should extracts the archives from its help page.

Here an example with 7za.exe :

$s7ZipDir = @ScriptDir
$sFile    = "Testfile.zip" ; the file, that was embedded via FileInstall
$sDestDir = @ScriptDir & "\mysoftware"
$sCommand = $s7ZipDir & '\7za.exe x "' & $sFile & '" -o"' & $sDestDir & '" -aoa -y'
RunWait ($sCommand, "", @SW_HIDE) ; or @SW_SHOW

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

@Nine is of course right when he says, that you can also use the Windows built-in zip functionality. I usually take the standalone versions of 7Zip, but this is just my personal preference :).

I have attached the required 7Zip modules. You have to embed them into your script using FileInstall. A nice feature is that with 7zG.exe a progress bar is displayed.

Example :

$s7ZipDir = @ScriptDir
$sFile    = "Testfile.zip" ; the file, that was embedded via FileInstall
$sDestDir = @ScriptDir & "\mysoftware"
$sCommand = $s7ZipDir & '\7zG.exe x "' & $sFile & '" -o"' & $sDestDir & '" -aoa -y'
RunWait ($sCommand, "", @SW_SHOW)

EDIT : Attachment removed, because it burdens the allowed upload quota of 8 MB too much. Please download the files from the Developer's website.

 

Edited by Musashi

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

@Musashi It is true that I always try to avoid downloading stuff if I can easily use built-in functionality (old fashion way).  So to give OP a perspective of choices, here the unzip function that I use :

#include <Constants.au3>
#include <String.au3>

Opt("MustDeclareVars", 1)

Const $sZipFile = @ScriptDir & "\Test.zip"
Const $sFolder = @ScriptDir & "\Temp"

DirRemove ($sFolder, $DIR_REMOVE)
UnZip($sZipFile, $sFolder)
If @error Then Exit MsgBox ($MB_SYSTEMMODAL,"","Error unzipping files : " & @error)

Func UnZip($sZipFile, $sDestFolder)
  If Not FileExists($sZipFile) Then Return SetError (1) ; source file does not exists
  If Not FileExists($sDestFolder) Then
    If Not DirCreate($sDestFolder) Then Return SetError (2) ; unable to create destination
  Else
    If Not StringInStr(FileGetAttrib($sDestFolder), "D") Then Return SetError (3) ; destination not folder
  EndIf
  Local $oShell = ObjCreate("shell.application")
  Local $oZip = $oShell.NameSpace($sZipFile)
  Local $iZipFileCount = $oZip.items.Count
  If Not $iZipFileCount Then Return SetError (4) ; zip file empty
  For $oFile In $oZip.items
    $oShell.NameSpace($sDestFolder).copyhere($ofile)
  Next
EndFunc   ;==>UnZip

 

Link to comment
Share on other sites

  • Moderators

Moved to the appropriate forum.

Moderation Team

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

3 hours ago, Skysnake said:

Added to the Wiki

:) Also got the zip counterpart of it if you want for the Wiki (which I find quite interesting indeed).

Edited by Nine
Link to comment
Share on other sites

2 hours ago, Skysnake said:

Please send?

Here the full example :

#include <Constants.au3>
#include <String.au3>

Opt("MustDeclareVars", 1)

Const $sZipFile = @ScriptDir & "\Test.zip"
Const $sFolder = @ScriptDir & "\Temp"
Const $sZipNew = @ScriptDir & "\Test2.zip"

DirRemove ($sFolder, $DIR_REMOVE)
UnZip($sZipFile, $sFolder)
If @error Then Exit MsgBox ($MB_SYSTEMMODAL,"","Error unzipping files : " & @error)
FileDelete ($sZipNew)
Zip ($sZipNew, $sFolder)
If @error Then Exit MsgBox ($MB_SYSTEMMODAL,"","Error zipping files : " & @error)

Func UnZip($sZipFile, $sDestFolder)
  If Not FileExists($sZipFile) Then Return SetError (1) ; source file does not exists
  If Not FileExists($sDestFolder) Then
    If Not DirCreate($sDestFolder) Then Return SetError (2) ; unable to create destination
  Else
    If Not StringInStr(FileGetAttrib($sDestFolder), "D") Then Return SetError (3) ; destination not folder
  EndIf
  Local $oShell = ObjCreate("shell.application")
  Local $oZip = $oShell.NameSpace($sZipFile)
  Local $iZipFileCount = $oZip.items.Count
  If Not $iZipFileCount Then Return SetError (4) ; zip file empty
  For $oFile In $oZip.items
    $oShell.NameSpace($sDestFolder).copyhere($ofile)
  Next
EndFunc   ;==>UnZip

Func Zip ($sZipFile, $sSourceFolder)
  If FileExists($sZipFile) Then Return SetError (1) ; destination file already exists
  If Not FileExists($sSourceFolder) Then Return SetError (2) ; source does not exist
  Local $hFile = FileOpen ($sZipFile, $FO_CREATEPATH+$FO_OVERWRITE+$FO_BINARY)
  Local Const $sString = Chr(80) & Chr(75) & Chr(5) & Chr(6) & _StringRepeat (Chr(0), 18) ; create ZIP file signature
  FileWrite ($hFile, $sString)
  FileClose($hFile)
  Local $oShell = ObjCreate("shell.application")
  If Not $oShell.NameSpace ($sSourceFolder).items.count Then Return SetError (3) ; folder empty
  Local $iFiles = 0
  For $oFile In $oShell.NameSpace($sSourceFolder).items
    $oShell.NameSpace($sZipFile).copyhere($oFile)
    Do
      Sleep (100) ; let the file being copied to ZIP
    Until $oShell.NameSpace($sZipFile).items.count > $iFiles
    $iFiles = $oShell.NameSpace($sZipFile).items.count
  Next
EndFunc

 

Edited by Nine
corrected a small bug
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

×
×
  • Create New...