'  
'   Rebex Sample Code License
' 
'   Copyright 2025, REBEX CR s.r.o.
'   All rights reserved.
'   https://www.rebex.net/
' 
'   Permission to use, copy, modify, and/or distribute this software for any
'   purpose with or without fee is hereby granted.
' 
'   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
'   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
'   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
'   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
'   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
'   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
'   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
'   OTHER DEALINGS IN THE SOFTWARE.
' 

Imports System
Imports System.IO
Imports System.Collections.Generic
Imports System.Text

Imports Rebex.IO
Imports Rebex.IO.Compression

''' <summary>
''' Simple command line utility that compresses files and/or directories into a ZIP archive.
''' </summary>
Class Program
    ''' <summary>
    ''' The main entry point for the application.
    ''' </summary>
    ''' <param name="args">Program arguments.</param>
    Shared Sub Main(ByVal args As String())
            ' set the trial license key
            Rebex.Licensing.Key = Rebex.Samples.TrialKey.Key


        If args.Length = 0 Then
            ShowHelp()
            Return
        End If

        Try
            ' parse the command line
            Dim config As New Arguments(args)

            ' You can execute the whole operation in a single line of code as follows:
            '   ZipArchive.Add(config.ZipArchivePath, config.SourcePath, Nothing, 0, 0, ActionOnExistingFiles.OverwriteAll)

            ' If you need to perform multiple ZIP archive operations,
            ' then the following pattern is more efficient:

            ' open or create a ZIP archive and add files to it
            Using archive As New ZipArchive(config.ZipArchivePath)
                Dim result As ArchiveOperationResult = archive.Add( _
                    config.SourcePath, _
                    Nothing, _
                    TraversalMode.Recursive, _
                    TransferMethod.Copy, _
                    ActionOnExistingFiles.OverwriteAll)

                Console.WriteLine("Added {0} file(s), {1} byte(s) to {2}.", _
                    result.FilesAffected, _
                    result.FilesUncompressedLength, _
                    archive.FilePath)
            End Using
        Catch e As SampleException
            ' display an error reported by arguments parser class
            Console.WriteLine("Error: {0}", e.Message)

            ' display help
            ShowHelp()
        Catch e As ZipException
            ' display an error reported by the ZIP component
            Console.WriteLine(e.Message)
        Catch e As DirectoryNotFoundException
            ' display directory-not-found error
            Console.WriteLine(e.Message)
        Catch e As FileNotFoundException
            ' display file-not-found error
            Console.WriteLine(e.Message)
        Catch e As Exception
            ' display all other errors
            Console.WriteLine("Error: {0}", e)
        End Try
    End Sub

    Private Shared Sub ShowHelp()
        Dim applicationName As String = AppDomain.CurrentDomain.FriendlyName
        Console.WriteLine("=====================================================================")
        Console.WriteLine(" Rebex {0} - Adds files to a ZIP archive", applicationName)
        Console.WriteLine("=====================================================================")
        Console.WriteLine()
        Console.WriteLine("The program is a sample for Rebex ZIP for .NET component.")
        Console.WriteLine("For more info, see http://www.rebex.net/zip.net/")
        Console.WriteLine()
        Console.WriteLine("Syntax: {0} zip-file-path [path-or-mask]", applicationName)
        Console.WriteLine("Example: {0} C:\archive.zip ", applicationName)
        Console.WriteLine("Example: {0} C:\archive.zip C:\file.txt", applicationName)
        Console.WriteLine("Example: {0} C:\archive.zip C:\data", applicationName)
        Console.WriteLine("Example: {0} C:\archive.zip C:\data\*", applicationName)
        Console.WriteLine("Example: {0} C:\archive.zip C:\data\*.txt", applicationName)
    End Sub
End Class
