'  
'   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 Rebex.IO
Imports Rebex.IO.Compression

''' <summary>
''' Command line utility that displays information about a ZIP archive:
''' number of files, file names, compressed/uncompressed lengths and last modification dates.
''' </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


        ' display the header
        Dim applicationName As String = AppDomain.CurrentDomain.FriendlyName
        Console.WriteLine("==============================================================")
        Console.WriteLine(" Rebex {0} - displays information about a ZIP archive", applicationName)
        Console.WriteLine("==============================================================")

        ' check whether a path to a ZIP archive was specified
        If args.Length <> 1 Then
            ' if not, display help and exit
            Console.WriteLine("Syntax: {0} zip-file-path", applicationName)
            Console.WriteLine("Example: {0} C:\archive.zip", applicationName)
            Return
        End If

        ' get ZIP archive path
        Dim zipArchivePath As String = args(0)

        Try
            ' open the ZIP archive (in read-only mode)
            Using zip As New ZipArchive(zipArchivePath, ArchiveOpenMode.Open, ArchiveAccessMode.Read)
                ' retrieve all files in the archive => use recursive GetItems
                Dim files As ZipItemCollection = zip.GetItems("*", TraversalMode.Recursive, ArchiveItemTypes.Files)

                If files.Count = 0 Then
                    Console.WriteLine("There are no files within the archive.")
                Else
                    ' sort the files by path (directories first)
                    files.Sort()

                    ' total uncompressed length of the all files within the archive
                    Dim totalFilesLength As Long = 0

                    ' display information about each file
                    Console.WriteLine("Last modified       Uncomp.len   Comp.len  Ratio  Path")
                    Console.WriteLine("--------------------------------------------------------------")
                    For Each file As ZipItem In files
                        ' update total uncompressed length
                        totalFilesLength += file.Length

                        ' compute compress ratio
                        Dim compressRatio As Double = 0
                        If file.Length > 0 Then
                            compressRatio = (1 - CDbl(file.CompressedLength) / CDbl(file.Length)) * 100
                        End If

                        Dim lastWriteTime As String = String.Empty
                        If file.LastWriteTime.HasValue Then
                            lastWriteTime = file.LastWriteTime.Value.ToString("u").TrimEnd("Z"c)
                        End If

                        ' display item info
                        Console.WriteLine("{0,19} {1,10} {2,10} {3,6:F2}% {4}", lastWriteTime, file.Length, file.CompressedLength, compressRatio, file.Path)
                    Next
                    Console.WriteLine("--------------------------------------------------------------")

                    ' display count of files
                    Console.WriteLine("Total number of files: {0}", files.Count)

                    ' read length of the ZIP archive
                    Dim archiveLength As Long = New FileInfo(zipArchivePath).Length

                    ' displays some more info
                    Console.WriteLine("Total archive length (compressed): {0}", archiveLength)
                    Console.WriteLine("Total length of uncompressed files: {0}", totalFilesLength)

                    If totalFilesLength > 0 Then
                        ' compute compress ratio of the whole archive
                        Dim compressRatio As Double = (1 - CDbl(archiveLength) / CDbl(totalFilesLength)) * 100
                        Console.WriteLine("Archive compress ratio: {0:F2}%", compressRatio)
                    End If
                End If

                ' display ZIP archive comment if it is not empty
                If zip.Comment <> String.Empty Then
                    Console.WriteLine("Archive comment: {0}", zip.Comment)
                End If
            End Using
        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 occured: {0}", e)
        End Try
    End Sub
End Class
