Jump to content

ZIP.au3 UDF in pure AutoIt


Recommended Posts

  • 4 months later...
  • Replies 233
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Popular Posts

Hi there! I just wrote a ZIP udf in AutoIt (without external files) which lets you create archives, fill them with files and folders and extract them to your computer these are the functions <&l

Fredricz, If you read my post at the top of this page you will find the answer - which unfortunately is not what you want to hear. M23

If $DLLChk <> 0 Then Return SetError($DLLChk, 0, 0);no dll Think this udf is based on an dll that dosent exist in win10 sorry, u could try http://www.autoitscript.com/forum/topic/85094

Posted Images

Hi torels,

May I pursuade you to give this UDF a bit more attention... you have a download count closing in on 3000 (congrats!) but there are still some major issues with the code that were raised in 2008 by trancexx that havn't been addressed.

For example:

Func _Zip_SearchInFile($hZipFile, $sSearchString)
    local $aArray
    $list = _Zip_List($hZipFile)
    for $i = 1 to UBound($list) - 1
        _Zip_Unzip($hZipFile, $list[$i], @TempDir & "\tmp_zip.file")
        $read = FileRead(@TempDir & "\tmp_zip.file")
        if StringInStr($read,$sSearchstring) > 0 Then
            _ArrayAdd($aArray, $list[$i])
        EndIf
    Next
    if UBound($aArray) - 1 = 0 Then
        Return SetError(1,0,1)
    Else
        Return $aArray
    EndIf
EndFunc

I'm guessing you meant something like: (I havn't looked too closely at error checking)

