Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Native Zip and Unzip XP/Vista/7 [AHK_L]


  • Please log in to reply
28 replies to this topic
shajul
  • Members
  • 571 posts
  • Last active: Aug 01 2015 03:45 PM
  • Joined: 15 Sep 2006
This version for Autohotkey_L was inspired by the script at Zip/Unzip using native ZipFolder Feature in XP by Sean.

Features:
1. Zip/Unzip natively on any Windows > XP
2. Zip file(s)/folder(s)/wildcard pattern files
3. Unzip destination folder created if !Exists [Thanks Sean]

Credits:
Sean - for original idea


Functions with examples:
/*           ,---,                                          ,--,    
           ,--.' |                                        ,--.'|    
           |  |  :                      .--.         ,--, |  | :    
  .--.--.  :  :  :                    .--,`|       ,'_ /| :  : '    
 /  /    ' :  |  |,--.  ,--.--.       |  |.   .--. |  | : |  ' |    
|  :  /`./ |  :  '   | /       \      '--`_ ,'_ /| :  . | '  | |    
|  :  ;_   |  |   /' :.--.  .-. |     ,--,'||  ' | |  . . |  | :    
 \  \    `.'  :  | | | \__\/: . .     |  | '|  | ' |  | | '  : |__  
  `----.   \  |  ' | : ," .--.; |     :  | |:  | : ;  ; | |  | '.'| 
 /  /`--'  /  :  :_:,'/  /  ,.  |   __|  : ''  :  `--'   \;  :    ; 
'--'.     /|  | ,'   ;  :   .'   \.'__/\_: |:  ,      .-./|  ,   /  
  `--'---' `--''     |  ,     .-./|   :    : `--`----'     ---`-'   
                      `--`---'     \   \  /                         
                                    `--`-'  
Zip/Unzip file(s)/folder(s)/wildcard pattern files
Requires: Autohotkey_L, Windows > XP
URL: http://www.autohotkey.com/forum/viewtopic.php?t=65401
Credits: Sean for original idea
*/

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

;; --------- 	EXAMPLE CODE	-------------------------------------
FilesToZip = D:\Projects\AHK\_Temp\Test\  ;Example of folder to compress
; FilesToZip = D:\Projects\AHK\_Temp\Test\*.ahk  ;Example of wildcards to compress
; FilesToZip := A_ScriptFullPath   ;Example of file to compress
sZip := A_ScriptDir . "\Test.zip"  ;Zip file to be created
sUnz := A_ScriptDir . "\ext\"      ;Directory to unzip files

Zip(FilesToZip,sZip)
Sleep, 500
Unz(sZip,sUnz)
;; --------- 	END EXAMPLE 	-------------------------------------



;; ----------- 	THE FUNCTIONS   -------------------------------------
Zip(FilesToZip,sZip)
{
If Not FileExist(sZip)
	CreateZipFile(sZip)
psh := ComObjCreate( "Shell.Application" )
pzip := psh.Namespace( sZip )
if InStr(FileExist(FilesToZip), "D")
	FilesToZip .= SubStr(FilesToZip,0)="\" ? "*.*" : "\*.*"
loop,%FilesToZip%,1
{
	zipped++
	ToolTip Zipping %A_LoopFileName% ..
	pzip.CopyHere( A_LoopFileLongPath, 4|16 )
	Loop
	{
		done := pzip.items().count
		if done = %zipped%
			break
	}
	done := -1
}
ToolTip
}

CreateZipFile(sZip)
{
	Header1 := "PK" . Chr(5) . Chr(6)
	VarSetCapacity(Header2, 18, 0)
	file := FileOpen(sZip,"w")
	file.Write(Header1)
	file.RawWrite(Header2,18)
	file.close()
}

Unz(sZip, sUnz)
{
    fso := ComObjCreate("Scripting.FileSystemObject")
    If Not fso.FolderExists(sUnz)  ;http://www.autohotkey.com/forum/viewtopic.php?p=402574
       fso.CreateFolder(sUnz)
    psh  := ComObjCreate("Shell.Application")
    zippedItems := psh.Namespace( sZip ).items().count
    psh.Namespace( sUnz ).CopyHere( psh.Namespace( sZip ).items, 4|16 )
    Loop {
        sleep 50
        unzippedItems := psh.Namespace( sUnz ).items().count
        ToolTip Unzipping in progress..
        IfEqual,zippedItems,%unzippedItems%
            break
    }
    ToolTip
}
;; ----------- 	END FUNCTIONS   -------------------------------------

Edit1: Added support for wildcards in path
Edit2: Greatly improved zipping speed (Removed sleep from zip fn)


Options for zipping, unzipping:

/*
4 Do not display a progress dialog box.
8 Give the file being operated on a new name in a move, copy, or rename operation if a file with the target name already exists.
16 Respond with "Yes to All" for any dialog box that is displayed.
64 Preserve undo information, if possible.
128 Perform the operation on files only if a wildcard file name (*.*) is specified.
256 Display a progress dialog box but do not show the file names.
512 Do not confirm the creation of a new directory if the operation requires one to be created.
1024 Do not display a user interface if an error occurs.
2048 Version 4.71. Do not copy the security attributes of the file.
4096 Only operate in the local directory. Don't operate recursively into subdirectories.
9182 Version 5.0. Do not move connected files as a group. Only move the specified files.
*/



fincs
  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007
BinWrite is unnecessary, outdated and not compatible with x64; you can do that using FileOpen()/File objects and VarSetCapacity.

shajul
  • Members
  • 571 posts
  • Last active: Aug 01 2015 03:45 PM
  • Joined: 15 Sep 2006

BinWrite is unnecessary, outdated and not compatible with x64; you can do that using FileOpen()/File objects and VarSetCapacity.


Thanks for pointing out.. I have modified the script accordingly.

fincs
  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007
Oh and by the way, this is unneccessary as well since objects are automatically freed:
psh := ""


shajul
  • Members
  • 571 posts
  • Last active: Aug 01 2015 03:45 PM
  • Joined: 15 Sep 2006

Oh and by the way, this is unneccessary as well since objects are automatically freed:

psh := ""


Thanks, modified.

hoppfrosch
  • Members
  • 399 posts
  • Last active: Feb 26 2016 05:31 AM
  • Joined: 25 Jan 2006
Just started testing your module ...

Zipping via Wildcard does not seem to work:

; Folder C:\Test contains file test.zip
Zip ("C:\Test\*", "C:\test.zip")
Zip ("C:\Test\*.*", "C:\test.zip")
Zip ("C:\Test\*.txt", "C:\test.zip")

; All three variants lead to an error dialog box: "The given filename is invalid or too long  ... test.txt"

Also recursive zipping of whole directories does not work correctly and ends up in an infinite loop:
Zip ("C:\Test", "C:\test.zip")

The zip file is created corretly, but the tooltip "Zipping in progress.." does show forever - and the script never finishes (seems its never hitting the until-condition of your loop) (In my real example C:\test\ contains a complete directory-tree ...)

System:
Win7 Ultimate
Autohotkey(_L) 1.0.90.0

shajul
  • Members
  • 571 posts
  • Last active: Aug 01 2015 03:45 PM
  • Joined: 15 Sep 2006
You are right, this function was designed to only zip one folder or file, hence, i used "Until zippedItems=1"


Zip ("C:\Test", "C:\test.zip")
This works fine for me.. I dont know what is causing the problem on your side.

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

Zipping via Wildcard does not seem to work:

You are right, this function was designed to only zip one folder or file

This is not true. Don't jump to the conclusion from one example. This time, I'd rather not provide the details.

shajul
  • Members
  • 571 posts
  • Last active: Aug 01 2015 03:45 PM
  • Joined: 15 Sep 2006

Zipping via Wildcard does not seem to work:

Now it works, kindly see the updated function in the first post! :)

Zip ("C:\Test\*.*", "C:\test.zip")

This can be represented as
Zip("C:\Test", "C:\test.zip")

Also recursive zipping of whole directories does not work correctly and ends up in an infinite loop:

Thanks, corrected.

Thanks for testing the function.

@Sean, thanks for stimulating me to find out a way!

fincs
  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007

fso := ComObjCreate("Scripting.FileSystemObject")
    If Not fso.FolderExists(sUnz)  ;http://www.autohotkey.com/forum/viewtopic.php?p=402574
       fso.CreateFolder(sUnz)

Is there any reason why you don't do that with AHK commands?:
FileCreateDir, %sUnz% ; doesn't do anything if the folder exists


Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

Is there any reason why you don't do that with AHK commands?

To provide a more portable COM solution.

hoppfrosch
  • Members
  • 399 posts
  • Last active: Feb 26 2016 05:31 AM
  • Joined: 25 Jan 2006

Zipping via Wildcard does not seem to work:

Now it works, kindly see the updated function in the first post! :)

Also recursive zipping of whole directories does not work correctly and ends up in an infinite loop:

Thanks, corrected.


Confirmed by testing ...
Thanks for your quick fix.

shajul
  • Members
  • 571 posts
  • Last active: Aug 01 2015 03:45 PM
  • Joined: 15 Sep 2006

Confirmed by testing ...
Thanks for your quick fix.


Glad you like it. You can use Zip Unzip Easily [COM] [AHK_L] for even more control of the process..

  • Guests
  • Last active:
  • Joined: --
It doesn't seem to work with relative paths.

aaffe
  • Members
  • 1045 posts
  • Last active: Jan 16 2014 01:32 PM
  • Joined: 17 May 2007
