-
Notifications
You must be signed in to change notification settings - Fork 71
/
SmartZip.ahk
92 lines (76 loc) · 2.33 KB
/
SmartZip.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
;; --------- THE FUNCTION ------------------------------------
/*
SmartZip()
Smart ZIP/UnZIP files
Parameters:
s, o When compressing, s is the dir/files of the source and o is ZIP filename of object. When unpressing, they are the reverse.
t The options used by CopyHere method. For availble values, please refer to: http://msdn.microsoft.com/en-us/library/windows/desktop/bb787866
Link:
http://www.autohotkey.com/forum/viewtopic.php?p=523649#523649
*/
/*
SetWorkingDir, %A_ScriptDir%
; Support flexible form of parameters
SmartZip("dir1", "test.zip") ; Pack the whole folder to ZIP file
;~ SmartZip("*.ahk", "scripts.zip") ; Pack ahk scripts of the working dir
;~ SmartZip("*.zip", "package.zip") ; Pack a number of ZIP files to one
;~ SmartZip("*.zip", "dir2") ; Unpack a number of ZIP files to dir2
;~ SmartZip("*.zip", "") ; Unpack to the working dir
return
*/
SmartZip(s, o, t = 4)
{
IfNotExist, %s%
return, -1 ; The souce is not exist. There may be misspelling.
oShell := ComObjCreate("Shell.Application")
if (SubStr(o, -3) = ".zip") ; Zip
{
IfNotExist, %o% ; Create the object ZIP file if it's not exist.
CreateZip(o)
Loop, %o%, 1
sObjectLongName := A_LoopFileLongPath
oObject := oShell.NameSpace(sObjectLongName)
Loop, %s%, 1
{
if (sObjectLongName = A_LoopFileLongPath)
{
continue
}
ToolTip, Zipping %A_LoopFileName% ..
oObject.CopyHere(A_LoopFileLongPath, t)
SplitPath, A_LoopFileLongPath, OutFileName
Loop
{
oObject := "", oObject := oShell.NameSpace(sObjectLongName) ; This doesn't affect the copyhere above.
if oObject.ParseName(OutFileName)
break
}
}
ToolTip
}
else if InStr(FileExist(o), "D") or (!FileExist(o) and (SubStr(s, -3) = ".zip")) ; Unzip
{
if !o
o := A_ScriptDir ; Use the working dir instead if the object is null.
else IfNotExist, %o%
FileCreateDir, %o%
Loop, %o%, 1
sObjectLongName := A_LoopFileLongPath
oObject := oShell.NameSpace(sObjectLongName)
Loop, %s%, 1
{
oSource := oShell.NameSpace(A_LoopFileLongPath)
oObject.CopyHere(oSource.Items, t)
}
}
}
CreateZip(n) ; Create empty Zip file
{
ZIPHeader1 := "PK" . Chr(5) . Chr(6)
VarSetCapacity(ZIPHeader2, 18, 0)
ZIPFile := FileOpen(n, "w")
ZIPFile.Write(ZIPHeader1)
ZIPFile.RawWrite(ZIPHeader2, 18)
ZIPFile.close()
}