Func _Zip_SearchInFile($hZipFile, $sSearchString)
    local $aArray[1]
    $list = _Zip_List($hZipFile)
    for $i = 1 to UBound($list) - 1
        _Zip_Unzip($hZipFile, $list[$i], @TempDir & "\tmp_zip.file")
        $read = FileRead(@TempDir & "\tmp_zip.file\" & $list[$i])
        If StringInStr($read,$sSearchstring) Then
            _ArrayAdd($aArray, $list[$i])
        EndIf
    Next
    If UBound($aArray) = 1 Then
        Return SetError(1,0,0)
    Else
        _ArrayPush($aArray, "")
        ReDim $aArray[UBound($aArray) - 1]
        Return $aArray
    EndIf
EndFunc

This still doesn't address the build up of files in the temp folder, or the annoying prompt to replace files in %tmp%\tmp_zip.file...

Just would like to see the bugs ironed out...

Thanks,

Matt

Link to post
Share on other sites
  • 1 month later...

About _Zip_UnzipAll($hZipFile, $hDestPath, $flag = 1)

  • $flag set to 0 or 1 doesn't change anything, in both cases I see the system copy progress bar : "Copying xx items (xx MB)"

  • then, Return values doesn't match the descriptive text on top of this function:

    ; Return Value(s):  On Success - 0
    ;                   On Failure - sets @error 1~3
    ;                   @error = 1 no Zip file
    ;                   @error = 2 no dll
    ;                   @error = 3 dll isn't registered

    whereas in reality :

    - on success, it returns 1 (and so on error it returns 0)

    - on error (no zip file), @error is set to 2

_Zip_CountAll($hZipFile) doesn't work, it returns a 3-lines string which contains :

-Type of file

-Size

-Modification date

I don't use other functions yet.

ps: I run on a french Vista 64bits, AutoIt 3.3.6.1 & the latest Zip.au3 available in the first post.

Link to post
Share on other sites

So I have used your Zip UDF in my script to zip files with certain file extensions in a directory, and then recursively going through its sub-directories, but I cannot see any .zip files after the script runs.

Can anyone tell me if I'm using this wrong? Here is my script:

#include <File.au3>
#include <Zip.au3>

; bad file extensions
Local $extData="ade|adp|app|asa|ashx|asp|bas|bat|cdx|cer|chm|class|cmd|com|cpl|crt|csh|der|exe|fxp|gadget|hlp|hta|htr|htw|ida|idc|idq|ins|isp|its|jse|ksh|lnk|mad|maf|mag|mam|maq|mar|mas|mat|mau|mav|maw|mda|mdb|mde|mdt|mdw|mdz|msc|msh|msh1|msh1xml|msh2|msh2xml|mshxml|msi|msp|mst|ops|pcd|pif|prf|prg|printer|pst|reg|rem|scf|scr|sct|shb|shs|shtm|shtml|soap|stm|url|vb|vbe|vbs|ws|wsc|wsf|wsh"
Local $extensions = StringSplit($extData, "|")

; What is the root directory?
$rootDirectory = InputBox("Root Directory", "Please enter the root directory...")

archiveDir($rootDirectory)

Func archiveDir($dir)
    
    $goDirs = True
    $goFiles = True
    
    ; Get all the files under the current dir
    $allOfDir = _FileListToArray($dir)
    Local $countDirs = 0
    Local $countFiles = 0

    $imax = UBound($allOfDir)
    For $i = 0 to $imax - 1
        If StringInStr(FileGetAttrib($dir & "\" & $allOfDir[$i]),"D") Then
            $countDirs = $countDirs + 1
        ElseIf StringInStr(($allOfDir[$i]),".") Then
            $countFiles = $countFiles + 1
        EndIf   
    Next

    MsgBox(0, "Value of $countDirs in " & $dir, $countDirs)
    MsgBox(0, "Value of $countFiles in " & $dir, $countFiles)

    If ($countDirs > 0) Then
        Local $allDirs[$countDirs]
        $goDirs = True
    Else
        $goDirs = False
    EndIf
    
    If ($countFiles > 0) Then
        Local $allFiles[$countFiles]
        $goFiles = True
    Else
        $goFiles = False
    EndIf

    $dirCount = 0
    $fileCount = 0

    For $i = 0 to $imax - 1
        If (StringInStr(FileGetAttrib($dir & "\" & $allOfDir[$i]),"D")) And ($goDirs == True) Then
            $allDirs[$dirCount] = $allOfDir[$i]
            $dirCount = $dirCount + 1
        ElseIf (StringInStr(($allOfDir[$i]),".")) And ($goFiles == True) Then
            $allFiles[$fileCount] = $allOfDir[$i]
            $fileCount = $fileCount + 1
        EndIf   
    Next
    
    ; Zip them if need be in current spot using 'ext_zip.zip' as file name, loop through each file ext.
    If ($goFiles == True) Then 
        $emax = UBound($extensions)
        $fmax = UBound($allFiles)
        For $e = 0 to $emax - 1
            For $f = 0 to $fmax - 1
                $currentExt = getExt($allFiles[$f])
                If ($currentExt == $extensions[$e]) Then
                    $zip = _Zip_Create($dir & "\" & $currentExt & "_zip.zip")
                    _Zip_AddFile($zip, $allFiles[$f])
                EndIf
            Next
        Next
    EndIf
    
    ; Get all dirs under current DirCopy

    ; For each dir, recursive call from step 2
    
    If ($goDirs == True) Then
        $dmax = UBound($allDirs)
        $rootDirectory = $rootDirectory & "\"
        For $d = 0 to $dmax - 1
            archiveDir($rootDirectory & $allDirs[$d])
        Next
    EndIf
    
EndFunc

Func getExt($filename)
        
        $pos = StringInStr($filename, ".")
        $retval = StringTrimLeft($filename, $pos + 1)
        Return $retval
        
EndFunc
Link to post
Share on other sites
  • 2 weeks later...
  • 3 weeks later...

Hi Torels,

I have problems using your "Zip.au3", when I need to read a command line parameter.

Please, test this code, using:

TestMeZip.exe AnyString

;--------------------------------------------------START

; TestMeZip.au3

; ---------------

#include <Zip.au3>

Dim $Directory

$Directory = ""

If $CmdLine[0] <> 0 Then $Directory = $CmdLine[1]

MsgBox(0, "Directory: ", $Directory)

Exit

;---------------------------------------------------END

Executing this, I obtain the following error message:

"AutoIt3\Include\Zip.au3 (456) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded"

Could you, please, tell me what i wrong ?

It's a bug ?

Regards

BobMaX

Link to post
Share on other sites

I needed this for a project of mine recently. Concerned about the state of this UDF, I've pretty much completely rewritten it. It has much more robust error checking and fixes many of the bugs and omissions in the original UDF. I've removed the virtual functions and made many changes / enhancements.

That said, I will never get back 3 days of my life. I hate the zipfldr library and Shell.Application object with a vengeance.

I'm going to put this in a new thread to avoid hijacking this one.

Please go HERE

Edited by wraithdu
Link to post
Share on other sites
  • 2 weeks later...
  • 1 month later...
  • 1 month later...

Hi m8!

Let me start to congratulate you on a job well done: Congrats! ;)

Having said that, your au3 has a bug:

If I include your script in mine, i can't pass any command line parameters: $CmdLine because I get an error on yours.

Can you please take a look at this? I've tried fixing it myself and almost could, but sometimes I get another error, so I think I'm just gona wait for your next release.

Thx on advance.

"Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life." - Terry Pratchett.

Link to post
Share on other sites
  • 2 weeks later...

hiya

I'm a bit puzzled; I wondered if anyone could help me (asap)?

You see, I wrote a script for my memory stick using this UDF, but I wrote it on my computer! It worked fine on the computer but not on my memory stick! Here's a snippet of the script:

$Zip = _Zip_Create($ScriptDir & "\contents.zip")
;~              MsgBox(0, $Zip, $ScriptDir & "\")
;~              If StringIsAlpha($Zip) > 2 Then MsgBox(0, "Congratulations", "You have achieved the impossible! Please contact Andrew Green @ Lyke ASAP!")
                $Error = ""
                $l_Files = _FileListToArray($ScriptDir, "*", 1)
                For $i = 1 To $l_Files[0]
                    MsgBox(0, $Zip, $ScriptDir & "\" & $l_Files[$i])
                    If $l_Files[$i] = $AppName Or $l_Files[$i] = "autorun.inf" Or $l_Files[$i] = "Zip.au3" Or $l_Files[$i] = "contents.zip" Then ContinueLoop
                    _Zip_AddFile($Zip, $ScriptDir & "\" & $l_Files[$i])
;~                  TrayTip("Files", "Added", 1)
                    If @error Then
                        $Error  = "Some of the files could not be encrypted though."
                        ContinueLoop
;~                  ElseIf NOT 0 Or @error Then
;~                      MsgBox(0, "Error", "Something really wierd is happening with the compression, please contact Andrew Green @ Lyke Ltd!")
                    EndIf
                    FileDelete($ScriptDir & "\" & $l_Files[$i])
                Next
                $l_Folders = _FileListToArray($ScriptDir, "*", 2)
                For $i = 1 To $l_Folders[0]
                    $ErrorMsg = "some of the directory's could not be encypted"
                    MsgBox(0, $Zip, $ScriptDir & "\" & $l_Folders[$i])
                    _Zip_AddFolder($Zip, $ScriptDir & "\" & $l_Folders[$i])
                    If @error Then
                        If $Error = "" Then
                            $Error = $ErrorMsg & "."
                        Else
                            $Error = $Error & " And " & $ErrorMsg & " either."
                        EndIf
                        ContinueLoop
;~                  ElseIf NOT 0 Or @error Then
;~                      MsgBox(0, "Error", "Something really wierd is happening with the compression, please contact Andrew Green @ Lyke Ltd!")
                    EndIf
                    If DirRemove($ScriptDir & "\" & $l_Folders[$i], 1) = 0 Then $D_ERROR = 1
                Next
                IF $D_ERROR = 1 Then MsgBox(0, "Error", "The foler: " & $ScriptDir & "\" & $l_Folders[$i] & " could not be deleted, but has still been added to the encrypted contents!")
;~              MsgBox(0, "Where are we", "We are here")

It is supposed to put all the drive contents into a zip, by scanning loose files and adding them and then scanning for directories and adding them (as you can see)! But for some reason, it crashes on the _Zip_AddFile function. You can probably see that I've experiemented a little; the only thing that seems to work is when I add MsgBox's in places!

But, I don't want MsgBox's in this function - so does anyone know how to get around this?

Thanks in advance...

PS... I cut this a bit short because I've got critical battery! :|

Link to post
Share on other sites

hiya

I'm a bit puzzled; I wondered if anyone could help me (asap)?

You see, I wrote a script for my memory stick using this UDF, but I wrote it on my computer! It worked fine on the computer but not on my memory stick! Here's a snippet of the script:

$Zip = _Zip_Create($ScriptDir & "\contents.zip")
;~              MsgBox(0, $Zip, $ScriptDir & "\")
;~              If StringIsAlpha($Zip) > 2 Then MsgBox(0, "Congratulations", "You have achieved the impossible! Please contact Andrew Green @ Lyke ASAP!")
                $Error = ""
                $l_Files = _FileListToArray($ScriptDir, "*", 1)
                For $i = 1 To $l_Files[0]
                    MsgBox(0, $Zip, $ScriptDir & "\" & $l_Files[$i])
                    If $l_Files[$i] = $AppName Or $l_Files[$i] = "autorun.inf" Or $l_Files[$i] = "Zip.au3" Or $l_Files[$i] = "contents.zip" Then ContinueLoop
                    _Zip_AddFile($Zip, $ScriptDir & "\" & $l_Files[$i])
;~                  TrayTip("Files", "Added", 1)
                    If @error Then
                        $Error = "Some of the files could not be encrypted though."
                        ContinueLoop
;~                  ElseIf NOT 0 Or @error Then
;~                      MsgBox(0, "Error", "Something really wierd is happening with the compression, please contact Andrew Green @ Lyke Ltd!")
                    EndIf
                    FileDelete($ScriptDir & "\" & $l_Files[$i])
                Next
                $l_Folders = _FileListToArray($ScriptDir, "*", 2)
                For $i = 1 To $l_Folders[0]
                    $ErrorMsg = "some of the directory's could not be encypted"
                    MsgBox(0, $Zip, $ScriptDir & "\" & $l_Folders[$i])
                    _Zip_AddFolder($Zip, $ScriptDir & "\" & $l_Folders[$i])
                    If @error Then
                        If $Error = "" Then
                            $Error = $ErrorMsg & "."
                        Else
                            $Error = $Error & " And " & $ErrorMsg & " either."
                        EndIf
                        ContinueLoop
;~                  ElseIf NOT 0 Or @error Then
;~                      MsgBox(0, "Error", "Something really wierd is happening with the compression, please contact Andrew Green @ Lyke Ltd!")
                    EndIf
                    If DirRemove($ScriptDir & "\" & $l_Folders[$i], 1) = 0 Then $D_ERROR = 1
                Next
                IF $D_ERROR = 1 Then MsgBox(0, "Error", "The foler: " & $ScriptDir & "\" & $l_Folders[$i] & " could not be deleted, but has still been added to the encrypted contents!")
;~              MsgBox(0, "Where are we", "We are here")

It is supposed to put all the drive contents into a zip, by scanning loose files and adding them and then scanning for directories and adding them (as you can see)! But for some reason, it crashes on the _Zip_AddFile function. You can probably see that I've experiemented a little; the only thing that seems to work is when I add MsgBox's in places!

But, I don't want MsgBox's in this function - so does anyone know how to get around this?

Thanks in advance...

PS... I cut this a bit short because I've got critical battery! :|

Being a memory stick, the first thing I thought of was free space ... which needs to be about twice that of what you are zipping.

However, with the mention of success with a Msgbox, it must be related to timing (i.e. one process must finish before the next starts), which can be a little tricky with zipping. Essentially you need to allow enough time or make suitable calls to determine one AddFile process has finished before you add another file.

I haven't actually looked at your code, but I remember having similar issues myself in the past. A judiciously placed Sleep or three may be enough, or you may need to do File Attribute calls, etc if moving files around. Also ... If I remember rightly, there have been several discussions about this through the forum ... related to running threads and so forth.

Hope this points you in the right direction!

I know I succeeded in conquering this ... even for large files/folders ... as can be exampled with my Create Zip Within program available through my sig (Toolbox) ...

especially in my later versions, which I've not yet uploaded ... and used similar functions to those listed here (but from an older topic).

Edited by TheSaint

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

Link to post
Share on other sites
  • 1 month later...

Hi, this script does not work for me:

First I have 2 file: bg.jpg and transparent.gif

Then I use this code to make a .ZIP file:

;REQUIRE: _ZIP.au3

#include <_zip.au3>
#Include <Crypt.au3>


$zipPath=@ScriptDir&"\dataTemp.zip";
$desPath=@ScriptDir&"\data";
;MsgBox(0,"", "Extract "&$zipPath&" to folder "&$desPath);
$ss=_Zip_Unzip($zipPath, "bg.jpg", $desPath, 1);
;MsgBox(0,"",$ss);
;MsgBox(0,"",@error);

Then I use this code to extract the file. Just try it about 2 times, it appears "Error copy file or folder-The file exists" and then the script is paused automatically and does not exit!

;REQUIRE: _ZIP.au3

#include <_zip.au3>
#Include <Crypt.au3>


$zipPath=@ScriptDir&"\dataTemp.zip";
$desPath=@ScriptDir&"\data";
;MsgBox(0,"", "Extract "&$zipPath&" to folder "&$desPath);
$ss=_Zip_Unzip($zipPath, "bg.jpg", $desPath, 1);

It's just weird! Thanks for help in advance :graduated:!

http://www.facebook.com/xx3004?sk=info

Link to post
Share on other sites
  • 1 month later...

I hope that someone can help figure out why this breaks....

I am running multiple _unzipall functions in a script and it consistantly fails at the 100'th operation....

so I tried a test script with just creating and adding a file.

this errors out at the 25to 30th loop....

73, error1:0, error2:0

F:\scripts\Zip.au3 (333) : ==> Object referenced outside a "With" statement.:

$hList = $oApp.Namespace($hZipFile).Items

$hList = $oApp.Namespace($hZipFile)^ ERROR

If I don't get this error, it just hangs in a loop.....

Any ideas?

Link to post
Share on other sites

This is the script I was using with the zip.au3 at

; Description: ZIP Functions

; Author: wraithdu

; Date: 2011-01-13

; Credits: PsaltyDS for the original idea on which this UDF is based.

; torels for the basic framework on which this UDF is based.

;

#include "Zip.au3"

Dim $Zip, $myfile

$myfile = @DesktopDir & "\foo.au3"

For $i = 1 To 200

$Zip = _Zip_Create(@DesktopDir & "\temp" & "\zip_002.zip") ;Create The Zip File. Returns a Handle to the zip File

$e1 = @error

_Zip_AddFile($Zip, $myfile, "") ;add $myfile to the zip archive

$e2 = @error

ConsoleWrite($i & ", error1:" & $e1 & ", error2:" & $e2 & @CRLF)

Next

Edited by Jeff987
Link to post
Share on other sites
  • 1 month later...

Hi everyone! I was using _Zip_AddFile() within a loop to add multiple (i.e. hundreds) of files to a single archive. Since I was not retaining folder structure, all files were being placed in the root of the archive.

Eventually I canme across a file that, although containing different data, had the same file name as a file that had already been placed in the archive. Windows prompted if I wanted to overwrite, etc., but unfortunately it breaks the scripts no matter what you choose.

So, I edited the _Zip_AddFile() function so that if it comes across a file that is already in the archive, it renames the file from "filename.ext" to "filename(2).ext". Works like a charm :) Enjoy!

Func _Zip_AddFile($hZipFile, $hFile2Add, $flag = 1)
    Local $DLLChk = _Zip_DllChk()
    Local $files = _Zip_Count($hZipFile)
    If $DLLChk <> 0 Then Return SetError($DLLChk, 0, 0);no dll
    If not _IsFullPath($hZipFile) then Return SetError(4,0) ;zip file isn't a full path
    If Not FileExists($hZipFile) Then Return SetError(1, 0, 0) ;no zip file
    $list=_Zip_List($hZipFile)
    For $fn In $list
        $cutp=StringInStr($hFile2Add,"\",0,-1)
        $fname=StringTrimLeft($hFile2Add,$cutp)
        If $fname=$fn Then
            $path=StringTrimRight($hFile2Add,StringLen($hFile2Add)-$cutp)
            $fname1=StringTrimRight($fname,(StringLen($fname)-StringInStr($fname,".",0))+1)
            $fname2=StringTrimLeft($fname,StringInStr($fname,".",0)-1)
            FileMove($hFile2Add,$path & $fname1 & "(2)" & $fname2)
            $hFile2Add=$path & $fname1 & "(2)" & $fname2
        EndIf
    Next
    $oApp = ObjCreate("Shell.Application")
    $copy = $oApp.NameSpace($hZipFile).CopyHere($hFile2Add)
    While 1
        If $flag = 1 then _Hide()
        ;If WinExists("Confirm File Replace") Then ControlClick("Confirm File Replace","","Button1")
        If _Zip_Count($hZipFile) = ($files+1) Then ExitLoop
        Sleep(10)
    WEnd
    Return SetError(0,0,1)
EndFunc   ;==>_Zip_AddFile
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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...