Hy shajul,
I modified your script a little bit so you can run it with 3 parameters:
1. zip or unzip
2. zip file to create or to unzip
3. file or folder to zip OR folder where to extract the zip file
/*           ,---,                                          ,--,   
           ,--.' |                                        ,--.'|   
           |  |  :                      .--.         ,--, |  | :   
  .--.--.  :  :  :                    .--,`|       ,'_ /| :  : '   
 /  /    ' :  |  |,--.  ,--.--.       |  |.   .--. |  | : |  ' |   
|  :  /`./ |  :  '   | /       \      '--`_ ,'_ /| :  . | '  | |   
|  :  ;_   |  |   /' :.--.  .-. |     ,--,'||  ' | |  . . |  | :   
 \  \    `.'  :  | | | \__\/: . .     |  | '|  | ' |  | | '  : |__ 
  `----.   \  |  ' | : ," .--.; |     :  | |:  | : ;  ; | |  | '.'|
 /  /`--'  /  :  :_:,'/  /  ,.  |   __|  : ''  :  `--'   \;  :    ;
'--'.     /|  | ,'   ;  :   .'   \.'__/\_: |:  ,      .-./|  ,   / 
  `--'---' `--''     |  ,     .-./|   :    : `--`----'     ---`-'   
                      `--`---'     \   \  /                         
                                    `--`-' 
Zip/Unzip file(s)/folder(s)/wildcard pattern files
Requires: Autohotkey_L, Windows > XP
URL: http://www.autohotkey.com/forum/viewtopic.php?t=65401
Credits: Sean for original idea
*/

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

If (%0%<3)
{
  MsgBox,16,Keine Parameter übergeben!,Bitte rufen Sie %A_Scriptname% mit folgenden Parametern auf:`n1: "zip"|"unzip"`n2: "zip": zu erstellende Zip-Datei "unzip": zu entpackende Zip-Datei`n3: "zip": zu verpackende(s) Datei/Verzeichnis "unzip": Verzeichnis`, in das entpackt wird 
  ExitApp
}

was=%1%
zipname=%2%
datvz=%3%
;Msgbox Param1: %was%`nParam2: %zipname%`nParam3: %datvz%

If (was!="zip" and was!="unzip")
{
  MsgBox,16,Falscher 1. Parameter!,1. Parameter muss entweder "zip" oder "unzip" sein!
  ExitApp
}

If (was="unzip" and !FileExist(zipname) )
{
  msgBox,16,%zipname% existiert nicht!,%zipname% existiert nicht!
  ExitApp
}

If (was="zip" and !FileExist(datvz) )
{
  MsgBox,16,Fehler im 3. Parameter!,Datei/Verzeichnis %datvz% existiert nicht!
  ExitApp
}

If (was="zip")
  Zip(datvz,zipname)
If (was="unzip")
  Unz(zipname,datvz)
;; ---------    END EXAMPLE    -------------------------------------



;; -----------    THE FUNCTIONS   -------------------------------------
Zip(FilesToZip,sZip)
{
  If Not FileExist(sZip)
     CreateZipFile(sZip)
  psh := ComObjCreate( "Shell.Application" )
  pzip := psh.Namespace( sZip )
  if InStr(FileExist(FilesToZip), "D")
     FilesToZip .= SubStr(FilesToZip,0)="\" ? "*.*" : "\*.*"
  loop,%FilesToZip%,1
  {
     zipped++
     ToolTip Zipping %A_LoopFileName% ..
     pzip.CopyHere( A_LoopFileLongPath, 4|16 )
     Loop
     {
        done := pzip.items().count
        if done = %zipped%
           break
     }
     done := -1
  }
  ToolTip
}

CreateZipFile(sZip)
{
   Header1 := "PK" . Chr(5) . Chr(6)
   VarSetCapacity(Header2, 18, 0)
   file := FileOpen(sZip,"w")
   file.Write(Header1)
   file.RawWrite(Header2,18)
   file.close()
}

Unz(sZip, sUnz)
{
    fso := ComObjCreate("Scripting.FileSystemObject")
    If Not fso.FolderExists(sUnz)  ;http://www.autohotkey.com/forum/viewtopic.php?p=402574
       fso.CreateFolder(sUnz)
    psh  := ComObjCreate("Shell.Application")
    zippedItems := psh.Namespace( sZip ).items().count
    psh.Namespace( sUnz ).CopyHere( psh.Namespace( sZip ).items, 4|16 )
    Loop {
        sleep 50
        unzippedItems := psh.Namespace( sUnz ).items().count
        ToolTip Unzipping in progress..
        IfEqual,zippedItems,%unzippedItems%
            break
    }
    ToolTip
}
;; -----------    END FUNCTIONS   -------------------------------------