﻿;-- TIME_STAMP   2016-10-22 15:27:29   v 0.2
;-- encoding=UTF-8

#include <GuiStatusBar.au3>
#include <InetConstants.au3>
#include <ProgressConstants.au3>
#include <File.au3>
#include <GuiComboBox.au3>
#include <Array.au3>

Global $INI = StringFormat('%s\TeamViewerMulti.ini', @ScriptDir)
If Not FileExists($INI) Then IniWrite($INI, 'path', 'versions', StringFormat('%s\TeamViewer_Portable', @ScriptDir))
Global $sPathTeamViewer = IniRead($INI, 'path', 'versions', StringFormat('%s\TeamViewer_Portable', @ScriptDir))
Global $aSection, $aBuddy


_TeamViewerManager()

Func _TeamViewerManager()
	Local $hGui = GUICreate('TeamViewer Manager', 250, 217)
	Local $idDownload = GUICtrlCreateButton('Download / Update', 30, 20, 190, 22)
	Local $idCombo = GUICtrlCreateCombo('Teamviewer Versionen', 30, 55, 190, 24, BitOr($GUI_SS_DEFAULT_COMBO, $CBS_DROPDOWNLIST))
	GUICtrlCreateGroup(' Kontakte ', 30, 85, 190, 80)
	Local $idBuddy = GUICtrlCreateCombo('', 40, 105, 170, 24, BitOr($GUI_SS_DEFAULT_COMBO, $CBS_DROPDOWNLIST))
	Local $idNew = GUICtrlCreateButton('Neu', 40, 135, 35, 22)
	Local $idEdit = GUICtrlCreateButton('Edit', 80, 135, 35, 22)
	Local $idDel = GUICtrlCreateButton('Del', 120, 135, 35, 22)
	Local $idCopy = GUICtrlCreateButton('Copy ID', 160, 135, 50, 22)
	GUICtrlCreateGroup('', -99, -99, 1, 1)
	Local $idStart = GUICtrlCreateButton('Start', 70, 175, 110, 22)
	Local $aSort[1]
	If FileExists($sPathTeamViewer) Then _FillCombo($aSort, $idCombo)
	_FillComboBuddy($idBuddy)

	GUISetState()
	While True
		Switch GUIGetMsg()
			Case -3
				Exit
			Case $idDownload
				Local $iErr = 0
				If MsgBox(262180, "Pfad", "Download-Pfad ist:" & @CRLF & @CRLF & $sPathTeamViewer & @CRLF & @CRLF & 'Beibehalten?') = 7 Then; Nein
					Local $sPath = FileSelectFolder('Pfad zum Speichern der Teamviewer Versionen', @ScriptDir, 1+2+4)
					If Not @error Then
						$sPathTeamViewer = $sPath
						IniWrite($INI, 'path', 'versions', $sPath)
					Else
						MsgBox(262192, "Fehler", "Es wurde kein Downloadpfad ausgewählt!")
						$iErr = 1
					EndIf
				EndIf
				If $iErr = 0 Then
					Local $iCountOld = InputBox('Anzahl Versionen', 'Bitte die Anzahl zu ladener Vorversionen angeben.' & @CRLF & @CRLF & _
					'"-1" = alle Vorversionen laden,' & @CRLF & '"0" = nur die aktuelle Version laden.', -1)
					$iCountOld = Number($iCountOld)
					If Not IsNumber($iCountOld) Then $iCountOld = -1
					_DL_TeamViewerPortable($sPathTeamViewer, $iCountOld)
					_FillCombo($aSort, $idCombo)
				EndIf
			Case $idNew
				_EvtBuddy($idBuddy, 'new')
			Case $idEdit
				_EvtBuddy($idBuddy, 'edit')
			Case $idDel
				_EvtBuddy($idBuddy, 'del')
			Case $idCopy
				_EvtBuddy($idBuddy, 'copy')
			Case $idStart
				Local $index = _GUICtrlComboBox_GetCurSel($idCombo)
				If $index > -1 Then ShellExecute(StringFormat('%s\%s\TeamViewer.exe', $sPathTeamViewer, $aSort[$index+1][0]))
		EndSwitch
	WEnd
