Jump to content

Recommended Posts

Posted (edited)
#include <File.au3>

If Not FileGetSize(@ScriptDir & "\MyProjectZipper.au3") Then
    FileWriteLine(@ScriptDir & "\MyProjectZipper.au3", FileRead(@ScriptFullPath))
EndIf

Exit PackMyProject()
Func PackMyProject()

    ; these are the files to add:
    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.2"

    ; my filename
    Local $sArchiveName = "MyProjectZipper-v0.0.0.2.zip"

    #Region Create an empty ZIP container
    Local $hFile = FileOpen(_PathFull(@ScriptDir & '\' & $sArchiveName), 18)
    If $hFile = -1 Then Exit 101
    FileWrite($hFile, Binary("0x504B0506000000000000000000000000000000000000"))
    FileClose($hFile)
    Sleep(300) ; make sure that the handle was released
    If FileGetSize(_PathFull(@ScriptDir & '\' & $sArchiveName)) <> 22 Then Exit 107 ; Zip signature: 22 bytes
    #EndRegion Create an empty ZIP container

    #Region Add files using Windows Shell
    Local $oShell = ObjCreate("Shell.Application")
    If Not IsObj($oShell) Then Exit 102
    Sleep(100) ; give it time to set permissions
    Local $oZipFolder = $oShell.NameSpace(_PathFull(@ScriptDir & '\' & $sArchiveName))
    If Not IsObj($oZipFolder) Then Exit 103
    Sleep(100) ; give it time to set permissions
    For $n = 1 To $aFiles[0]
        Sleep(50) ; shouldn't need this, but it won't hurt either
        ConsoleWrite((FileGetSize($aFiles[$n]) ? '+' : '!') & ' File #' & $n & @TAB & $aFiles[$n] & @CRLF)
        If Not FileGetSize($aFiles[$n]) Then Exit 104
        $oZipFolder.CopyHere(_PathFull(@ScriptDir & '\' & $aFiles[$n]))
    Next

    Local $iWaiting = 0
    While $oZipFolder.Items().Count <> $aFiles[0]
        $iWaiting += 1
        If $iWaiting > 10 Then Exit 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 Exit 106

    ConsoleWrite('+ ...all good.' & @CRLF)
    ShellExecute(_PathFull(@ScriptDir & '\' & $sArchiveName))
EndFunc   ;==>PackMyProject

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.2.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 🤷‍♂️

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 in memory, adds the comment and overwrites it.

Edited by argumentum
v0.0.0.2

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting  image.gif.922e3a93535f431de08b31ee669cc446.gif
autoit_scripter_blue_userbar.png

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
×
×
  • Create New...