TwoCanoe Posted December 28, 2024 Posted December 28, 2024 Hello all, festive greetings. I am trying to zip/unzip files using the code snippet: https://www.autoitscript.com/wiki/Snippets_(_Files_%26_Folders_)#Windows_-_Built_In_ZIP_and_Unzip_(Windows_Shell) Here is my test script: expandcollapse popup#include <Constants.au3> #include <String.au3> Opt("MustDeclareVars", 1) Const $sZipFile = @ScriptDir & "\Test.zip" Const $sFolder = @ScriptDir & "\Temp" Const $sZipNew = @ScriptDir & "\Test2.zip" Zip("ZipTest.zip", @ScriptDir) ; My Test Zip DirRemove ($sFolder, $DIR_REMOVE) UnZip($sZipFile, $sFolder) If @error Then Exit MsgBox ($MB_SYSTEMMODAL,"","Error unzipping files : " & @error) FileDelete ($sZipNew) Zip ($sZipNew, $sFolder) If @error Then Exit MsgBox ($MB_SYSTEMMODAL,"","Error zipping files : " & @error) Func UnZip($sZipFile, $sDestFolder) If Not FileExists($sZipFile) Then Return SetError (1) ; source file does not exists If Not FileExists($sDestFolder) Then If Not DirCreate($sDestFolder) Then Return SetError (2) ; unable to create destination Else If Not StringInStr(FileGetAttrib($sDestFolder), "D") Then Return SetError (3) ; destination not folder EndIf Local $oShell = ObjCreate("shell.application") Local $oZip = $oShell.NameSpace($sZipFile) Local $iZipFileCount = $oZip.items.Count If Not $iZipFileCount Then Return SetError (4) ; zip file empty For $oFile In $oZip.items $oShell.NameSpace($sDestFolder).copyhere($ofile) Next EndFunc ;==>UnZip Func Zip ($sZipFile, $sSourceFolder) If FileExists($sZipFile) Then Return SetError (1) ; destination file already exists If Not FileExists($sSourceFolder) Then Return SetError (2) ; source does not exist Local $hFile = FileOpen ($sZipFile, $FO_CREATEPATH+$FO_OVERWRITE+$FO_BINARY) Local Const $sString = Chr(80) & Chr(75) & Chr(5) & Chr(6) & _StringRepeat (Chr(0), 18) ; create ZIP file signature FileWrite ($hFile, $sString) FileClose($hFile) Local $oShell = ObjCreate("shell.application") If Not $oShell.NameSpace ($sSourceFolder).items.count Then Return SetError (3) ; folder empty Local $iFiles = 0 For $oFile In $oShell.NameSpace($sSourceFolder).items $oShell.NameSpace($sZipFile).copyhere($oFile) Do Sleep (100) ; let the file being copied to ZIP Until $oShell.NameSpace($sZipFile).items.count > $iFiles $iFiles = $oShell.NameSpace($sZipFile).items.count Next EndFunc The script throws an error: --------------------------- AutoIt Error --------------------------- Line 46 (File "G:\speech backup\zip test.au3"): $oShell.NameSpace($sZipFile).copyhere($oFile) $oShell.NameSpace($sZipFile)^ ERROR Error: The requested action with this object has failed. --------------------------- OK --------------------------- When I run the script, the zip file is created, though it is an empty zip file. Does anyone have any thoughts?
Solution ioa747 Posted December 28, 2024 Solution Posted December 28, 2024 try expandcollapse popup#include <Constants.au3> #include <String.au3> Opt("MustDeclareVars", 1) Const $sFolder = @ScriptDir & "\ScriptDir" Const $sZipNew = @ScriptDir & "\ScriptDir.zip" DirCopy(@ScriptDir, $sFolder) Zip($sZipNew, $sFolder) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "", "Error zipping files : " & @error) UnZip($sZipNew, $sFolder & "_extracted") If @error Then Exit MsgBox($MB_SYSTEMMODAL, "", "Error unzipping files : " & @error) ShellExecute(@ScriptDir) Func UnZip($sZipFile, $sDestFolder) If Not FileExists($sZipFile) Then Return SetError(1) ; source file does not exists If Not FileExists($sDestFolder) Then If Not DirCreate($sDestFolder) Then Return SetError(2) ; unable to create destination Else If Not StringInStr(FileGetAttrib($sDestFolder), "D") Then Return SetError(3) ; destination not folder EndIf Local $oShell = ObjCreate("shell.application") Local $oZip = $oShell.NameSpace($sZipFile) Local $iZipFileCount = $oZip.items.Count If Not $iZipFileCount Then Return SetError(4) ; zip file empty For $oFile In $oZip.items $oShell.NameSpace($sDestFolder).copyhere($ofile) Next EndFunc ;==>UnZip Func Zip($sZipFile, $sSourceFolder) If FileExists($sZipFile) Then Return SetError(1) ; destination file already exists If Not FileExists($sSourceFolder) Then Return SetError(2) ; source does not exist Local $hFile = FileOpen($sZipFile, $FO_CREATEPATH + $FO_OVERWRITE + $FO_BINARY) Local Const $sString = Chr(80) & Chr(75) & Chr(5) & Chr(6) & _StringRepeat(Chr(0), 18) ; create ZIP file signature FileWrite($hFile, $sString) FileClose($hFile) Local $oShell = ObjCreate("shell.application") If Not $oShell.NameSpace($sSourceFolder).items.count Then Return SetError(3) ; folder empty Local $iFiles = 0 For $oFile In $oShell.NameSpace($sSourceFolder).items $oShell.NameSpace($sZipFile).copyhere($oFile) Do Sleep(100) ; let the file being copied to ZIP Until $oShell.NameSpace($sZipFile).items.count > $iFiles $iFiles = $oShell.NameSpace($sZipFile).items.count Next EndFunc ;==>Zip I know that I know nothing
TwoCanoe Posted December 28, 2024 Author Posted December 28, 2024 Thanks again ioa747. Works fine, though leaves the 'temp' folder behind. Easy enough to sort. Thanks again. ioa747 1
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now