네트워커의 보안 티스토리

#NoEnv
SetWorkingDir, %A_ScriptDir%
 ;SmartZip("test.zip", "dir2")   ; Unpack a number of ZIP files to dir2
; Support flexible form of parameters
SmartZip("dir2", "testdir.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
;; ---------    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
*/

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()
}
;; ---------    FUNCTION END   ------------------------------------

 

;출처: https://autohotkey.com/board/topic/60706-native-zip-and-unzip-xpvista7-ahk-l/page-2

;윈도우 10 64비트 환경에서 실행해서 테스트 완료.