Example #1
0
        static int MainInside(string[] args)
        {
            var dllCatalog = new DirectoryCatalog(AssemblyDirectory, "Zip*Strip*.dll");
            var exeCatalog = new DirectoryCatalog(AssemblyDirectory, "Zip*Strip*.exe");
            var catalog = new AggregateCatalog(dllCatalog, exeCatalog);
            var mef = new CompositionContainer(catalog);

            var operationArguments = mef.GetExportedValues<IZipOperationArgument>();
            var operations = new List<IZipOperation>()
            {
                new CleanDirectoryAttributesOperation(),
            };

            string file = null;
            string output = null;
            var options = new OptionSet()
            {
                { "file=", val => file = val },
                { "output=", val => output = val },
            };

            foreach (var arg in operationArguments)
            {
                arg.Configure(options, operations);
            }

            var unparsedArguments = options.Parse(args);
            if (unparsedArguments.Count == 0 && file == null)
            {
                Console.Error.WriteLine("No argument found for file name");
                return 1;
            }
            if (file == null)
            {
                file = unparsedArguments.FirstOrDefault(arg => !(arg.StartsWith("-") | arg.StartsWith("/")));
                if (file != null)
                {
                    unparsedArguments.Remove(file);
                }
            }
            if (file != null)
            {
                try
                {
                    file = Path.GetFullPath(file);
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine("Could not use '{0}' as a file name: {1}", file, ex.Message);
                    throw;
                }

                Console.WriteLine("File: {0}", file);
            }
            if (output != null)
            {
                try
                {
                    output = Path.GetFullPath(output);
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine("Could not use '{0}' as a output name: {1}", output, ex.Message);
                    throw;
                }

                Console.WriteLine("Destination: {0}", output);
            }
            else
            {
                output = file;
            }
            if (unparsedArguments.Count > 0)
            {
                foreach (var arg in unparsedArguments)
                {
                    Console.Error.WriteLine("Unknown option: {0}", arg);
                }
                return 1;
            }

            if (!File.Exists(file))
            {
                throw new FileNotFoundException("Could not find ZIP file", file);
            }

            foreach (var op in operations)
            {
                Console.WriteLine("Operation: {0}", op);
            }

            using (var zip = new ZipFile(file))
            {
                var op = new ZipOperator(operations);

                bool changed;

                Console.WriteLine("Analyzing");
                op.Run(zip, OutputHelper.ProgressBarCallback, out changed);

                if (!changed)
                {
                    Console.WriteLine("No changes");
                    return 0;
                }

                Console.WriteLine("Saving to {0}", output);
                zip.RegisterIntegerSaveProgress(OutputHelper.ProgressBarCallback);

                zip.Save(output);
                return 0;
            }
        }