EndFunc

Func _FillCombo(ByRef $aSort, $ID)
	Local $aVersionFolder = _FileListToArray($sPathTeamViewer, '*', 2)
	Local $sCombo = '', $sDefault = '', $iCount = 0
	ReDim $aSort[$aVersionFolder[0]+1][2]
	$aSort[0][0] = $aVersionFolder[0] ; [[$aVersionFolder[$i], TeamviewerVersion ]]
	For $i = 1 To $aVersionFolder[0]
		If StringLeft($aVersionFolder[$i], 10) = 'TeamViewer' Then
			$iCount += 1
			$aSort[$i][0] = $aVersionFolder[$i]
			$aSort[$i][1] = Number(StringTrimRight(StringTrimLeft($aVersionFolder[$i],12),9))
		EndIf
	Next
	_ArraySort($aSort, 1, 1, 0, 1) ; für Anzeige sortieren nach Version
	If $iCount <> $aVersionFolder[0] Then ReDim $aSort[$iCount+1][2]
	For $i = 1 To $aSort[0][0]
		$sCombo &= StringTrimRight($aSort[$i][0], 9) & '|'
		If $sDefault = '' Then $sDefault = StringTrimRight($aSort[$i][0], 9)
	Next
	_GUICtrlComboBox_ResetContent($ID)
	GUICtrlSetData($ID, StringTrimRight($sCombo, 1), $sDefault)
EndFunc

Func _EvtBuddy($_idCombo, $_sEvt)
	Local $index = _GUICtrlComboBox_GetCurSel($_idCombo)
	Local $input, $aSplit, $iFound, $sSelected
	Switch $_sEvt
		Case 'new'
			$input = InputBox('Kontakt NEU', 'Bitte Kontakt und ID Erfassen:' & @CRLF & @CRLF & 'Name=ID')
			If @error Then Return
			$aSplit = StringSplit($input, '=')
			If $aSplit[0] = 1 Then Return MsgBox(262192, 'Fehler', 'Kontakt-Name und ID sind nicht durch "=" getrennt.')
			$iFound = _ArraySearch($aBuddy, $aSplit[1], 1, 0, 0, 0, 1, 0)
			If Not @error Then Return MsgBox(262192, 'Fehler', 'Kontakt-Name bereits vorhanden.')
			$aBuddy[0][0] += 1
			ReDim $aBuddy[$aBuddy[0][0]+1][2]
			$aBuddy[$aBuddy[0][0]][0] = StringStripWS($aSplit[1], 1+2)
			$aBuddy[$aBuddy[0][0]][1] = StringStripWS($aSplit[2], 1+2)
		Case 'edit'
			If $index = -1 Then Return MsgBox(262192, 'Fehler', 'Kein Kontakt ausgewählt.')
			_GUICtrlComboBox_GetLBText($_idCombo, $index, $sSelected)
			$iFound = _ArraySearch($aBuddy, $sSelected, 1, 0, 0, 0, 1, 0)
			$input = InputBox('Kontakt BEARBEITEN', 'Bitte Name oder ID Bearbeiten:' & @CRLF & @CRLF & 'Name=ID', StringFormat('%s=%s', $aBuddy[$iFound][0], $aBuddy[$iFound][1]))
			If @error Then Return
			$aSplit = StringSplit($input, '=')
			If $aSplit[0] = 1 Then Return MsgBox(262192, 'Fehler', 'Kontakt-Name und ID sind nicht durch "=" getrennt.')
			Local $iTmp = _ArraySearch($aBuddy, $aSplit[1], 1, 0, 0, 0, 1, 0)
			If Not @error And $iTmp <> $iFound Then Return MsgBox(262192, 'Fehler', 'Kontakt-Name bereits vorhanden.')
			$aBuddy[$iFound][0] = StringStripWS($aSplit[1], 1+2)
			$aBuddy[$iFound][1] = StringStripWS($aSplit[2], 1+2)
		Case 'del'
			If $index = -1 Then Return MsgBox(262192, 'Fehler', 'Kein Kontakt ausgewählt.')
			_GUICtrlComboBox_GetLBText($_idCombo, $index, $sSelected)
			$iFound = _ArraySearch($aBuddy, $sSelected, 1, 0, 0, 0, 1, 0)
			If MsgBox(262180, 'Löschen', 'Soll der Kontakt "' & $sSelected & '" wirklich gelöscht werden?') = 7 Then Return
			_ArrayDelete($aBuddy, $index)
		Case 'copy'
			If $index = -1 Then Return MsgBox(262192, 'Fehler', 'Kein Kontakt ausgewählt.')
			_GUICtrlComboBox_GetLBText($_idCombo, $index, $sSelected)
			$iFound = _ArraySearch($aBuddy, $sSelected, 1, 0, 0, 0, 1, 0)
			ClipPut($aBuddy[$iFound][1])
			Return MsgBox(0, 'Kontakt ID', 'ID des Kontakts"' & $sSelected & '" in die Zwischenablage kopiert.')
	EndSwitch
	IniWriteSection($INI, 'buddy', $aBuddy)
	_FillComboBuddy($_idCombo)
