[API]
WriteFile

[INCLUDE]
ctPackage_GetFileCount
BinaryMoveFileDataEx

[DEFINE]
Public Enum ctPackage_IfExistsModes
  ctPackage_Exists_NoCheck = 0&
  ctPackage_Exists_Break = 1&
  ctPackage_Exists_Update = 2&
  ctPackage_Exists_Skip = 3&
  ctPackage_Exists_ExceptionError1 = 4&
  ctPackage_Exists_ExceptionStop = 5&
  ctPackage_Exists_ExceptionEnd = 6&
  ctPackage_Exists_ExceptionCrash = 7&
  End Enum
Public Enum ctPackage_DoubleNewFilesModes
  ctPackage_DoubleNewFiles_NoCheck = 0&
  ctPackage_DoubleNewFiles_Break = 1&
  ctPackage_DoubleNewFiles_SkipFirst = 2&
  ctPackage_DoubleNewFiles_SkipLast = 3&
  ctPackage_DoubleNewFiles_ExceptionError1 = 4&
  ctPackage_DoubleNewFiles_ExceptionStop = 5&
  ctPackage_DoubleNewFiles_ExceptionEnd = 6&
  ctPackage_DoubleNewFiles_ExceptionCrash = 7&
  End Enum

[CODE]
Public Function ctPackage_Create(ByVal FileName As String, ByRef ctPackagePointer As Long, Optional ByVal CanUseExisting As Boolean = True) As Boolean
  Dim xChar1 As String
  Dim ctPIType As ctPackage_InfoType

  ctPackage_Create = ctPackage_Open(FileName, ctPackagePointer, True)
'Stop
  If Not ctPackage_Create Then
    ctPIType.FileHandle = CreateFile(FileName, CreateOrReuse, FILE_READWRITE)
    If Not ctPIType.FileHandle = -1& Then
      ctPIType.FileHeader = GetFileSize(ctPIType.FileHandle, 0&)
      If Not ctPIType.FileHeader = -1& Then
        ' xChar1 = ctPackage_InternalUsage_EmptyHeader & ctPackageConst_EndSizeText1 & ctPackage_LenLongToChar(ctPIType.FileHeader) & ctPackageConst_EndSizeText2
        xChar1 = ctPackageConst_HeaderInfoText & String(ctPackageConst_HeaderEmptyNullsWidth, vbNullChar) & ctPackageConst_EndSizeText1 & ctPackage_LenLongToChar(ctPIType.FileHeader) & ctPackageConst_EndSizeText2
        If Not SetFilePointer(ctPIType.FileHandle, ctPIType.FileHeader, 0&, 0&) = -1& Then
          'Put FileID, ctPackage_Create + 1&, xChar1
          If Not WriteFile(ctPIType.FileHandle, ByVal xChar1, ctPackageConst_HeaderSize + ctPackageConst_EndSize, 0&, ByVal 0&) = 0& Then
            ctPackagePointer = LocalAlloc(0&, ctPackage_InfoTypeSize)
            If Not ctPackagePointer = 0& Then
              ctPIType.CanModify = cByte1
              Call RtlMoveMemory(ByVal ctPackagePointer, ctPIType, ctPackage_InfoTypeSize)
              ctPackage_Create = True
              End If
            End If
          End If
        End If
      If Not ctPackage_Create Then
        Call CloseHandle(ctPIType.FileHandle)
        End If
      End If
    Else
    If Not CanUseExisting Then
      If Not ctPackage_GetFileCount(ctPackagePointer) = 0& Then
        ctPackage_Create = False
        End If 'ctPackage_Create = True
      End If
    End If
  End Function

