takg

Recommendations for zipping files and folders

12 posts in this topic

#1 ·  Posted (edited)

I have been using _Zip.au3 to put files in a zip container. I discovered a problem recently where the AutoIt script executable I created wasn't exiting after the zipping process was completed. The zip file appears to be created correctly but the executable just doesn't exit. Adding an Exit at the end of the script did nothing and there doesn't appear to be any errors except that the script appears to be paused (indicating a problem before the last line of code). This problem appears to be fixed by adding a Sleep(500) line after the _ZipCreate like this:

$Zip = _Zip_Create($myzip)
Sleep(500)
_Zip_AddItem($Zip,$myfolder)

I have no idea why this worked but perhaps if someone can explain why it works I will live with this workaround.

In any case, after reading all the comments in the forum (see thread started by torels with title "ZIP.au3 UDF in pure AutoIt") about _Zip.au3 I am wondering if I should be using it at all. It might be more desirable to use a command line from an AutoIt script to do this. Also, I am hesitant to use _Zip.au3 because it doesn't appear to be distributed with AutoIt (even though it has been around since 2008), it may have been copied from someone else, and it doesn't appear to have been updated since 2008.

Please recommend one of these options:

1) Continue using _Zip.au3

2) Use a different UDF for zip compression

3) Use a command line that just uses the zip features built into Windows. Before I consider this, I'd have to make sure the same command works in all current versions of Windows. Also, examples would be helpful (I saw an example on the thread listed above but can't find it now).

4) Other: it would be nice to have an option that can password protect the zip container. Does _Zip.au3 have it? I think it was recommended but it doesn't show up in _Zip.au3 feature list.

Thanks in advance for your recommendations.

Edited by takg

Share this post


Link to post
Share on other sites



What versions of Windows do you have in your environment?

#3 is a good choice but if you still have XP or something that could be a problem.

Share this post


Link to post
Share on other sites

Using Windows 7, 8 and 10. [No 9 because Windows ate 9 ;-)]

I am not sure Windows has a built-in command line zip utility. If not then this option won't work for me.

Also, the script failed again. I will try 2 seconds instead of half a second.

Share this post


Link to post
Share on other sites

Here is a before (Using Zip.UDF) and after (Using Com Object) the later worked much better for me.  You sort of need to engineer the reverse to create a zip.

 

Before:

#Include <Array.au3>
#Include <File.au3>
#Include <Zip.au3>

$sMaps = "C:\Tiburon\"  ;Where To Save Maps

$aZip = _FileListToArray("C:\Tiburon", "*.zip", $FLTA_FILES, True) ;Get A List of All .Zip Files
If @Error Then ;If No Downloads Exit
    ;MsgBox(0, "", @Error)
    Exit