EndFunc

Func _FillComboBuddy($_ID)
	_GUICtrlComboBox_ResetContent($_ID)
	Local $aTmp = IniReadSection($INI, 'buddy')
	If @error Then
		Global $aBuddy[1][2] = [[0]]
	Else
		Global $aBuddy = $aTmp
	EndIf
	If $aBuddy[0][0] = 0 Then Return
	_ArraySort($aBuddy, 0, 1)
	Local $sCombo = ''
	For $i = 1 To $aBuddy[0][0]
		$sCombo &= $aBuddy[$i][0] & '|'
	Next
	GUICtrlSetData($_ID, StringTrimRight($sCombo, 1), $aBuddy[1][0])
EndFunc

Func _DL_TeamViewerPortable($_sFolderTarget='', $_iCountOldVersions=3)
	$_sFolderTarget = $_sFolderTarget = '' ? $sPathTeamViewer : $_sFolderTarget
	If Not FileExists($_sFolderTarget) Then DirCreate($_sFolderTarget)
	Local $aLinks = _Get_DL_links($_iCountOldVersions), $sVersionFolder, $sZIP
	Local $iCountDL = UBound($aLinks)
	For $i = 0 To UBound($aLinks) -1
		$sVersionFolder = StringFormat('%s\TeamViewer__%s_Portable', $_sFolderTarget, $aLinks[$i][0])
		If FileExists($sVersionFolder) Then
			$iCountDL -= 1
			ContinueLoop
		EndIf
		$sZIP = StringFormat('%s\TeamViewerPortable__%s.zip', $_sFolderTarget, $aLinks[$i][0])
		_DownloadFile($aLinks[$i][1], $sZIP, 'Teamviewer Portable v' & $aLinks[$i][0])
		If Not @error Then
			SplashTextOn('Teamviewer Portable', 'Datei Entpacken', 250, 75)
			DirCreate($sVersionFolder)
			_BuildInZIP($sZIP, $sVersionFolder, True)
			If @error Then
				DirRemove($sVersionFolder)
				MsgBox(262192, 'Fehler', 'Die Datei' & @CRLF & StringFormat('"..\TeamViewerPortable__%s.zip"', $aLinks[$i][0]) _
					& @CRLF & 'konnte nicht entpackt werden und wurde wieder gelöscht.')
			EndIf
			FileDelete($sZIP)
			SplashOff()
		EndIf
	Next
	Local $sTxtSplash = 'Verarbeitung abgeschlossen'
	If $iCountDL = 0 Then $sTxtSplash = 'Es sind keine neueren Versionen vorhanden'
	SplashTextOn('Teamviewer Portable', $sTxtSplash, 250, 75)
	Sleep(2000)
	SplashOff()