Public Function ctPackage_Update( _
  ByVal ctPackagePointer As Long, _
  ByRef FilesExternInternOffsetArray() As String, _
  ByVal FileArrayCount As Long, _
  Optional ByVal DelFileOrNotByteStream As String, _
  Optional ByVal CanCompress As Boolean = True, _
  Optional ByVal BreakIfFileAccessError As Boolean = False, _
  Optional ByVal FileExistsHandling As ctPackage_IfExistsModes = ctPackage_Exists_Break, _
  Optional ByVal Reserved_DoubleAttacheFilesHandling As ctPackage_DoubleNewFilesModes = ctPackage_DoubleNewFiles_Break, _
  Optional ByVal ctPackagePointer_OutFile As Long, _
  Optional ByVal MinimumCompress As Long = ctPackage_MinimumCompressDefault, _
  Optional ByVal ProgressControl As Long = 0&) As Long



  Dim xFilenamesSize As Long
  Dim xFileNames As String
  Dim xDataSize As Long
  Dim xOldDataSize As Long
  Dim xTOC As String
  Dim xTOCNew As String
  Dim xDataPos As Long
  Dim xNewDataPos As Long
  Dim xLong3 As Long
  Dim xFileCount As Long
  Dim xLongA1(65535) As Long
  Dim xLongA2(65535) As Long
  'Dim xRemove As Long
  Dim xNewFlag As Long
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong4 As Long
  Dim xLong6 As Long
  Dim xChar1 As String
  Dim xChar2 As String
  Dim xIsSameFile As Boolean
  
  Dim xLong5 As Long
  Dim xProgress1 As Long
  Dim xProgress2 As Long
  
  Dim ctPIType As ctPackage_InfoType
  Dim ctPITypeOut As ctPackage_InfoType
  
  Dim TempFileHandle As Long
  
'    Stop
  If ctPackage_GetInfoEx(ctPackagePointer, ctPIType) Then
    If Not ctPackagePointer_OutFile = 0& Then
      If Not ctPackage_GetInfoEx(ctPackagePointer_OutFile, ctPITypeOut) Then GoTo Lb2
      If (ctPITypeOut.FileHandle = ctPIType.FileHandle) Then
        If ctPITypeOut.FileHeader = ctPIType.FileHeader Then
          xIsSameFile = True
          Else
          GoTo Lb2
          End If
        End If
      Else
      ctPITypeOut = ctPIType
      xIsSameFile = True
      End If
    If ctPITypeOut.CanModify Then
      
      
      On Error GoTo Er1
      
      For xLong1 = FileArrayCount - 1& To 0& Step -1&
        If FilesExternInternOffsetArray(xLong1 * 3 + 2&) = "" Then
          If Left(FilesExternInternOffsetArray(xLong1 * 3&), 1&) = vbNullChar Then
            xLongA2(xLong1) = Len(FilesExternInternOffsetArray(xLong1 * 3&)) - 1&
            Else
            'If (GetAttr(FilesExternInternOffsetArray(xLong1 * 3&)) Mod 32&) \ 16& = 1& Then
            '  Error 1
            '  Else
            '  Open FilesExternInternOffsetArray(xLong1 * 3&) For Input As FileAccessFileID
            '  Close FileAccessFileID
            '  Open FilesExternInternOffsetArray(xLong1 * 3&) For Binary Access Read As FileAccessFileID
            '  xLongA2(xLong1) = LOF(FileAccessFileID)
            '  Close FileAccessFileID
            '  End If
            xLong5 = CreateFile(FilesExternInternOffsetArray(xLong1 * 3&), OpenExisting, FILE_READ)
            If xLong5 = -1& Then
              Error 75
              Else
              xLongA2(xLong1) = GetFileSize(xLong5, 0&)
              Call CloseHandle(xLong5)
              End If
            End If
          Else
          'xLongA2(xLong1) = ctPackage_LenCharToLong(FilesExternInternOffsetArray(xLong1 * 3 + 2&), 10&)
          xLongA2(xLong1) = ctPackage_LenCharToLong(FilesExternInternOffsetArray(xLong1 * 3 + 2&), ctPackageConst_GetFileList_PosIntSize7)
          'If Not xLongA2(xLong1) + 13& = Len(FilesExternInternOffsetArray(xLong1 * 3& + 2&)) Then Stop
          End If
