'  
'   Rebex Sample Code License
' 
'   Copyright 2026, 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 decompresses files and/or directories from 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)

            ' create a target directory if needed
            If Not Directory.Exists(config.TargetPath) Then
                Directory.CreateDirectory(config.TargetPath)
            End If

            ' You can execute the whole operation in a single line of code as follows:
            '   ZipArchive.ExtractAll(config.ZipArchivePath, config.TargetPath, TransferMethod.Copy, ActionOnExistingFiles.OverwriteAll)

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

            ' open zip archive and extract files from it
            Using archive As New ZipArchive(config.ZipArchivePath, ArchiveOpenMode.Open)
                Dim result As ArchiveOperationResult = archive.ExtractAll( _
                    config.TargetPath, _
                    TransferMethod.Copy, _
                    ActionOnExistingFiles.OverwriteAll)

                Console.WriteLine("Extracted {0} file(s), {1} byte(s) from {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} - Extracts all files from 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 [target-directory]", applicationName)
        Console.WriteLine("Example: {0} C:\archive.zip ", applicationName)
        Console.WriteLine("Example: {0} C:\archive.zip C:\data", applicationName)
    End Sub
End Class
