Exemple #1
0
        /// <summary>
        /// Console starting point to compress or decompress file.
        /// Determines chunk size and memory buffer to limit RAM usage.
        /// </summary>
        /// <param name="args">Must comply GZipTest.exe compress/decompress [initial file name][restul file name]</param>
        /// <returns>Returns 0 if success or 1 if error happened</returns>
        static int Main(string[] args)
        {
            int chunkSize     = 1024 * 1024;
            int memBufferSize = 3;      // max chunks in memory buffer per each buffer

            if (ParameterValidator.Validate(args) == false)
            {
                return(1);
            }
            string fileToProcess = args[1];
            string fileResult    = args[2];

            try
            {
                long length = new FileInfo(fileToProcess).Length;
                if (length == 0)
                {
                    File.Create(fileResult);
                    return(0);
                }

                if (length < chunkSize)                                 // File is smaller than chunk size
                {
                    chunkSize = (int)length;                            // file size would be used instead
                }
                IGzipOperations job = null;
                if (args[0] == "compress")
                {
                    job = new Compressor(chunkSize);
                }
                else if (args[0] == "decompress")
                {
                    job = new Decompressor();
                }

                var actionManager = new ActionManager(fileToProcess, fileResult, memBufferSize, job);
                var threadWorker  = new ThreadWorker(actionManager);
                threadWorker.StartThreads();
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR: " + ex.Message);
                return(1);
            }
            return(0);
        }