Rs1:
        Next
      
      '''xLongA2(xLong1) = ctPackage_LenCharToLong(FilesExternInternOffsetArray(xLong1 * 3 + 1&), ctPackageConst_GetFileList_PosIntSize7)
      If CanCompress Then xNewFlag = 1&
      
      On Error GoTo 0
      xChar1 = String(ctPackageConst_HeaderSize, vbNullChar)
      
      
      
      'Get FileID, ctpitype.fileheader + 1&, xChar1
      If Not SetFilePointer(ctPIType.FileHandle, ctPIType.FileHeader, 0&, 0&) = -1& Then
        If Not ReadFile(ctPIType.FileHandle, ByVal xChar1, ctPackageConst_HeaderSize, 0&, ByVal 0&) = 0& Then
          ctPackage_Update = ctPackage_LenCharToLong(xChar1, ctPackageConst_HeaderPosFileCount + 1&)
          xFilenamesSize = ctPackage_LenCharToLong(xChar1, ctPackageConst_HeaderPosFileNames)
          xOldDataSize = ctPackage_LenCharToLong(xChar1, ctPackageConst_HeaderPosDataSize)
          
          
          'xDataPos = ctpitype.fileheader + ctPackageConst_HeaderSize + ctPackage_Update * ctPackageConst_TOCWidth + xFilenamesSize
          xTOC = String(ctPackage_Update * ctPackageConst_TOCWidth, vbNullChar)
          xFileNames = String(xFilenamesSize, vbNullChar)
          
          'Get FileID, ctpitype.fileheader + ctPackageConst_HeaderSize + 1, xTOC
          If Not ReadFile(ctPIType.FileHandle, ByVal xTOC, ctPackage_Update * ctPackageConst_TOCWidth, 0&, ByVal 0&) = 0& Then
          'Get FileID, , xFileNames
            If Not ReadFile(ctPIType.FileHandle, ByVal xFileNames, xFilenamesSize, 0&, ByVal 0&) = 0& Then
          
              TempFileHandle = -1&
              xDataPos = ctPIType.FileHeader + ctPackageConst_HeaderSize + ctPackage_Update * ctPackageConst_TOCWidth + xFilenamesSize
              xLong5 = xDataPos
              
          '    Stop
              'If Not (FileExistsHandling = ctPackage_Exists_NoCheck Or ctPackage_Update = 0&) And DelFileOrNotByteStream = String(Len(DelFileOrNotByteStream), vbNullChar) Then
              If Not ctPackage_Update = 0& Then
                xChar1 = xFileNames
                xFileNames = ""
              
                
                For xLong2 = 0& To ctPackage_Update - 1&
                  
                  xLong3 = ctPackage_LenCharToLong(xTOC, xLong2 * ctPackageConst_TOCWidth + ctPackageConst_TOCPosFileNameSize)
                  xLong4 = ctPackage_LenCharToLong(xTOC, xLong2 * ctPackageConst_TOCWidth + ctPackageConst_TOCPosIntSize)
                  xChar2 = UCase(Left(xChar1, xLong3))
                  
                  If Mid(DelFileOrNotByteStream, xLong2 + 1&, 1&) = Chr(1) Then
                    xChar2 = vbNullChar
                    Else
                  
                  
                  
                    For xLong1 = 0& To FileArrayCount - 1&
                      If UCase(FilesExternInternOffsetArray(xLong1 * 3& + 1&)) = xChar2 Then
                        Select Case FileExistsHandling
                          Case ctPackage_Exists_Break
                            Exit Function
                          Case ctPackage_Exists_Update
                            xChar2 = vbNullChar
                          Case ctPackage_Exists_Skip
                            For xLong6 = xLong1 * 3& To FileArrayCount * 3& - 4&
                              FilesExternInternOffsetArray(xLong6) = FilesExternInternOffsetArray(xLong6 + 3&)
                              Next
                            For xLong6 = xLong1 To FileArrayCount - 2&
                              xLongA2(xLong6) = xLongA2(xLong6 + 1&)
                              Next
                            FileArrayCount = FileArrayCount - 1&
                          Case ctPackage_Exists_ExceptionError1
                            Error 1
                            Exit Function
                          Case ctPackage_Exists_ExceptionStop
                            Stop
                          Case ctPackage_Exists_ExceptionEnd
                            End
                          Case ctPackage_Exists_ExceptionCrash
                            Stop
                            Stop
                            Stop
                          End Select
                        End If
                      Next
                    End If
                  
          '        Stop
                  If xChar2 = vbNullChar Then
                    ctPackage_Update = ctPackage_Update - 1&
                    Else
                    xDataSize = xDataSize + xLong4
                    xFileNames = xFileNames & Left(xChar1, xLong3)
                    xTOCNew = xTOCNew + Mid(xTOC, xLong2 * ctPackageConst_TOCWidth + 1&, ctPackageConst_TOCWidth)
                    If xFileCount = 0& Then
                      xLongA1(xFileCount * 2&) = xDataPos
                      xLongA1(xFileCount * 2& + 1&) = xLong4
                      xFileCount = xFileCount + 1&
                      Else
                      If xLongA1(xFileCount * 2& - 2&) + xLongA1(xFileCount * 2& - 1&) = xDataPos Then
                        xLongA1(xFileCount * 2& - 1&) = xLongA1(xFileCount * 2& - 1&) + xLong4
                        Else
                        xLongA1(xFileCount * 2&) = xDataPos
                        xLongA1(xFileCount * 2& + 1&) = xLong4
                        xFileCount = xFileCount + 1&
                        End If
                      End If
                    End If
                  xChar1 = Mid(xChar1, xLong3 + 1)
                  xDataPos = xDataPos + xLong4
                  Next
                End If
              
          '    Stop
              xLong2 = xDataSize
              '''''''''xDataSize = xDataSize + xLongA2(xLong1)
              For xLong1 = 0& To FileArrayCount - 1&
                ''''''xDataSize = xDataSize + xLongA2(xLong1)
                xLong2 = xLong2 + xLongA2(xLong1)
                xTOCNew = xTOCNew & vbNullChar & ctPackage_LenLongToChar(Len(FilesExternInternOffsetArray(xLong1 * 3& + 1&))) & _
                          String(4&, vbNullChar) & _
                          ctPackage_LenLongToChar(xLongA2(xLong1))
                xFileNames = xFileNames & FilesExternInternOffsetArray(xLong1 * 3& + 1&)
                Next
              '''xNewFileCount = ctPackage_update + FileArrayCount
              'ctPackage_update = ctPackage_update + FileArrayCount
              xNewDataPos = ctPITypeOut.FileHeader + ctPackageConst_HeaderSize + (ctPackage_Update + FileArrayCount) * ctPackageConst_TOCWidth + Len(xFileNames)
              
              xChar1 = ctPackageConst_HeaderInfoText & String(ctPackageConst_HeaderEmptyNullsWidth, vbNullChar) ' ctPackage_InternalUsage_EmptyHeader
              
              
              
              If Not SetFilePointer(ctPITypeOut.FileHandle, ctPITypeOut.FileHeader, 0&, 0&) = -1& Then
                'Put OutFileID, ctPITypeOut.FileHeader + 1&, xChar1
                If Not WriteFile(ctPITypeOut.FileHandle, ByVal xChar1, ctPackageConst_HeaderSize, 0&, ByVal 0&) = 0& Then
                  'If xDataSize > xOldDataSize Then
                  
                  If xIsSameFile Then
                    'xOldDataSize = LOF(OutFileID) - ctPackageConst_EndSize
                    xOldDataSize = GetFileSize(ctPITypeOut.FileHandle, 0&) '- ctPackageConst_EndSize
                    End If
                  
                  'If xLong2 > xOldDataSize - xLong5 Or NoClearWaste Then
                    xProgress2 = xLong2 'xDataSize
                  '  Else
                  '  xProgress2 = xOldDataSize - xNewDataPos 'xLong5
                  '  End If
                      
                  
                  'xLong5 = xNewDataPos
                  For xLong4 = 0& To xFileCount - 1&
                    'If xLong5 <= xLongA1(xLong4 * 2&) Then
                    If xNewDataPos <= xLongA1(xLong4 * 2&) Then
                      Exit For
                      End If
                    'xLong5 = xLong5 + xLongA1(xLong4 * 2& + 1&)
                    xNewDataPos = xNewDataPos + xLongA1(xLong4 * 2& + 1&)
                    Next
                  
                  'For xLong1 = xLong4 To xFileCount - 1&
                  '  xNewDataPos = xNewDataPos + xLongA1(xLong1 * 2& + 1&)
                  '  Next
                  
                  'xLong2 = xNewDataPos
                  xLong2 = xNewDataPos
                  For xLong1 = xLong4 - 1& To 0& Step -1&
                    xLong3 = xLongA1(xLong1 * 2& + 1&)
                    xLong2 = xLong2 - xLong3
                    'xLong5 = xLong5 - xLong3
                  
                    Call BinaryMoveFileDataEx(ctPIType.FileHandle, xLong3, xLongA1(xLong1 * 2&), xLong2, ctPITypeOut.FileHandle, ctPackage_DefaultBlockSize, ProgressControl, xProgress1, xProgress2)
                  '  xDataSize = xDataSize + xLong3
                    xProgress1 = xProgress1 + xLong3
                    Next
              '    Stop
                  For xLong1 = xLong4 To xFileCount - 1&
                    xLong3 = xLongA1(xLong1 * 2& + 1&)
                    'Call BinaryMoveFileData(FileID, xLong3, xLongA1(xLong1 * 2&), xLong5, -1&,  ctPackage_DefaultBlockSize, ProgressControl, xProgress1, xProgress2)
                    Call BinaryMoveFileDataEx(ctPIType.FileHandle, xLong3, xLongA1(xLong1 * 2&), xNewDataPos, ctPITypeOut.FileHandle, ctPackage_DefaultBlockSize, ProgressControl, xProgress1, xProgress2)
                    xProgress1 = xProgress1 + xLong3
                  '  xDataSize = xDataSize + xLong3
                  '  xProgress1 = xProgress1 + xLong3
                    'xLong5 = xLong5 + xLong3
                    xNewDataPos = xNewDataPos + xLong3
                    Next
                  
              '    Stop
                  'xProgress1 = xOldDataSize
                  'xNewDataPos = xLong5
                  
                  'xLongA2(xLong1) = ctPackage_LenCharToLong(FilesExternInternOffsetArray(xLong1 * 3 + 1&), ctPackageConst_GetFileList_PosIntSize7)
                  
                  For xLong1& = 0& To FileArrayCount - 1&
                  
                  
'                      Stop
                    xLong5 = xLongA2(xLong1)
                    If xLongA2(xLong1) = 0& Then
                      xLong4 = 0& 'xNewFlag And Not 1&
                      Else
                      
                  '      xChar1 = Mid(FilesExternInternOffsetArray(xLong1 * 3&), 2&)
                      If Not FilesExternInternOffsetArray(xLong1 * 3&) = "" Then
                        If Not Left(FilesExternInternOffsetArray(xLong1 * 3&), 1&) = vbNullChar Then
                          If Not TempFileHandle = -1& Then
                            Call CloseHandle(TempFileHandle)
                            End If
                          'Close FileAccessFileID
                          'Open FilesExternInternOffsetArray(xLong1 * 3&) For Input As FileAccessFileID
                          'Close FileAccessFileID
                          'Open FilesExternInternOffsetArray(xLong1 * 3&) For Binary Access Read As FileAccessFileID
                          TempFileHandle = CreateFile(FilesExternInternOffsetArray(xLong1 * 3&), OpenExisting, FILE_READ)
                          If TempFileHandle = -1& Then
                            Error 75
                            'Else
                            'xLongA2(xLong1) = GetFileSize(xLong5, 0&)
                            'Call CloseHandle(xLong5)
                            End If
                          
                          End If
                        End If
                        
                      If FilesExternInternOffsetArray(xLong1 * 3& + 2&) = "" Then
                        If xNewFlag = 0& Then
                          If Left(FilesExternInternOffsetArray(xLong1 * 3&), 1&) = vbNullChar Then
                            xChar1 = Mid(FilesExternInternOffsetArray(xLong1 * 3&), 2&)
                            'Put OutFileID, xNewDataPos + 1&, xChar1
                            If SetFilePointer(ctPITypeOut.FileHandle, xNewDataPos, 0&, 0&) = -1& Then
                              Error 57
                              ElseIf WriteFile(ctPITypeOut.FileHandle, ByVal xChar1, Len(xChar1), 0&, ByVal 0&) = 0& Then
                              Error 61 '?
                              End If
                            
                            Else
                            Call BinaryMoveFileDataEx(TempFileHandle, xLongA2(xLong1), 0&, xNewDataPos, ctPITypeOut.FileHandle, ctPackage_DefaultBlockSize, ProgressControl, xProgress1, xProgress2)
                            End If
                          xLong4 = 0&
                          Else
                          xLong4 = xNewFlag
                          If Left(FilesExternInternOffsetArray(xLong1 * 3&), 1&) = vbNullChar Then
                            xChar1 = Mid(FilesExternInternOffsetArray(xLong1 * 3&), 2&)
                            Else
                            xChar1 = String(xLongA2(xLong1), vbNullChar)
                            'Get FileAccessFileID, 1&, xChar1
                            If ReadFile(ctPIType.FileHandle, ByVal xChar1, xLongA2(xLong1), 0&, ByVal 0&) = 0& Then
                              Error 75
                              'Else
                              End If
                            End If
                        
                          #If ctCompressA_Compress Then
                            If CanCompress Then
                              xChar2 = ctCompressA_Compress(xChar1, ProgressControl, xProgress1, xProgress2, xLongA2(xLong1))
                              xLong4 = Len(xChar2)
                              If xLong4 < MinimumCompress / 100& * xLongA2(xLong1) Or MinimumCompress > 100& Then
                                xChar1 = xChar2
                                xLongA2(xLong1) = xLong4
                                xLong4 = xNewFlag
                                Else
                                xLong4 = xNewFlag And Not 1&
                                End If
                              Else
                              xLong4 = xNewFlag
                              End If
                            #Else
                            xLong4 = xNewFlag
                            #End If
                            
                          'Put OutFileID, xNewDataPos + 1&, xChar1
                          If SetFilePointer(ctPITypeOut.FileHandle, xNewDataPos, 0&, 0&) = -1& Then
                            Error 57
                            ElseIf WriteFile(ctPITypeOut.FileHandle, ByVal xChar1, Len(xChar1), 0&, ByVal 0&) = 0& Then
                            Error 61 '?
                            End If
                          End If
                          
                          
                        Else
                        xLong6 = ctPackage_LenCharToLong(FilesExternInternOffsetArray(xLong1 * 3 + 2&), 2&)
                        If xLong6 = 0& Then
                          xChar1 = Mid(FilesExternInternOffsetArray(xLong1 * 3 + 2&), 14&)
                          'Put OutFileID, xNewDataPos + 1&, xChar1
                          If SetFilePointer(ctPITypeOut.FileHandle, xNewDataPos, 0&, 0&) = -1& Then
                            Error 57
                            ElseIf WriteFile(ctPITypeOut.FileHandle, ByVal xChar1, Len(xChar1), 0&, ByVal 0&) = 0& Then
                            Error 61 '?
                            End If
                          Else
                          Call BinaryMoveFileDataEx(TempFileHandle, xLongA2(xLong1), ctPackage_LenCharToLong(FilesExternInternOffsetArray(xLong1 * 3 + 2&), 2&), xNewDataPos, ctPITypeOut.FileHandle, ctPackage_DefaultBlockSize, ProgressControl, xProgress1, xProgress2)
                          End If
                        xLong4 = Asc(Left(FilesExternInternOffsetArray(xLong1 * 3 + 2&), 1&))
                        xTOCNew = Left(xTOCNew, (ctPackage_Update + xLong1) * ctPackageConst_TOCWidth + ctPackageConst_TOCOverWriteExtSizePos) & _
                                  Mid(FilesExternInternOffsetArray(xLong1 * 3 + 2&), ctPackageConst_GetFileList_PosExtSize7, 4&) & _
                                  Mid(xTOCNew, (ctPackage_Update + xLong1) * ctPackageConst_TOCWidth + ctPackageConst_TOCOverWriteExtSizePosRs)
                  '      xLong5 = ctPackage_LenCharToLong(FilesExternInternOffsetArray(xLong1 * 3 + 2&), ctPackageConst_TOCParamPosExtSize)
                        End If
                        
                      xDataSize = xDataSize + xLongA2(xLong1)
                      End If
                    xTOCNew = Left(xTOCNew, (ctPackage_Update + xLong1) * ctPackageConst_TOCWidth) & _
                              Chr(xLong4) & _
                              Mid(xTOCNew, (ctPackage_Update + xLong1) * ctPackageConst_TOCWidth + ctPackageConst_TOCPWSecondStart, ctPackageConst_TOCPWSecondWidth) & _
                              ctPackage_LenLongToChar(xLongA2(xLong1)) & _
                              Mid(xTOCNew, (ctPackage_Update + xLong1) * ctPackageConst_TOCWidth + ctPackageConst_TOCPosLast)
                  '  xTOCNew = Left(xTOCNew, (ctPackage_Update + xLong1) * ctPackageConst_TOCWidth) & _
                  '            Chr(xLong4) & _
                  '            Mid(xTOCNew, (ctPackage_Update + xLong1) * ctPackageConst_TOCWidth + ctPackageConst_TOCPWSecondStart, ctPackageConst_TOCPWSecondWidth) & _
                  '            ctPackage_LenLongToChar(xLong5) & _
                  '            Mid(xTOCNew, (ctPackage_Update + xLong1) * ctPackageConst_TOCWidth + ctPackageConst_TOCPosLast)
                    
                    'xDataSize = xDataSize + xLongA2(xLong1)
                    xNewDataPos = xNewDataPos + xLongA2(xLong1)
                    xProgress1 = xProgress1 + xLong5 'xLongA2(xLong1) ' + 1&)
                    ctProgressB_SetValue ProgressControl, xProgress1, xProgress2
                    DoEvents
                    
                    
                    
                    
                    
                  ''''''''''''''''''''''''''''''''  If xAbort Then Exit Function
                    Next
                  
                  If Not TempFileHandle = -1& Then
                    Call CloseHandle(TempFileHandle)
                    End If
                  'Close FileAccessFileID
                  'Stop
                  'If xOldDataSize > xNewDataPos And Not NoClearWaste Then
                    'Call BinaryClearFileData(OutFileID, xNewDataPos, xOldDataSize - xNewDataPos, ctPackage_DefaultBlockSize, ProgressControl, xProgress1, xProgress2)
                  '  Stop
                  '  End If
                  
                  
'                    Stop
                  'xChar1 = ctPackageConst_HeaderInfoText & ctPackage_LenLongToChar(ctPackage_Update + FileArrayCount) & ctPackage_LenLongToChar(Len(xFileNames)) & ctPackage_LenLongToChar(xDataSize) & xTOCNew & xFileNames
                  ctPackage_Update = ctPackage_Update + FileArrayCount
                  xChar1 = ctPackageConst_HeaderInfoText & ctPackage_LenLongToChar(ctPackage_Update) & ctPackage_LenLongToChar(Len(xFileNames)) & ctPackage_LenLongToChar(xDataSize) & xTOCNew & xFileNames
                  'Put OutFileID, ctPIType.FileHeader + 1&, xChar1
                  If SetFilePointer(ctPITypeOut.FileHandle, ctPIType.FileHeader, 0&, 0&) = -1& Then
                    Error 57
                    ElseIf WriteFile(ctPITypeOut.FileHandle, ByVal xChar1, Len(xChar1), 0&, ByVal 0&) = 0& Then
                    Error 61 '?
                    End If
                  
'                    Stop
                  xChar1 = ctPackageConst_EndSizeText1 & ctPackage_LenLongToChar(ctPITypeOut.FileHeader) & ctPackageConst_EndSizeText2
                  'xLong1 = LOF(OutFileID) - ctPackageConst_EndSize
                  'If xLong1 > xNewDataPos Then 'Or NoClearWaste Then
                  '  Put OutFileID, xLong1 + 1&, xChar1
                  '  Else
                  '  Put OutFileID, xNewDataPos + 1&, xChar1
                  '  End If
                  'If SetFilePointer(ctPITypeOut.FileHandle, GetFileSize(ctPITypeOut.FileHandle, 0&), 0&, 0&) = -1& Then
'                    Stop
                  If SetFilePointer(ctPITypeOut.FileHandle, xNewDataPos, 0&, 0&) = -1& Then
                    Error 57
                    ElseIf WriteFile(ctPITypeOut.FileHandle, ByVal xChar1, ctPackageConst_EndSize, 0&, ByVal 0&) = 0& Then
                    Error 61 '?
                    End If
                  If xOldDataSize > xNewDataPos + ctPackageConst_EndSize And xIsSameFile Then
                    Stop
                    End If
Lb1:
                  On Error GoTo 0
                  'Close FileAccessFileID
                
                  End If
                End If
              End If
            End If
          End If
        End If
      End If
    End If
Lb2:
  
  
  
  
  
  
  
  
  
  Exit Function
  
Er1:
  If BreakIfFileAccessError Then
    Resume Lb1
    Else
   ' For xLong2 = xLong1 To FileArrayCount - 3&
   '   FilesExternInternOffsetArray(xLong2 * 3&) = FilesExternInternOffsetArray(xLong2 * 3& + 3&)
   '   FilesExternInternOffsetArray(xLong2 * 3& + 1&) = FilesExternInternOffsetArray(xLong2 * 3& + 4&)
   '   FilesExternInternOffsetArray(xLong2 * 3& + 2&) = FilesExternInternOffsetArray(xLong2 * 3& + 5&)
   '   FileArrayCount = FileArrayCount - 1&
   '   Next
  '              For xLong5 = xLong1 * 3& To FileArrayCount * 3& - 4&
    For xLong2 = xLong1 * 3& To FileArrayCount * 3& - 4&
      FilesExternInternOffsetArray(xLong2) = FilesExternInternOffsetArray(xLong2 + 3&)
      Next
    
    For xLong2 = xLong1 To FileArrayCount - 2&
      xLongA2(xLong2) = xLongA2(xLong2 + 1&)
      Next
    Resume Rs1
    End If
  End Function