Else ;Else Get The Highest Version Map .Zip Package and Save to $sZip and get Name and save in $sMap2
    ;_ArrayDisplay($aZip)
    $sZip = $aZip[$aZip[0]]
    $aMap = StringSplit($sZip, "\")
    $sMap2 = StringTrimRight($aMap[$aMap[0]], 4)
    $sMaps &= $sMap2
EndIf

$sLogPath = "C:\Tiburon\" & $sMap2 & ".log" ;Create Our Log File Path with $sMap2 as the Name
_FileWriteLog($sLogPath, "New Map Package " & $sMap2 & " was downloaded") ;Write To Log that The .Zip was downloaded
$vStep1 = DirRemove($sMaps, 1) ;If Zip was Already Extracted Delete As To Extract New (Prevent Corrupted Downloads)
If $vStep1 = 1 Then _FileWriteLog($sLogPath, "Deleted old " & $sMap2 & " folder") ;Write To Log That Old Folder Was Found and Deleted
$vStep2 = _Zip_UnzipAll($sZip, $sMaps, 1) ;Unzip Our File
If $vStep2 = 0 Then _FileWriteLog($sLogPath, "Successfully Unziped Package for " & $sMap2) ;Log That We Unzipped Our File
$vStep3 = FileDelete($sZip) ;Delete Zip Package When Finished
If $vStep3 = 1 Then _FileWriteLog($sLogPath, "Successfully Deleted " & $sMap2 & " .Zip Package") ;Log That We Deleted Our Zip File Successfully

 

After:

#Include <Array.au3>
#Include <File.au3>

$sMaps = "C:\Tiburon\"  ;Where To Save Maps

$aZip = _FileListToArray("C:\Tiburon", "*.zip", $FLTA_FILES, True) ;Get A List of All .Zip Files
If @Error Then ;If No Downloads Exit
    ;MsgBox(0, "", @Error)
    Exit
Else ;Else Get The Highest Version Map .Zip Package and Save to $sZip and get Name and save in $sMap2
    ;_ArrayDisplay($aZip)
    $sZip = $aZip[$aZip[0]]
    $aMap = StringSplit($sZip, "\")
    $sMap2 = StringTrimRight($aMap[$aMap[0]], 4)
    $sMaps &= $sMap2
EndIf

$sLogPath = "C:\Tiburon\" & $sMap2 & ".log" ;Create Our Log File Path with $sMap2 as the Name
_FileWriteLog($sLogPath, "New Map Package " & $sMap2 & " was downloaded") ;Write To Log that The .Zip was downloaded
$vStep1 = DirRemove($sMaps, 1) ;If Zip was Already Extracted Delete As To Extract New (Prevent Corrupted Downloads)
If $vStep1 = 1 Then _FileWriteLog($sLogPath, "Deleted old " & $sMap2 & " folder") ;Write To Log That Old Folder Was Found and Deleted
DirCreate($sMaps)
$objShell5 = ObjCreate("Shell.Application")
$FilesInZip5 = $objShell5.NameSpace($sZip).items
$objShell5.NameSpace($sMaps).CopyHere($FilesInZip5,0x4)
;If $vStep2 = 0 Then _FileWriteLog($sLogPath, "Successfully Unziped Package for " & $sMap2) ;Log That We Unzipped Our File
$vStep3 = FileDelete($sZip) ;Delete Zip Package When Finished
If $vStep3 = 1 Then _FileWriteLog($sLogPath, "Successfully Deleted " & $sMap2 & " .Zip Package") ;Log That We Deleted Our Zip File Successfully

 

Share this post


Link to post
Share on other sites

Using the Shell.Application COM Object qualifies as an Other (#4) option; I hadn't considered this option. I'll give it a try.

Since changing to Sleep(2000) it seems to work. If it stops working I'll just use the COM Object. 

Thanks for your help.

Share this post


Link to post
Share on other sites

I am aware there have been two ZIP UDF's here, and maybe others.

Both of those, I thought were using the Windows ZIP DLL that comes with the last few versions of Windows. Don't know what the current Windows' status is with that though.

I guess it depends on your requirements, but while I still use one of those ZIP UDF's in a few of my programs, I generally tend to just use the 7-Zip executable with new stuff i create these days. It is easy enough to include that as part of your installer setup.


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

I believe the glitch is a file sharing violation. The function _Zip_Create isn't closing before the _Zip_AddItem starts. So, for now, the Sleep(2000) is necessary but not sure it will be enough in all cases.

I like the simplicity of _Zip.au3 so I will keep using it but can anyone confirm that there isn't a better version? For example, a version that includes password protection? The COM object does not do password protection.

Edited by takg

Share this post


Link to post
Share on other sites

The COM object does not do password protection.

One major reason I now use the 7-Zip executable via command-line.


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

I use 7zip too but I'd rather not have to deal with installing 7zip on the machine. I'd rather have an option that I can build into an AutoIt exe.

It would be great if the zipping capability was built into AutoIt. Is there somewhere I can make a feature request?

Edited by takg

Share this post


Link to post
Share on other sites

Created ticket #3180.

Share this post


Link to post
Share on other sites

I don't want to curb your enthusiasm, but if it hasn't happened by now, it is unlikely to for whatever reason that has prevented it up to now, so I advise looking elsewhere ... I certainly wouldn't hang around waiting or pause any program you are working on where you want to use it.

I suspect most of us here would like easy zipping with password facility etc.

You can see in the two main ZIP UDF topics in Examples that password is a common theme, amongst other things.

Do you actually need to install the 7-Zip program, or can you just use the exe if placed in your program folder? I seem to recall you can do that ... or a portable version that can sit in a sub-folder of your program.

In all my cases, 7-Zip was already installed.


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites

You are right; the request won't help me now. I am really just voting for including it. The _Zip.au3 works fine for now and I will keep the COM object and 7z options in mind for future.

Thanks for all the help. I  really appreciate it.

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now