Popular Post argumentum Posted Monday at 05:08 PM Popular Post Posted Monday at 05:08 PM (edited) expandcollapse popup#include <File.au3> If Not FileGetSize(@ScriptDir & "\MyProjectZipper.au3") Then ; create it for the example FileWriteLine(@ScriptDir & "\MyProjectZipper.au3", FileRead(@ScriptFullPath)) EndIf ; if one don't work the other should ;~ Exit PackMyProject() Exit PackMyProjectViaPowershell() Func PackMyProject() ; these are the files to add: ;~ Local $aFiles = StringSplit("MyProjectZipper.au3|MyOtherFile.au3", "|", 1) Local $aFiles = StringSplit("MyProjectZipper.au3", "|", 1) ; this is the comment with version and link Local $sCommentText = "https://www.autoitscript.com/forum/topic/213715-zip-your-project-nicely/" & @CRLF & @CRLF & "MyProject v0.0.0.3" ; my filename Local $sArchiveName = "MyProjectZipper-v0.0.0.3.zip" #Region Create an empty ZIP container Local $oMyError, $hFile = FileOpen(_PathFull(@ScriptDir & '\' & $sArchiveName), 18) If $hFile = -1 Then Return SetError(101, 101, 101) FileWrite($hFile, Binary("0x504B0506000000000000000000000000000000000000")) FileClose($hFile) Sleep(300) ; make sure that the handle was released If FileGetSize(_PathFull(@ScriptDir & '\' & $sArchiveName)) <> 22 Then Return SetError(107, 107, 107) ; Zip signature: 22 bytes #EndRegion Create an empty ZIP container #Region Add files using Windows Shell If ObjEvent("AutoIt.Error") = "" And FuncName(ObjEvent("AutoIt.Error")) = "" Then $oMyError = ObjEvent("AutoIt.Error", MyProjectErrFunc) Local $iFiles, $iWaiting = 0, $oShell = ObjCreate("Shell.Application") If Not IsObj($oShell) Then Return SetError(102, 102, 102) Sleep(100) ; give it time to set permissions Local $oZipFolder = $oShell.NameSpace(_PathFull(@ScriptDir & '\' & $sArchiveName)) If Not IsObj($oZipFolder) Then Return SetError(103, 103, 103) Sleep(100) ; give it time to set permissions For $iFiles = 1 To $aFiles[0] Sleep(50) ; shouldn't need this, but it won't hurt either ConsoleWrite((FileGetSize($aFiles[$iFiles]) ? '+' : '!') & ' File #' & $iFiles & @TAB & $aFiles[$iFiles] & @CRLF) If Not FileGetSize($aFiles[$iFiles]) Then Return SetError(104, 104, 104) $oZipFolder.CopyHere(_PathFull(@ScriptDir & '\' & $aFiles[$iFiles])) Next While $oZipFolder.Items().Count <> $aFiles[0] $iWaiting += 1 If $iWaiting > 10 Then Return SetError(105, 105, 105) ; ..taking too long :( Sleep(300) ConsoleWrite('..patiently waiting.. (' & $oZipFolder.Items().Count & ')' & @CRLF) WEnd Sleep(300) ; don't rush the background zipping process #EndRegion Add files using Windows Shell ; add a comment with the version number If Not AddZipComment($sArchiveName, $sCommentText) Then Return SetError(106, 106, 106) ConsoleWrite('+ ...all good.' & @CRLF) ShellExecute(_PathFull(@ScriptDir & '\' & $sArchiveName)) EndFunc ;==>PackMyProject ;~ Exit PackMyProjectViaPowershell() Func PackMyProjectViaPowershell() ; these are the files to add: ;~ Local $aFiles = StringSplit("MyProjectZipper.au3|MyOtherFile.au3", "|", 1) Local $iFile, $aFiles = StringSplit("MyProjectZipper.au3", "|", 1) ; this is the comment with version and link Local $sCommentText = "https://www.autoitscript.com/forum/topic/213715-zip-your-project-nicely/" & @CRLF & @CRLF & "MyProject v0.0.0.4" ; my filename Local $sArchiveName = "MyProjectZipper-v0.0.0.4.zip" #Region Create the ZIP file using PowerShell Local $sPSCommand = 'powershell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -Command "Compress-Archive -Path ' For $iFile = 1 To $aFiles[0] If $iFile > 1 Then $sPSCommand &= ", " $sPSCommand &= "'" & _PathFull(@ScriptDir & "\" & $aFiles[$iFile]) & "'" Next $sPSCommand &= ' -DestinationPath ''' & _PathFull(@ScriptDir & "\" & $sArchiveName) & ''' -Force"' Local $iPID = Run($sPSCommand, "", @SW_HIDE) ProcessWaitClose($iPID) ; This safely blocks the script until PowerShell completely finishes zipping If Not FileGetSize($sArchiveName) Then Return SetError(101, 101, 101) #EndRegion Create the ZIP file using PowerShell ; add a comment with the version number If Not AddZipComment(_PathFull(@ScriptDir & "\" & $sArchiveName), $sCommentText) Then Return SetError(106, 106, 106) ConsoleWrite('+ ...all good.' & @CRLF) ShellExecute(_PathFull(@ScriptDir & '\' & $sArchiveName)) EndFunc ;==>PackMyProjectViaPowershell Func MyProjectErrFunc($oMyError) ConsoleWrite('@@(' & $oMyError.scriptline & ') : 0x' & Hex($oMyError.number, 8) & ' : ' & $oMyError.windescription & @CRLF) EndFunc ;==>MyProjectErrFunc Func AddZipComment($sFilePath, $sCommentText) ; add or replace the comment Local $hFileRead = FileOpen($sFilePath, 16) If $hFileRead = -1 Then Return SetError(1, 1, False) ; could not read the file Local $bFullData = FileRead($hFileRead) FileClose($hFileRead) Local $sHexData = String($bFullData) Local $iEocdPos = StringInStr($sHexData, "504B0506", 0, -1) If $iEocdPos = 0 Then Return SetError(2, 2, False) ; is not a ZIP file Local $iEocdByteOffset = ($iEocdPos - 3) / 2 Local $bComment = Binary(StringToBinary($sCommentText, 4)) Local $iCommentLength = BinaryLen($bComment) If $iCommentLength > 65535 Then $iCommentLength = 65535 Local $sNewLengthHex = Hex(Mod($iCommentLength, 256), 2) & Hex(Int($iCommentLength / 256), 2) Local $bLeftPart = BinaryMid($bFullData, 1, $iEocdByteOffset + 20) Local $bFinalZipData = $bLeftPart & Binary("0x" & $sNewLengthHex) & $bComment Local $hFileWrite = FileOpen($sFilePath, 18) If $hFileWrite = -1 Then Return SetError(3, 3, False) ; failed to overwrite FileWrite($hFileWrite, $bFinalZipData) FileClose($hFileWrite) Return True EndFunc ;==>AddZipComment That will make something like this: MyProjectZipper-v0.0.0.4.zip I'm sure your project will have a different name. 🤪 I'm always wishing that the files that I get from the forum had a link to where it was at, and the version. It's very common for people to use 7Zip and that doesn't let me add my own notes to it. So, with this script, you don't have to have any packer. Just run it and it will do it for you. Yes, the file will be slightly bigger, but anyone can decompress it in any Windows system and add a comment to the file if so they wish ( but of course, it's going to have all the information one might need ) 🤔 I added this to the examples area because, I couldn't find a rants area 😅 Q: ..it ain't working. A: hmmm, maybe: 1) If you can right-click an empty area on the desktop, go to New, and do NOT see "Compressed (zipped) Folder" in the menu, Windows ZIP engine is stripped out or disabled in the registry. 2) An Admin / User level thing ? 3) Aggressive Antivirus / Controlled Folder Access (OneDrive) ? It should work 🤷♂️ If PackMyProject() don't work in that PC, then try PackMyProjectViaPowershell() function. One of them should work. Q: If I ZIP the file with something else, can I add the comment with AddZipComment() A: Yes. Is unrelated to any windows engine. It reads the file to memory, adds the comment and overwrites it. Code zipped won't make a large file, so it should fit in memory. Edited Tuesday at 04:39 PM by argumentum v0.0.0.4 Parsix, donnyh13, ioa747 and 2 others 5 Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now