EndFunc  ;==>_DL_TeamViewerPortable

Func _Get_DL_links($_iCountOldVersions=3)
	Local $sSite = BinaryToString(InetRead('https://www.teamviewer.com/de/download/windows/', 1+2))
	; get current version
	Local $aMatch = StringRegExp($sSite, '<p class="DownloadVersion">v(\d+)', 1)
	If @error Then Return SetError(1,0,'')
	Local $sVerMain = $aMatch[0]
	If $_iCountOldVersions > ($sVerMain -3) Then $_iCountOldVersions = $sVerMain -3 ; oldest: v3
	If $_iCountOldVersions = -1 Then $_iCountOldVersions = $sVerMain -3             ; all versions
	Local $aLinks[$_iCountOldVersions+1][2]
	For $i = 0 To UBound($aLinks) -2
		$aLinks[$i][0] = $sVerMain - ($_iCountOldVersions - $i)
		$aLinks[$i][1] = StringFormat('http://download.teamviewer.com/download/version_%sx/TeamViewerPortable.zip', $aLinks[$i][0])
	Next
	$aLinks[UBound($aLinks)-1][0] = $sVerMain
	$aLinks[UBound($aLinks)-1][1] = 'https://download.teamviewer.com/download/TeamViewerPortable.zip'
	Return $aLinks
EndFunc  ;==>_Get_DL_links

