Example #1
0
        static void Main(string[] args)
        {
            int capaticy = 50;

            AppDomain.CurrentDomain.UnhandledException += ExceptionHandler;

            Console.CancelKeyPress += (sender, eventArgs) =>
            {
                Console.WriteLine();
                _isCancelled = true;
                if (_backgroundThread != null)
                    _backgroundThread.Abort();
            };

            if (ValidateArguments(args))
            {
                bool compress = string.Equals(args[0], COMPRESS);
                string initPath = args[1];
                string archPath = args[2];

                IArchivator compressor = new CompressEngine(new GZipCompress(), capaticy);
                IArchivator decompressor = new DecompressEngine(new GZipDecompress(), capaticy);

                _ss = new Thread(() => ScreenSaver()) { IsBackground = true, Name = "ScreenSaver" };

                Console.WriteLine("Operation Started");
                _ss.Start();

                if (string.Equals(args[0], COMPRESS))
                {
                    _backgroundThread = new Thread(() => Compress(initPath, archPath, compressor, ARCHIVE_SIZE)) { IsBackground = true, Name = "MainProgram" };
                    _backgroundThread.Start();
                }
                else if (string.Equals(args[0], DECOMPRESS))
                {
                    _backgroundThread = new Thread(() => DeCompress(initPath, archPath, decompressor, ARCHIVE_SIZE)) { IsBackground = true };
                    _backgroundThread.Start();
                }

                _mainEvent.WaitOne();

                Console.WriteLine();

                if (_isCompleted)
                    Console.WriteLine("0");
                else if (_isCancelled)
                    Console.WriteLine("Cancelled");
                else if (_isError)
                    Console.WriteLine("1");

                Console.WriteLine();
                Console.WriteLine("Press any key");
                Console.ReadLine();
            }
        }
        public void Decompress_Find_Segment()
        {
            var engine = new DecompressEngine(new GZipDecompress(), 100);
            byte[] buffer = new byte[60];
            Array.Copy(Helper.GZIP_HEADER_BYTES, 0, buffer, 10, Helper.GZIP_HEADER_BYTES.Length);
            Array.Copy(Helper.GZIP_HEADER_BYTES, 0, buffer, 50, Helper.GZIP_HEADER_BYTES.Length);

            byte[] segment = null;
            Assert.IsTrue(engine.FindSegmentStartAndLastIndex(buffer, ref segment) == true);
            Assert.IsTrue(segment.Length == 40);

            //LastPeace
            byte[] buffer2 = new byte[60];
            Array.Copy(Helper.GZIP_HEADER_BYTES, 0, buffer2, 0, Helper.GZIP_HEADER_BYTES.Length);
                        
            Assert.IsTrue(engine.FindSegmentStartAndLastIndex(buffer2, ref segment) == false);
            Assert.IsTrue(segment.Length == buffer2.Length);
        }
        private void CompressDeCompress(long bufferSize)
        {
            byte[] archivedFile;

            string hash;

            using (MD5 md5Hash = MD5.Create())
            {
                hash = Helper.GetMd5Hash(md5Hash, file);
            }

            var engine = new CompressEngine(new GZipCompress(), capacity);
            using (MemoryStream original = new MemoryStream(file))
            {
                using (MemoryStream archive = new MemoryStream())
                {
                    engine.DoAction(original, archive, bufferSize);
                    archivedFile = archive.ToArray();
                }
            }

            byte[] dearchivedFile = null;
            using (MemoryStream archive = new MemoryStream(archivedFile))
            {
                using (MemoryStream original = new MemoryStream())
                {
                    DecompressEngine engine2 = new DecompressEngine(new GZipDecompress(), capacity);
                    engine2.DoAction(archive, original, bufferSize);
                    dearchivedFile = original.ToArray();
                }
            }

            using (MD5 md5Hash = MD5.Create())
            {
                Assert.IsTrue(Helper.VerifyMd5Hash(md5Hash, dearchivedFile, hash));
            }
        }