Zip Udf, un udf per zippare i files

« Older   Newer »
  Share  
torels
CAT_IMG Posted on 16/10/2008, 20:34     +1   -1




Ciao a tutti
Voglio presentarvi il mio udf per zippare i files :P
sul forum inglese ha avuto quasi 1000 downloads

Spero torni utile a qualcuno :P
Queste sono le funzioni (nonche' user CallTip entries)
CODICE
_Zip_Create($FileName) Create Zip File. Required #include <Zip.au3>
_Zip_AddFile($ZipFile, $File2Add, [$flag]) Add file to Zip Archive. Required #include <Zip.au3>
_Zip_AddFolder($ZipFile, $Folder, [$flag]) Add folder to Zip Archive. Required #include <Zip.au3>
_Zip_AddFolderContents($ZipFile, $Folder, [$flag]) Add folder's content to Zip Archive. Required #include <Zip.au3>
_Zip_UnzipAll($ZipFile, $DestPath, [$flag]) Extract all files from Zip Archive. Required #include <Zip.au3>
_Zip_Unzip($ZipFile,$FileName, $DestPath, [$flag]) Extract file from Zip Archive. Required #include <Zip.au3>
_Zip_Count($ZipFile) Count items in zip. Required #include <Zip.au3>
_Zip_List($ZipFile) List items in zip. Required #include <Zip.au3>
_Zip_Search($ZipFile, $SearchString) Search a File in the Zip Archive. Required #include <Zip.au3>
_Zip_SearchInFile($ZipFile, $SearchString) Search In each File of the Zip Archive. Required #include <Zip.au3>


ecco il codice
SPOILER (click to view)
CODICE
;#AutoIt3Wrapper_au3check_parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include <array.au3>
; ------------------------------------------------------------------------------
;
; AutoIt Version: 3.2
; Language:       English
; Description:    ZIP Functions.
; Author:                  torels_
;
; ------------------------------------------------------------------------------
;===============================================================================
;
; Function Name:    _Zip_Create()
; Description:      Create Empty ZIP file.
; Parameter(s):     $hFilename - Complete path to zip file that will be created
; Requirement(s):   none.
; Return Value(s):  On Success - Returns the Zip file path (to be used as a handle - even though it's not necessary)
;                   On Failure - @error = 1 (file could not be created)
; Author(s):        torels_
;
;===============================================================================
Func _Zip_Create($hFilename)
       $hFp = FileOpen($hFilename, 26)
       $sString = Chr(80) & Chr(75) & Chr(5) & Chr(6) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0)
       FileWrite($hFp, $sString)
       FileClose($hFp)
       
       If Not FileExists($hFilename) Then
               Return SetError(1, 0, 0)
       Else
               Return $hFilename
       EndIf
EndFunc   ;==>_Zip_Create

;===============================================================================
;
; Function Name:    _Zip_AddFile()
; Description:      Add a file to a ZIP Archieve.
; Parameter(s):     $hZipFile - Complete path to zip file that will be created (or handle if existant)
;                                        $hFile2Add - Complete path to the file that will be added
;                                        $flag = 1
;                                        - 0 ProgressBox
;                                        - 1 no progress box
; Requirement(s):   none.
; 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
;                   On Failure - Returns False
; Author(s):        torels_
; Notes:                        The return values will be given once the compressing process is ultimated... it takes some time with big files
;
;===============================================================================
Func _Zip_AddFile($hZipFile, $hFile2Add, $flag = 4)
       Local $DLLChk = _Zip_DllChk()
       Local $files = _Zip_Count($hZipFile)
       If $DLLChk <> 0 Then Return SetError($DLLChk, 0, 0);no dll
       If Not FileExists($hZipFile) Then Return SetError(1, 0, 0) ;no zip file
       
       $oApp = ObjCreate("Shell.Application")
       $copy = $oApp.NameSpace($hZipFile).CopyHere($hFile2Add)
       ;WinSetState"[CLASS:SysAnimate32; INSTANCE:1]" "#32770"
       While 1
               If _Zip_Count($hZipFile) = ($files+1) Then ExitLoop
       WEnd
       
       Return SetError(0,0,1)
EndFunc   ;==>_Zip_AddFile

;===============================================================================
;
; Function Name:    _Zip_AddFolder()
; Description:      Add a folder to a ZIP Archieve.
; Parameter(s):     $hZipFile - Complete path to zip file that will be created (or handle if existant)
;                                        $hFolder - Complete path to the folder that will be added (possibly including "\" at the end)
;                                        $flag = 4
;                                        - 4 no progress box
;                                        - 8 file name changed if already exists
;                                        - 16 respond "yes to all" for any dialog box displayed
;                                        - 64 preservw undo information, if possible
;                                        - 256 progress dialogbox without file names on it
;                                        - 512 Do not confirm the creation of a directory if the operation requires one to be created
;                                        - 1024 Do not display a user interface if an error occours
;                                        - 4096 Don't operate recursively in subdirectories but just in the local directory
; Requirement(s):   none.
; 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
; Author(s):        torels_
; Notes:                        The return values will be given once the compressing process is ultimated... it takes some time with big files
;
;===============================================================================
Func _Zip_AddFolder($hZipFile, $hFolder, $flag = 4)
       Local $DLLChk = _Zip_DllChk()
       If $DLLChk <> 0 Then Return SetError($DLLChk, 0, 0);no dll
       If Not FileExists($hZipFile) Then Return SetError(1, 0, 0) ;no zip file
       
       If StringRight($hFolder, 1) <> "\" Then $hFolder &= "\"
       
       $files = _Zip_Count($hZipFile)
       $oApp = ObjCreate("Shell.Application")
       $oCopy = $oApp.NameSpace($hZipFile).CopyHere($oApp.Namespace($hFolder), $flag)

       While 1
               If _Zip_Count($hZipFile) = ($files+1) Then ExitLoop
       WEnd
       
       Return SetError(0,0,1)
EndFunc   ;==>_Zip_AddFolder

;===============================================================================
;
; Function Name:    _Zip_AddFolderContents()
; Description:      Add a folder to a ZIP Archieve.
; Parameter(s):     $hZipFile - Complete path to zip file that will be created (or handle if existant)
;                                        $hFolder - Complete path to the folder that will be added (possibly including "\" at the end)
;                                        $flag = 4
;                                        - 4 no progress box
;                                        - 8 file name changed if already exists
;                                        - 16 respond "yes to all" for any dialog box displayed
;                                        - 64 preservw undo information, if possible
;                                        - 256 progress dialogbox without file names on it
;                                        - 512 Do not confirm the creation of a directory if the operation requires one to be created
;                                        - 1024 Do not display a user interface if an error occours
;                                        - 4096 Don't operate recursively in subdirectories but just in the local directory
; Requirement(s):   none.
; 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
; Author(s):        torels_
; Notes:                        The return values will be given once the compressing process is ultimated... it takes some time with big files
;
;===============================================================================
Func _Zip_AddFolderContents($hZipFile, $hFolder, $flag = 4)
       Local $DLLChk = _Zip_DllChk()
       If $DLLChk <> 0 Then Return SetError($DLLChk, 0, 0);no dll
       If Not FileExists($hZipFile) Then Return SetError(1, 0, 0) ;no zip file
       
       If StringRight($hFolder, 1) <> "\" Then $hFolder &= "\"
       
       $files = _Zip_Count($hZipFile)
       $oApp = ObjCreate("Shell.Application")
       $oFolder = $oApp.NameSpace($hFolder)
       $oCopy = $oApp.NameSpace($hZipFile).CopyHere($oFolder.Items, $flag)
       $oFC = $oApp.NameSpace($hFolder).items.count

       While 1
               If _Zip_Count($hZipFile) = ($files+$oFC) Then ExitLoop
       WEnd
       
       Return SetError(0,0,1)
EndFunc   ;==>_Zip_AddFolderContents

;===============================================================================
;
; Function Name:    _Zip_UnzipAll()
; Description:      Extract all files contained in a ZIP Archieve.
; Parameter(s):     $hZipFile - Complete path to zip file that will be created (or handle if existant)
;                                        $hDestPath - Complete path to where the files will be extracted
;                                        $flag = 4
;                                        - 4 no progress box
;                                        - 8 file name changed if already exists
;                                        - 16 respond "yes to all" for any dialog box displayed
;                                        - 64 preservw undo information, if possible
;                                        - 256 progress dialogbox without file names on it
;                                        - 512 Do not confirm the creation of a directory if the operation requires one to be created
;                                        - 1024 Do not display a user interface if an error occours
;                                        - 4096 Don't operate recursively in subdirectories but just in the local directory
; Requirement(s):   none.
; 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
; Author(s):        torels_
; Notes:                        The return values will be given once the extracting process is ultimated... it takes some time with big files
;
;===============================================================================
Func _Zip_UnzipAll($hZipFile, $hDestPath, $flag = 4)
       Local $DLLChk = _Zip_DllChk()
       If $DLLChk <> 0 Then Return SetError($DLLChk, 0, 0);no dll
       If Not FileExists($hZipFile) Then Return SetError(2, 0, 0) ;no zip file
       
       If Not FileExists($hDestPath) Then DirCreate($hDestPath)
       
       Local $aArray[1]
       $oApp = ObjCreate("Shell.Application")
       $oApp.Namespace($hDestPath).CopyHere($oApp.Namespace($hZipFile).Items, $flag)
       
       For $item In $oApp.Namespace($hZipFile).Items
               _ArrayAdd($aArray, $item)
       Next
       
       While 1
               Sleep(500)
               If FileExists($hDestPath & "\" & $aArray[UBound($aArray) - 1]) Then Return SetError(0, 0, 1)
               ExitLoop
       WEnd
       
EndFunc   ;==>_Zip_UnzipAll

;===============================================================================
;
; Function Name:    _Zip_Unzip()
; Description:      Extract a single file contained in a ZIP Archieve.
; Parameter(s):     $hZipFile - Complete path to zip file that will be created (or handle if existant)
;                                        $hFilename - Name of the element in the zip archive ex. "hello_world.txt"
;                                        $hDestPath - Complete path to where the files will be extracted
;                                        $flag = 4
;                                        - 4 no progress box
;                                        - 8 file name changed if already exists
;                                        - 16 respond "yes to all" for any dialog box displayed
;                                        - 64 preservw undo information, if possible
;                                        - 256 progress dialogbox without file names on it
;                                        - 512 Do not confirm the creation of a directory if the operation requires one to be created
;                                        - 1024 Do not display a user interface if an error occours
;                                        - 4096 Don't operate recursively in subdirectories but just in the local directory
; Requirement(s):   none.
; 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
; Author(s):        torels_
; Notes:                        The return values will be given once the extracting process is ultimated... it takes some time with big files
;
;===============================================================================
Func _Zip_Unzip($hZipFile, $hFilename, $hDestPath, $flag = 4)
       Local $DLLChk = _Zip_DllChk()
       If $DLLChk <> 0 Then Return SetError($DLLChk, 0, 0) ;no dll
       If Not FileExists($hZipFile) Then Return SetError(1, 0, 0) ;no zip file
       
       If Not FileExists($hDestPath) Then DirCreate($hDestPath)
       
       $oApp = ObjCreate("Shell.Application")
       $hFolderitem = $oApp.NameSpace($hZipFile).Parsename($hFilename)
       
       $oApp.NameSpace($hDestPath).Copyhere($hFolderitem, $flag)
       
       While 1
               Sleep(500)
               If FileExists($hDestPath & "\" & $hFilename) Then
                       return SetError(0, 0, 1)
                       ExitLoop
               EndIf
       WEnd
       
       
EndFunc   ;==>_Zip_Unzip

;===============================================================================
;
; Function Name:    _Zip_Count()
; Description:      Count files contained in a ZIP Archieve.
; Parameter(s):     $hZipFile - Complete path to zip file that will be created (or handle if existant)
; Requirement(s):   none.
; 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
; Author(s):        torels_
;
;===============================================================================
Func _Zip_Count($hZipFile)
       Local $DLLChk = _Zip_DllChk()
       If $DLLChk <> 0 Then Return SetError($DLLChk, 0, 0) ;no dll
       If Not FileExists($hZipFile) Then Return SetError(1, 0, 0) ;no zip file
       
       $items = _Zip_List($hZipFile)
       Return UBound($items) - 1
EndFunc   ;==>_Zip_Count

;===============================================================================
;
; Function Name:    _Zip_Count()
; Description:      Returns a collection of all the files contained in a ZIP Archieve.
; Parameter(s):     $hZipFile - Complete path to zip file that will be created (or handle if existant)
; Requirement(s):   none.
; 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
; Author(s):        torels_
;
;===============================================================================
Func _Zip_List($hZipFile)
       local $aArray[1]
       Local $DLLChk = _Zip_DllChk()
       If $DLLChk <> 0 Then Return SetError($DLLChk, 0, 0) ;no dll
       If Not FileExists($hZipFile) Then Return SetError(1, 0, 0) ;no zip file
       
       $oApp = ObjCreate("Shell.Application")
       $hList = $oApp.Namespace($hZipFile).Items
       
       For $item in $hList
               _ArrayAdd($aArray,$item.name)
       Next
       $aArray[0] = UBound($aArray) - 1
       Return $aArray
EndFunc   ;==>_Zip_List

;===============================================================================
;
; Function Name:    _Zip_Search()
; Description:      Search files in a ZIP Archive.
; Parameter(s):     $hZipFile - Complete path to zip file that will be created (or handle if existant)
;                                        $sSearchString - name of the file to be searched
; Requirement(s):   none.
; Return Value(s):  On Success - 0
;                   On Failure - sets @error 1 (no file found)
; Author(s):        torels_
; Notes:                        none
;
;===============================================================================
Func _Zip_Search($hZipFile, $sSearchString)
       local $aArray
       $list = _Zip_List($hZipFile)
       for $i = 0 to UBound($list) - 1
               if StringInStr($list[$i],$sSearchstring) > 0 Then
                       _ArrayAdd($aArray, $list[$i])
               EndIf
       Next
       if UBound($aArray) - 1 = 0 Then
               Return SetError(1,0,0)
       Else
               Return $aArray
       EndIf
EndFunc ;==> _Zip_Search

;===============================================================================
;
; Function Name:    _Zip_SearchInFile()
; Description:      Search files in a ZIP Archive.
; Parameter(s):     $hZipFile - Complete path to zip file that will be created (or handle if existant)
;                                        $sSearchString - name of the file to be searched
; Requirement(s):   none.
; Return Value(s):  On Success - 0
;                   On Failure - sets @error 1 (no file found)
; Author(s):        torels_
; Notes:                        none
;
;===============================================================================
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")
               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 ;==> _Zip_Search

;===============================================================================
;
; Function Name:    _Zip_DllChk()
; Description:      Internal error handler.
; Parameter(s):     none.
; Requirement(s):   none.
; Return Value(s):  Failure - @extended = 1
; Author(s):        smashley
;
;===============================================================================
Func _Zip_DllChk()
       If Not FileExists(@SystemDir & "\zipfldr.dll") Then Return 2
       If Not RegRead("HKEY_CLASSES_ROOT\CLSID\{E88DCCE0-B7B3-11d1-A9F0-00AA0060FA31}", "") Then Return 3
       Return 0
EndFunc   ;==>_Zip_DllChk




Download attachment
Zip.au3 ( Number of downloads: 151 )

 
Top
|=Z3z!=|
CAT_IMG Posted on 30/6/2009, 13:13     +1   -1




Bello :)
 
Top
antru
CAT_IMG Posted on 30/6/2009, 13:45     +1   -1




Bello e utile!
 
Top
negogreeven
CAT_IMG Posted on 10/9/2010, 05:39     +1   -1




Hello everyone! I am new to this forum. Just wanted to greet all of you :)
 
Top
BlackHack00
CAT_IMG Posted on 10/9/2010, 10:13     +1   -1




hello negogreeven you must present in the section
"nuovi utenti"
 
Top
Aaly
CAT_IMG Posted on 15/10/2010, 18:46     +1   -1




Peccato che su vista e windows7 non funzioni.
Puoi aggiornare?
 
Top
3ad_Pr0grammer
CAT_IMG Posted on 15/10/2010, 19:50     +1   -1




Utile :) Dopo lo provo.
 
Top
luigi128
CAT_IMG Posted on 22/10/2012, 17:57     +1   -1




Ma funziona con tutti gli archivi?
Es. .rar .zip .7z .jar ...
 
Top
»Master
CAT_IMG Posted on 22/10/2012, 23:00     +1   +1   -1




Se č ZIP UDF, allor ami pare che funzioni soltanto con il formatto di file ZIP :asd:
Poi non confondere estensione con tipo di file, puoi prendere una musica e cambiare l'estensione da .mp3 a .txt che non cambia un corno :asd:
 
Top
8 replies since 16/10/2008, 20:34   8076 views
  Share