Пример #1
0
        private static int Main(string[] args)
        {
            var cmdLineParams = new CmdLineParams();

            try
            {
                using (var parser = new CommandLineParser(cmdLineParams))
                {
                    parser.Parse();
                    if (parser.HasErrors)
                    {
                        Console.WriteLine(parser.UsageInfo.GetErrorsAsString(78));
                        return(-1);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.ToString());
                return(-1);
            }
            try
            {
                if (cmdLineParams.ExtractFile)
                {
                    bool result = ZipFileExtractor.ExtractFilesFromZipFiles(cmdLineParams);
                    if (result)
                    {
                        return(0);
                    }
                    else
                    {
                        return(-1);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error Occured {0}", ex.Message);
            }
            return(0);
        }
Пример #2
0
        /// <summary>
        /// Extracts the list of files given in csv from zip files to a destination folder
        /// </summary>
        /// <param name="cmdLineParams"></param>
        public static bool ExtractFilesFromZipFiles(CmdLineParams cmdLineParams)
        {
            string fileContent = string.Empty;

            string destinationFilePath = cmdLineParams.DestinationPath;

            if (string.IsNullOrEmpty(destinationFilePath))
            {
                Console.WriteLine("Destination Path not passed.Getting from the config file");
                destinationFilePath = ConfigurationController.Default.DestinationPath;
                if (!Directory.Exists(destinationFilePath))
                {
                    Directory.CreateDirectory(destinationFilePath);
                }
                Console.WriteLine("New Directory {0} Created", destinationFilePath);
            }
            if (string.IsNullOrEmpty(cmdLineParams.ListOfFiles))
            {
                fileContent = File.ReadAllText(ConfigurationController.Default.FileListPath);
            }
            else
            {
                fileContent = File.ReadAllText(cmdLineParams.ListOfFiles);
            }
            if (string.IsNullOrEmpty(fileContent))
            {
                Console.WriteLine("File Content is empty...Press Any Key to Continue");
                Console.ReadKey();
                return(false);
            }
            string[]      listOfFiles        = fileContent.Split(',');
            List <string> filesToBeExtracted = new List <string>();

            filesToBeExtracted.AddRange(listOfFiles);

            string ZipFilePath = string.Empty;

            if (string.IsNullOrEmpty(cmdLineParams.ZipFilePath))
            {
                ZipFilePath = ConfigurationController.Default.ZipPath;
            }
            else
            {
                ZipFilePath = cmdLineParams.ZipFilePath;
            }
            string[] zipFiles  = Directory.GetFiles(cmdLineParams.ZipFilePath, "*.zip", SearchOption.AllDirectories);
            var      stopWatch = Stopwatch.StartNew();

            if (zipFiles != null)
            {
                foreach (string zipFile in zipFiles)
                {
                    using (var archive = ZipFile.OpenRead(zipFile))
                    {
                        foreach (var entry in archive.Entries)
                        {
                            if (!string.IsNullOrEmpty(entry.Name))
                            {
                                if (IsFilePresent(filesToBeExtracted, entry.Name))
                                {
                                    if (!Directory.Exists(destinationFilePath))
                                    {
                                        Console.WriteLine("{0} does not exists... Creating {0}", destinationFilePath);
                                        Directory.CreateDirectory(destinationFilePath);
                                    }
                                    Console.Write("Extracting file {0} to {1}", entry.Name, destinationFilePath);
                                    stopWatch.Restart();
                                    entry.ExtractToFile(Path.Combine(destinationFilePath, entry.Name), true);
                                    stopWatch.Stop();
                                    Console.WriteLine("...Done in :{0}", stopWatch.Elapsed);
                                    filesToBeExtracted.Remove(entry.Name);
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                Console.Error.Write("Not Zip Files found to be extracted");
                return(false);
            }
            Console.WriteLine("Extraction Successful\nPress Any key to exit");
            Console.ReadKey();
            return(true);
        }