/*
* ZIPFiles.prg
* Clase para comprimir archivos y carpetas usando el Activex ChilKat ZIP
* Manual en https://www.chilkatsoft.com/refdoc/xChilkatZip2Ref.html
* Ejemplos en https://www.example-code.com/vbscript/zip.asp
* Copyright 2016 Bingen
*
*/
#include "Xailer.ch"
DYNAMIC CreateObject
DYNAMIC XA_Break
//------------------------------------------------------------------------------
CLASS TZIPFiles FROM TComponent
DATA oChilkat
DATA oZip
PUBLISHED:
PROPERTY cDLLName INIT "ChilkatAx.dll"
PROPERTY cActivexName INIT "Chilkat_9_5_0"
PROPERTY cKey INIT "xxxxxxxxxxx" Aquí tu clave de Chilkat
PROPERTY cUtility INIT "Creación de archivos ZIP"
PROPERTY aFileMask Init {}
PROPERTY lRecurseDir Init .F.
PROPERTY cFileName Init Application:cDirectory+"ZipFiles.Zip"
PROPERTY lIncludePath INIT .T.
PROPERTY nCompressionLevel iNIT 5 // 0 No compression, 1, 2, 3, 4, 5 Normal, 6, 7, 8, 9 Best
PROPERTY cPassword Init ""
PROPERTY cComment Init ""
PUBLIC:
DATA lInstalled INIT .F. READONLY
DATA lCompressed INIT .F. READONLY
DATA nErrorCode INIT 0 READONLY
DATA cErrorDescription INIT "" READONLY
METHOD New( oParent ) CONSTRUCTOR // --> Self
METHOD Create( oParent ) CONSTRUCTOR // --> Self
METHOD Close() // --> Nil
METHOD Free() // --> Nil
METHOD AddFile(cFileName)
METHOD Run() // --> lExito
METHOD FileCount() // --> nNumOfFIles
PRIVATE:
ENDCLASS
//------------------------------------------------------------------------------
METHOD New( oParent ) CLASS TZIPFiles
//Ver si está instalada la DLL de ChilKat
TRY
::oChilkat := TOleAuto():New( ::cActivexName+".Global" )
IF ValType( ::oChilkat:Version ) == "C"
::lInstalled := .T.
ENDIF
CATCH
::lInstalled := .F.
END
//Si no esta instalada y no hay DLL
If !::lInstalled .And. !File(::cDllName)
MsgError("No se ha localizado el archivo necesario para "+::cUtility+" "+::cDLLName+CRLF+CRLF+"Contacte con el distribuidor de la aplicación para poder utilizar "+::cUtility,"Error envío "+::cUtility+" 1")
Return .F.
Endif
//Si no esta instalada y si hay DLL intentar registrarla
If !::lInstalled .And. File(::cDLLName)
If !DLLRegisterServer( ::cDLLName )
MsgError("No se ha podido registrar el archivo necesario para "+::cUtility+" "+::cDLLName+CRLF+CRLF+"Debería de ejecutar la aplicación con permisos de administrador una sola vez para poder activar "+::cUtility,"Error envío "+::cUtility+" 2")
Return .F.
Else
//Ver de nuevo si está instalada la DLL de ChilKat
TRY
::oChilkat := TOleAuto():New( ::cActivexName+".Global" )
IF ValType( ::oChilkat:Version ) == "C"
::lInstalled := .T.
ENDIF
CATCH
::lInstalled := .F.
END
Endif
Endif
If !::lInstalled
MsgError("No se ha podido poner en marcha el archivo necesario para "+::cUtility+" "+::cDLLName+CRLF+CRLF+"Contacte con el distribuidor de la aplicación para poder utilizar "+::cUtility,"Error envío "+::cUtility+" 3")
Return .F.
Endif
//Si está instalada meterle la clave de ChilKat
IF ::oChilkat:UnlockBundle(::cKey) <> 1
MsgError("No se ha podido poner en marcha el archivo necesario para "+::cUtility+" "+::cDLLName+CRLF+CRLF+"Contacte con el distribuidor de la aplicación para poder utilizar "+::cUtility,"Error envío "+::cUtility+" 4")
Return .F.
ENDIF
::oChilkat := Nil
::Create( oParent )
RETURN Self
//------------------------------------------------------------------------------
METHOD Create( oParent ) CLASS TZIPFiles
::Super:Create( oParent )
::oZip := TOleAuto():New( ::cActivexName+".Zip" )
::oZip:CaseSensitive:=.F.
RETURN Self
//------------------------------------------------------------------------------
METHOD Close() CLASS TZIPFiles
::Free()
RETURN Nil
//------------------------------------------------------------------------------
METHOD Free() CLASS TZIPFiles
::oZip := Nil
::Super:Free()
RETURN Nil
//------------------------------------------------------------------------------
METHOD AddFile(cFileName) CLASS TZIPFiles
IF File(cFileName)
AAdd(::aFileMask,cFileName)
Endif
RETURN Nil
//------------------------------------------------------------------------------
METHOD Run() CLASS TZIPFiles
Local nItem:=0
With Object ::oZip
:NewZip(::cFileName)
:DiscardPaths := !::lIncludePath
:SetCompressionLevel(::nCompressionLevel)
For nItem:=1 to Len(::aFileMask)
:AppendFiles(::aFileMask[nItem],::lRecurseDir)
NExt
If Len(::cPassword)>0
:Encryption := 4
:EncryptKeyLength := 256
:EncryptPassword := ::cPassword
Endif
::lCompressed := :WriteZipAndClose() = 1 // 1 es que todo ha ido bien
End
::Free()
RETURN ::lCompressed
//------------------------------------------------------------------------------
METHOD FileCount()
Return ::oZip:FileCount()
//------------------------------------------------------------------------------
/* Sample
With Object TZipFiles():New()
:aFileMask := {"*.exe","*.Ini"}
:lRecurseDir := .T.
:cFileName := "test.zip"
:cPassword := "Test"
If !:Run()
MsgInfo(:cErrorDescription,:nErrorCode )
Endif
END
*/