; #FUNCTION# ====================================================================================================================
; Name ..........: _DownloadFile
; Description ...: Downloads a file from a given URL, shows: progress, speed and remaining time.
; Syntax ........: _DownloadFile($_sUrl, $_sFile[, $_sTitle = ''[, $_iWidth = -1[, $_iHeight = -1[, $_iX = -1[, $_iY = -1]]]]])
; Parameters ....: $_sUrl               - The web adress of the file to load.
; ...............: $_sFile              - The path to store this file.
; ..[optional]...: $_sTitle             - Title of the download window. Default is '', = 'Download'.
; ..[optional]...: $_iWidth             - Width of the download window. Default is -1, = 250.
; ..[optional]...: $_iHeight            - Height of the download window. Default is -1, = 75.
; ..[optional]...: $_iX                 - X position of the download window. Default is -1, centered. Other negative value: X px from right, postive values: X px absolute.
; ..[optional]...: $_iY                 - Y position of the download window. Default is -1, centered. Other negative value: Y px from bottom, postive values: Y px absolute.
; Return values .: Success              None
; ...............: Failure              1 and show message box - download has failed or was escaped.
; Author ........: BugFix
; ===============================================================================================================================
Func _DownloadFile($_sUrl, $_sFile, $_sTitle='', $_iWidth=-1, $_iHeight=-1, $_iX=-1, $_iY=-1) ; Pos: -1/-1 = center, -X/-Y = X px from right / Y px from bottom
	$_sTitle = $_sTitle = '' ? 'Download' : $_sTitle
	$_iWidth = $_iWidth = -1 ? 250 : $_iWidth
	$_iHeight = $_iHeight = -1 ? 75 : $_iHeight
	If FileExists($_sFile) Then FileMove($_sFile, $_sFile & '.bak', 1)

    ; Download im Hintergrund, warten bis DL kpl. - Anzeige in GUI
    Local $iBytesSize, $hDL = GUICreate($_sTitle, $_iWidth, $_iHeight, -1, -1, BitOR(0x00C00000,0x00080000)) ; WS_CAPTION,WS_SYSMENU
	Local $idDL_sum = GUICtrlCreateLabel('0,000 KB', 5, 5, $_iWidth-10, 30, 0x01) ; SS_CENTER
	GUICtrlSetFont(-1, 14, Default, Default, 'Verdana')
	Local $aParts[3] = [75,$_iWidth-75,-1]
	Local $hStatus = _GUICtrlStatusBar_Create($hDL, $aParts)
	_GUICtrlStatusBar_SetMinHeight($hStatus, 25)
	Local $idProgress = GUICtrlCreateProgress(0, $_iHeight-20-26, $_iWidth, 20, $PBS_SMOOTH)
	Local $iTimer = TimerInit()
	; Fenstergröße absolut ermitteln und Fenster verschieben
	Local $aWin = WinGetPos($hDL)
	Select
		Case $_iX = -1
			$_iX = (@DesktopWidth - $aWin[2]) / 2
		Case $_iX < -1
			$_iX = @DesktopWidth - ($aWin[2] - $_iX)
	EndSelect
	Select
		Case $_iY = -1
			$_iY = (@DesktopHeight - $aWin[3]) / 2
		Case $_iY < -1
			$_iY = @DesktopHeight - ($aWin[3] - $_iY)
	EndSelect
	WinMove($hDL, '', $_iX, $_iY)
	GUISetState()

	Local $iSizeSource = InetGetSize($_sUrl)
    Local $hDownload = InetGet($_sUrl, $_sFile, $INET_FORCERELOAD, $INET_DOWNLOADBACKGROUND)
	_GUICtrlStatusBar_SetText($hStatus, @TAB & _FormatByte($iSizeSource, '', False, 0), 2)

	Local $fEsc = False, $iDL_old = 0, $iDL_diff, $iTimeNeed
    Do
        If TimerDiff($iTimer) >= 1000 Then
			$iBytesSize = InetGetInfo($hDownload, $INET_DOWNLOADREAD)
			GUICtrlSetData($idDL_sum, StringReplace(_FormatByte($iBytesSize), '.', ','))
			GUICtrlSetData($idProgress, Int(($iBytesSize/$iSizeSource)*100))
			$iDL_diff = $iBytesSize - $iDL_old
			$iDL_old = $iBytesSize
			_GUICtrlStatusBar_SetText($hStatus, @TAB & _FormatByte($iDL_diff, '', False, '0') & '/s', 0)
			$iTimeNeed = ($iSizeSource - $iBytesSize) / $iDL_diff
			_GUICtrlStatusBar_SetText($hStatus, @TAB & _FormatSeconds($iTimeNeed), 1)
			$iTimer = TimerInit()
		EndIf
		If GUIGetMsg() = -3 Then
			$fEsc = True
			ExitLoop
		EndIf
	Until InetGetInfo($hDownload, $INET_DOWNLOADCOMPLETE)
	_GUICtrlStatusBar_SetText($hStatus, '', 0)
	_GUICtrlStatusBar_SetText($hStatus, '', 1)

    ; Infos über die Download Datei holen
    Local $aData = InetGetInfo($hDownload)
    If @error Or $fEsc Then
        FileDelete($_sFile)
		Local $sTmp = $fEsc ? 'abgebrochen' : 'fehlgeschlagen'
		GUICtrlSetData($idDL_sum, 'Download ' & $sTmp & '!')
		Return SetError(1,0,MsgBox(262192, 'Fehler', 'Der Download ist ' & $sTmp & '.'))
	Else
		GUICtrlSetData($idDL_sum, StringReplace(_FormatByte($iBytesSize), '.', ','))
		GUICtrlSetData($idProgress, 100)
		Sleep(500)
		GUICtrlSetData($idDL_sum, 'Download beendet.')
    EndIf
    InetClose($hDownload)
	GUIDelete($hDL)
EndFunc  ;==>_DownloadFile


