public OutputGenerator()
        {
            generators         = new Dictionary <Type, IFileGenerator>();
            generatingContexts = new Dictionary <string, IFileGeneratingContext>();
            var fileWriter    = new DirectFileWriter();
            var csharpContext = new CSharpFileGeneratingContext(fileWriter);

            generatingContexts.Add(csharpContext.FileType, csharpContext);
        }
Beispiel #2
0
        static int Main(string[] args)
        {
            // Get a reference to the language file
            ResourceManager text = Language.ResourceManager;

            // Parse the command line options
            _options = new Options();

            if (!Parser.Default.ParseArguments(args, _options))
            {
                Console.WriteLine(_options.GetUsage());

                return(255);
            }

            // Make sure an archive file is given
            if (_options.ArchiveFile == null)
            {
                Console.WriteLine(_options.GetUsage());

                return(255);
            }

            // Display a banner
            if (!_options.Silent)
            {
                Console.WriteLine("Akeeba eXtract CLI v. " + Assembly.GetCallingAssembly().GetName().Version);
                Console.WriteLine($"Copyright (c)2006-{DateTime.Now.Year} Nicholas K. Dionysopoulos / Akeeba Ltd");
                Console.WriteLine("-------------------------------------------------------------------------------");
                Console.WriteLine("This is free software. You may redistribute copies of it under the terms of");
                Console.WriteLine("the MIT License <http://www.opensource.org/licenses/mit-license.php>.");
                Console.WriteLine("");
            }

            // If no output directory is given we assume the current workign directory
            if (_options.TargetFolder == null)
            {
                _options.TargetFolder = Directory.GetCurrentDirectory();
            }

            try
            {
                // Make sure the file exists
                if (!File.Exists(_options.ArchiveFile))
                {
                    throw new FileNotFoundException(String.Format(text.GetString("ERR_NOT_FOUND"), _options.ArchiveFile));
                }

                // Make sure the file can be read
                try
                {
                    using (FileStream stream = File.Open(_options.ArchiveFile, FileMode.Open, FileAccess.Read))
                    {
                    }
                }
                catch
                {
                    throw new FileNotFoundException(String.Format(text.GetString("ERR_CANNOT_READ"), _options.ArchiveFile));
                }

                // Try to extract
                using (Unarchiver.Unarchiver extractor = Unarchiver.Unarchiver.CreateForFile(_options.ArchiveFile, _options.Password))
                {
                    // Attach event subscribers
                    extractor.ProgressEvent += OnProgressHandler;
                    extractor.EntityEvent   += onEntityHandler;

                    // Create a cancelation token (it's required by the unarchiver)
                    CancellationTokenSource cts = new CancellationTokenSource();
                    var token = cts.Token;

                    Task t = Task.Factory.StartNew(
                        () =>
                    {
                        if (extractor == null)
                        {
                            throw new Exception("Internal state consistency violation: extractor object is null");
                        }

                        // Get the appropriate writer
                        IDataWriter writer = new NullWriter();

                        if (!_options.Test)
                        {
                            writer = new DirectFileWriter(_options.TargetFolder);
                        }

                        // Test the extraction
                        extractor.Extract(token, writer);
                    }, token,
                        TaskCreationOptions.None,
                        TaskScheduler.Default
                        );

                    t.Wait(token);
                }
            }
            catch (Exception e)
            {
                Exception targetException = (e.InnerException == null) ? e : e.InnerException;

                if (!_options.Silent || _options.Verbose)
                {
                    Console.WriteLine("");
                    Console.WriteLine(text.GetString("ERR_HEADER"));
                    Console.WriteLine("");
                    Console.WriteLine(targetException.Message);
                }

                if (_options.Verbose)
                {
                    Console.WriteLine("");
                    Console.WriteLine("Stack trace:");
                    Console.WriteLine("");
                    Console.WriteLine(targetException.StackTrace);
                }

                return(250);
            }

            return(0);
        }
Beispiel #3
0
        /// <summary>
        /// Extract the backup archive to the specified filesystem path using direct file writes
        /// </summary>
        /// <param name="token">A CancellationToken used by the caller to cancel the execution before it's complete.</param>
        /// <param name="destinationPath">The path where the archive will be extracted to</param>
        public void Extract(CancellationToken token, string destinationPath)
        {
            IDataWriter myDataWriter = new DirectFileWriter(destinationPath);

            Extract(token, myDataWriter);
        }
Beispiel #4
0
        /// <summary>
        /// Start the archive extraction asynchronously.
        /// </summary>
        /// <returns></returns>
        private async Task StartExtractionAsync()
        {
            string archiveFile       = _gateway.GetBackupArchivePath();
            string outputDirectory   = _gateway.GetOutputFolderPath();
            string password          = _gateway.GetPassword();
            bool   ignoreWriteErrors = _gateway.GetIgnoreFileWriteErrors();
            bool   dryRun            = _gateway.GetDryRun();

            _tokenSource = new CancellationTokenSource();
            CancellationToken token = _tokenSource.Token;

            try
            {
                using (Unarchiver extractor = Unarchiver.CreateForFile(archiveFile, password))
                {
                    // Wire events
                    _totalArchiveSize = 0;

                    extractor.ArchiveInformationEvent += onArchiveInformationHandler;
                    extractor.ProgressEvent           += OnProgressHandler;
                    extractor.EntityEvent             += onEntityHandler;

                    Task t = Task.Factory.StartNew(
                        () =>
                    {
                        if (extractor == null)
                        {
                            throw new Exception("Internal state consistency violation: extractor object is null");
                        }

                        // Get the appropriate writer
                        IDataWriter writer = new NullWriter();

                        if (!dryRun)
                        {
                            writer = new DirectFileWriter(outputDirectory);

                            if (ignoreWriteErrors)
                            {
                                // TODO Use a different file writer when we are asked to ignore write errorss
                                writer = new DirectFileWriter(outputDirectory);
                            }
                        }

                        // Test the extraction
                        extractor.Extract(token, writer);
                    }, token,
                        TaskCreationOptions.None,
                        TaskScheduler.Default
                        );

                    await t;
                }
            }
            catch (Exception e)
            {
                Exception targetException = (e.InnerException == null) ? e : e.InnerException;

                _gateway.SetTaskbarProgressState(TaskBarProgress.TaskbarStates.Error);

                // Show error message
                _gateway.showErrorMessage(_languageResource.GetString("LBL_ERROR_CAPTION"), targetException.Message);
            }

            _gateway.SetExtractionOptionsState(true);
            ResetProgress();
        }