Mittwoch, 11. Juli 2012

Einfaches arbeiten mit ZIP Archiven / Working with ZIP files the easy way

Das Shell.Application Objekt kennt unter anderem eine Methode namens 'CopyHere'. Diese auf den ersten Blick recht harmlos erscheinende Methode beinhaltet jedoch auch das Verarbeiten von ZIP Archiven.

Wenn wir komplette Verzeichnisse archivieren bzw. wiederherstellen wollen, dann ist dies mit CopyHere auf relative einfache Weise möglich.

Das heutige Code Beispiel besteht aus zwei kleine Routinen zum Packen und Entpacken von Verzeichnissen unter Nutzung von CopyHere:

Among other things the Shell.Application object has a method called 'CopyHere'. At first glance, it appears to be rather harmless. However it contains complete processing of ZIP archives.

So, as long as we only compute complete directories, CopyHere offers a rather easy way of doing this.

Today's code example contains two small routines for un-/packing directories via CopyHere.

ZIP
CLEAR

lcFile    = [D:\Archive\myZipArchive.zip]
lcDir    = [D:\TEMP]
?
?Zip2Archive( lcDir , lcFile )

FUNCTION zip2Archive as Boolean
     LPARAMETERS vSourceDir as String, vZipFile as String
    DECLARE Sleep IN WIN32API INTEGER
    LOCAL    llReturn as Boolean, liOption as Integer, ;
            loShellObj as Object, loInputObj as Object, loOutputObj as Object, loFile as Object
    llReturn = .F.
    loShellObj = CreateObject( [Shell.Application] )
    IF TYPE( [loShellObj] ) = [O]
        IF NOT FILE( vZipFile )
            STRTOFILE( [] , vZipFile )
        ENDIF 
        loInputObj    = loShellObj.NameSpace( vSourceDir )
        loOutputObj    = loShellObj.NameSpace( vZipFile )
        liOption    = 4 && Do not display a progress dialog box 
        IF TYPE( [loInputObj] ) = [O] AND TYPE( [loOutputObj] ) = [O]
            TRY 
                FOR EACH loFile IN loInputobj.Items
                    loTargetObjExists = loOutputObj.ParseName( loFile.Name )
                    loSourceObjExists = loInputObj.ParseName( loFile.Name )
                    IF ISNULL( loTargetObjExists )
                        loOutputObj.CopyHere( loFile , liOption )
                        SLEEP( 200 )
                    ELSE  
                        IF loOutputObj.ParseName( loFile.Name ).ModifyDate < loFile.ModifyDate
                            ?loFile.Name + [ kann aktualisiert werden]
                        ELSE 
                            ?loFile.Name + [ ist aktuell]
                        ENDIF 
                    ENDIF 
                ENDFOR 
                llReturn = .T.
            CATCH 
                * Place messagebox here
            ENDTRY
        ENDIF 
    ENDIF
    RELEASE loInputObj, loOutputObj, loShellObj, loFile
    RETURN llReturn
ENDFUNC 
Paste your text here.

UNZIP
lcFile    = [D:\Archive\myZipFile.zip]
lcDir    = [D:\TEMP]
UnzipArchive( lcFile , lcDir )

FUNCTION UnzipArchive as Boolean
    LPARAMETERS vZipFile as String, vTargetDir as String
SET STEP ON 
    LOCAL    llReturn as Boolean, liOption as Integer, ;
            loShellObj as Object, loInputObj as Object, loOutputObj as Object
    llReturn = .F.
    
    loShellObj = CreateObject( [Shell.Application] )
    IF TYPE( [loShellObj] ) = [O]
        loOutputObj    = loShellObj.NameSpace( vTargetDir )
        loInputObj    = loShellObj.NameSpace( vZipFile )
        liOption    = 4 && Do not display a progress dialog box.
        IF TYPE( [loInputObj] ) = [O] AND TYPE( [loOutputObj] ) = [O]
            TRY 
                loOutputObj.CopyHere( loInputObj.Items , liOption )
                llReturn = .T.
            CATCH 
                * Place messagebox here
            ENDTRY
        ENDIF 
    ENDIF
    RELEASE loInputObj, loOutputObj, loShellObj 
    RETURN llReturn
ENDFUNC 

Der oben stehende Code kann natürlich auf die Verarbeitung bestimmter Dateien/Dateitypen umgestellt bzw. erweitert werden.
Above code can easily be modified to work with specific files/filetypes.

Weiterführende Infos zur Arbeit mit Verzeichnissen und Archiven gibt es hier:
Additional Infos about working with directories and archives can be found here:

http://technet.microsoft.com/en-us/library/ee176625