; #FUNCTION# ====================================================================================================================
; Name ..........: _FormatSeconds
; Description ...: Returns a given value of seconds in the format
; ...............:        <24h: "hh:mm:ss", >=24h: "x d / hh:mm:ss h"
; Syntax ........: _FormatSeconds($_sec)
; Parameters ....: $_sec                - The number of seconds.
; Return values .: The formatted string.
; Author ........: BugFix
; ===============================================================================================================================
Func _FormatSeconds($_sec)
	Return ( $_sec < 60 ? StringFormat('00:00:%02u', $_sec) : _
             $_sec < 60*60 ? StringFormat('00:%02u', Floor($_sec/60)) & ':' & _
			        StringFormat('%02u', Mod($_sec,60)) : _
			 $_sec < 60*60*24 ? StringFormat('%02u', Floor($_sec/3600)) & ':' & _
                    StringFormat('%02u', Floor(Mod($_sec,3600)/60)) & ':' & _
                    StringFormat('%02u', Mod(Mod($_sec,3600),60)) : _
			 ( $_sec = 86400 ? "24:00:00" : Floor($_sec/86400) & ' d / ' & _
					StringFormat('%02u', Floor(Mod($_sec,86400)/3600)) & ':' & _
					StringFormat('%02u', Floor(Mod(Mod($_sec,86400),3600)/60)) & ':' & _
					StringFormat('%02u', Mod(Mod(Mod($_sec,86400),3600),60)) & ' h') )
EndFunc  ;==>_FormatSeconds

; #FUNCTION# ====================================================================================================================
; Name ..........: _FormatByte
; Description ...: Formats a given value of bytes with highest or given unit, optional as structure with all units
; Parameters ....: $_iByte    The value of bytes to format
; ...............: $_sUnit    (Default = '', unit of highest value) or count of given unit (TB, GB, MB, KB, Byte)
; ...............: $_fStruct  Returns a structure with .TB .GB .MB .KB .Byte (Default = False)
; ...............: $_sDigit   Number of decimal digits (Default = '3') as string!
; Return values .: The formatted string or the structure.
; Author ........: BugFix
; ===============================================================================================================================
Func _FormatByte($_iByte, $_sUnit='', $_fStruct=False, $_sDigit='3')
	Local Static $aByte[5][2] = [[0x10000000000],[0x40000000],[0x100000],[0x400],[0x1]]
	Local Static $tBytes = DllStructCreate('int TB;int GB;int MB;int KB;int Byte;')
	Local Static $aUnit[5] = ['TB','GB','MB','KB','Byte']
	Local $iModulo = $_iByte, $iHighest = 4
	For $i = 0 To 3
		$aByte[$i][1] = $iModulo >= $aByte[$i][0] ? Floor($iModulo/$aByte[$i][0]) : 0
		$iModulo = $aByte[$i][1] > 0 ? Mod($iModulo,$aByte[$i][0]) : $iModulo
		$iHighest = $aByte[$i][1] > 0 ? ($i < $iHighest ? $i : $iHighest) : $iHighest
	Next
	$aByte[4][1] = $iModulo
	If $_fStruct Then
		$tBytes.TB   = $aByte[0][1]
		$tBytes.GB   = $aByte[1][1]
		$tBytes.MB   = $aByte[2][1]
		$tBytes.KB   = $aByte[3][1]
		$tBytes.Byte = $aByte[4][1]
		Return $tBytes
	EndIf
	$_sUnit = StringInStr('TB GB MB KB Byte', $_sUnit) ? $_sUnit : ''
	$_sUnit = $_sUnit = '' ? $aUnit[$iHighest] : $_sUnit
	Local $iUserUnit = Floor(StringInStr('TB GB MB KB Byte', $_sUnit)/3)
	If Number($_sDigit) < 0 Then $_sDigit = '0'
	Local $sFormat = '%.' & $_sDigit & 'f %s'
	Return StringFormat($sFormat, $_iByte/$aByte[$iUserUnit][0], $aUnit[$iUserUnit])
EndFunc  ;==>_FormatByte

