How to create a ZIP file with only AutoHotkey code

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
JoeWinograd
Posts: 2179
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

How to create a ZIP file with only AutoHotkey code

Post by JoeWinograd » 01 Oct 2022, 19:20

Hi Folks,

I'm using the FileCopyDir capability (new in v1.1.34+) to unpack ZIP files, retaining the source folder/subfolder structure. Now I'm looking for AHK code to create ZIP files. It must be able to pack a folder and all its subfolders to an unlimited depth, including empty folders, such that the new FileCopyDir capability can unpack the ZIP file into the original folder/subfolder structure (btw, FileCopyDir handles empty folders fine). It must work on W7 through W11.

The thread referenced above has a post referring to some old threads. I tried the code in several posts there, as well other code from searching this forum, but haven't found anything yet that works well and meets my objectives. I wrote some AHK code that runs 7-Zip (7za.exe) to create ZIP files (works fine), and I saw some forum posts with code that calls PowerShell to do it, but I don't want to use either of those methods.

Any ideas? Thanks much, Joe


User avatar
JoeWinograd
Posts: 2179
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: How to create a ZIP file with only AutoHotkey code

Post by JoeWinograd » 01 Oct 2022, 22:17

Hi flyingDman,

That's based on one of the old threads I was referring to:
https://www.autohotkey.com/board/topic/60706-native-zip-and-unzip-xpvista7-ahk-l/

I haven't tried mikeyww's revision of it, but when I played with Shajul's code at that thread, and other revisions of it there, none of them could handle an empty folder. Here's what happens with the scripts there:

shajul fails on empty folder.png
shajul fails on empty folder.png (7.33 KiB) Viewed 1403 times

mikeyww's revision of it doesn't seem to address that and, in fact, doesn't even seem to handle folders and subfolders. Regards, Joe

lexikos
Posts: 9559
Joined: 30 Sep 2013, 04:07
Contact:

Re: How to create a ZIP file with only AutoHotkey code

Post by lexikos » 02 Oct 2022, 00:29

As far as I can tell, "compressed (zipped) folders" cannot add an empty directory to a zip file. If you add a non-empty folder and then remove its contents from the zip, the folder is removed as well.

That aside, the "CopyHere" method can take a collection of shell items rather than a filename. With Shell := ComObjCreate("Shell.Application"), something like Shell.Namespace(0).ParseName(FullPath) will give you a FolderItem corresponding to FullPath, which can be a directory. If you pass that to CopyHere, it will copy the entire directory (minus any empty directories), preserving the directory structure. Passing Shell.NameSpace(FullPath).Items instead would copy the contents of the directory (still preserving the structure if there are subdirectories).

The only other option included with Windows, as far as I am aware, is the .NET API used by PowerShell (ZipArchive or ZipFile). I believe it requires .NET Framework 4.5 or later, which is not included in a stock Windows 7 installation.

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: How to create a ZIP file with only AutoHotkey code

Post by mikeyww » 02 Oct 2022, 07:18

A ZIP file can contain an empty folder, but I'm not sure if that helps the situation!
Attachments
t.zip
Example of ZIP file with empty folder
(206 Bytes) Downloaded 70 times

User avatar
JoeWinograd
Posts: 2179
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: How to create a ZIP file with only AutoHotkey code

Post by JoeWinograd » 02 Oct 2022, 09:53

Hi lexikos,

7-Zip and PKZIP can create zip files with empty folders (and the new FileCopyDir can un-zip them).

Thanks for the CopyHere idea. I'll give it a look. Re the .NET API idea, I need it to work on a stock W7, so I'll have to pass on that one.

Regards, Joe

lexikos
Posts: 9559
Joined: 30 Sep 2013, 04:07
Contact:

Re: How to create a ZIP file with only AutoHotkey code

Post by lexikos » 03 Oct 2022, 01:07

I was aware about 7-zip, having used it to confirm that the zip format itself supports empty folders.

For detecting when CopyHere finishes, I recently encountered a more reliable method (that turned out to be unnecessary because I was extracting files and not compressing). This topic prompted me to complete a proof of concept - SHInstanceExplorer - but it is for v2. There is an example for zipping a folder (or its contents) that would easily translate to v1 (basically just replace ComObject with ComObjCreate). The class itself would take more work to translate, but it is just for detecting when CopyHere finishes.

User avatar
JoeWinograd
Posts: 2179
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: How to create a ZIP file with only AutoHotkey code

Post by JoeWinograd » 03 Oct 2022, 17:03

Thanks for the SHInstanceExplorer proof of concept...much appreciated! Regards, Joe

Aaronn700
Posts: 1
Joined: 23 Sep 2023, 03:43
Contact:

Re: How to create a ZIP file with only AutoHotkey code

Post by Aaronn700 » 24 Sep 2023, 00:26

It sounds like you've been exploring methods for working with 7-Zip and have encountered a reliable way to detect when the "CopyHere" operation finishes.

Post Reply

Return to “Ask for Help (v1)”