//  
//   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.
// 

using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

using Rebex.IO;
using Rebex.IO.Compression;

namespace Rebex.Samples
{
    /// <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>
        static void Main(string[] args)
        {
            // set the trial license key
            Rebex.Licensing.Key = Rebex.Samples.TrialKey.Key;


            if (args.Length == 0)
            {
                ShowHelp();
                return;
            }

            try
            {
                // parse the command line
                Arguments config = new Arguments(args);

                // create a target directory if needed
                if (!Directory.Exists(config.TargetPath))
                    Directory.CreateDirectory(config.TargetPath);

                // 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 (ZipArchive archive = new ZipArchive(config.ZipArchivePath, ArchiveOpenMode.Open))
                {
                    ArchiveOperationResult result = 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
                    );
                }
            }
            catch (SampleException e)
            {
                // display an error reported by arguments parser class
                Console.WriteLine("Error: {0}", e.Message);

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

        private static void ShowHelp()
        {
            string applicationName = 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);
        }
    }
}