;===============================================================================
; Function Name:   _BuildInZIP($sPath, $s_a_ToZip, $fUnZip=False, $iIndexBase=1, $fOverwrite=True)
; Description:     Erstellt aus einer Datei od. einem Array von Dateien eine ZIP-Datei
;                  mit Hilfe der Windows eigenen ZIP-Funktion
; Parameter(s):    $sPath       Pfad der zu erstellenden ZIP-Datei // Bestehenden ZIP-Datei
;                  $s_a_ToZip   String der Einzeldatei oder Array der zu zippenden Dateien
;                               // Pfad des Zielordners, in den entpackt werden soll
;                  $fUnZip=True Entpacken=True, Packen=False (Standard)
;                  $iIndexBase  Index Basis (erstes Element mit Pfad) des übergebenen Arrays (0 od.1)
;                  $fOverwrite  Falls $sPath existiert ==> ݢerschreiben (Standard=True)
; Return Value(s): Erfolg  1
;                  Fehler  set @error  1 - Überschreiben aus, aber Zieldatei existiert
;                                      2 - Zieldatei endet nicht auf ".zip"
;                                      3 - zu komprimierende Datei existiert nicht
;                                          @extended: Anzahl der fehlenden Dateien
; Author(s):       BugFix (bugfix@autoit.de)
;===============================================================================
Func _BuildInZIP($sPath, $s_a_ToZip, $fUnZip=False, $iIndexBase=1, $fOverwrite=True)
	Local $fh, $sNul = '', $sZIPHeader, $objShell = ObjCreate("Shell.Application"), $objZipOrdner, $err = 0
	Local Const $_FOF_CREATEPROGRESSDLG = 0x0 ; Fortschrittsanzeige
	Local Const $_FOF_NOCONFIRMATION = 0x10	  ; Dateien überschreiben
	Local Const $_FOF_NOCONFIRMMKDIR = 0x200  ; Verzeichnisse o. Nachfrage erstellen
	Local Const $_FOF_NOERRORUI = 0x400       ; Keine Fehleranzeige
	Local $flag = BitOR($_FOF_CREATEPROGRESSDLG, $_FOF_NOCONFIRMATION, $_FOF_NOCONFIRMMKDIR, $_FOF_NOERRORUI)
	If Not $fOverwrite Then $flag = BitXOR($flag, $_FOF_NOCONFIRMATION)
	$sPath = FileGetLongName($sPath, 1)
	If $fUnZip Then
		Local $temp, $objZip
		If Not FileExists($s_a_ToZip) Then DirCreate($s_a_ToZip)
		$temp = $objShell.Namespace($s_a_ToZip)
		$objZip = $objShell.Namespace($sPath)
		$temp.CopyHere($objZip.Items, $flag)
		Return 1
	EndIf
	If (Not $fOverwrite) And FileExists($sPath) Then Return SetError(1,0,0)
	If StringRight($sPath, 4) <> '.zip' Then Return SetError(2,0,0)
	For $i = 1 To 18
		$sNul &= Chr(0)
	Next
	$sZIPHeader = Chr(80) & Chr(75) & Chr(5) & Chr(6) & $sNul
	$fh = FileOpen($sPath, 10)
	FileWrite($fh, $sZIPHeader)
	FileClose($fh)
	$objZipOrdner = $objShell.Namespace($sPath)
	If Not IsArray($s_a_ToZip) Then
		If Not FileExists($s_a_ToZip) Then Return SetError(3,1,0)
		$objZipOrdner.CopyHere($s_a_ToZip, $flag)
		Sleep(1000)
		Return 1
	EndIf
	For $i = $iIndexBase To UBound($s_a_ToZip) -1
		If Not FileExists($s_a_ToZip[$i]) Then
			$err += 1
			ContinueLoop
		EndIf
		$objZipOrdner.CopyHere($s_a_ToZip[$i], $flag)
		Sleep(1000)
	Next
	If $err Then Return SetError(3,$err,0)
	Return 1
EndFunc  ;==>_BuildInZIP
