Пример #1
0
        public int Compress(string sourceFileName, string destFileName, CustomCancellationToken cancelToken)
        {
            var sourceStream = File.OpenRead(sourceFileName);
            var destStream   = new FileStream(destFileName, FileMode.OpenOrCreate, FileAccess.Write);
            var gzipStream   = new GZipStream(destStream, CompressionMode.Compress);

            try
            {
                WriteToStreamMultiThread(sourceStream, gzipStream, cancelToken);
            }
            catch (OperationCanceledException cancelEx)
            {
                InnerException = cancelEx;
                jobResult      = 1;
            }
            catch (Exception ex)
            {
                InnerException = ex;
                jobResult      = 1;
            }
            finally
            {
                sourceStream.Close();
                gzipStream.Close();
                destStream.Close();
                waitHandle.Set();
                if (OnCompressionComplete != null)
                {
                    OnCompressionComplete(this, new CompressionCompleteEventArgs(jobResult, 0, InnerException));
                }
            }
            return(jobResult);
        }
Пример #2
0
 internal GzipAsyncResult(string sourceFileName, string destFileName, CustomCancellationToken cancelToken, AsyncCallback callback, object state) :
     base(callback, state)
 {
     this.SourceFileName = sourceFileName;
     this.DestFileName   = destFileName;
     this.CancelToken    = cancelToken;
 }
Пример #3
0
 public void Start(Stream fromS, Stream toS, CustomCancellationToken cancelToken)
 {
     InnerThread.Start(new GZipThreadArgs()
     {
         SourceStream = fromS, DestStream = toS, cancelToken = cancelToken
     });
 }
Пример #4
0
 public static CustomCancellationToken GetToken()
 {
     if (token == null)
     {
         token = new CustomCancellationToken();
     }
     return(token);
 }
Пример #5
0
        static int Main(string[] args)
        {
            waitHandle = new AutoResetEvent(false);
            GZipCompressor compressor = new GZipCompressor(waitHandle);

            token = CustomCancellationTokenSource.GetToken();
            compressor.OnCompressionComplete        += compressor_OnCompressionComplete;
            compressor.OnCompressionProgressChanged += compressor_OnProgressChanged;
            Console.CancelKeyPress += Console_CancelKeyPress;
            jobResult = 0;
            try
            {
                SourceFileName = args[1];
                EndFileName    = args[2];
                switch (args[0].ToLower())
                {
                case "compress":
                    compressor.Compress(SourceFileName, EndFileName);
                    break;

                case "decompress":
                    compressor.Decompress(SourceFileName, EndFileName);
                    break;

                default:
                    throw new NotSupportedException();
                }
            }
            catch (NotSupportedException)
            {
                Console.WriteLine("Unknown method!");
                ShowHelp();
                waitHandle.Set();
                jobResult = 1;
            }
            catch (IndexOutOfRangeException)
            {
                Console.WriteLine("Not enought parameters!");
                ShowHelp();
                waitHandle.Set();
                jobResult = 1;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                ShowHelp();
                waitHandle.Set();
                jobResult = 1;
            }
            waitHandle.WaitOne();
            return(jobResult);
        }
Пример #6
0
        /*
         * void RunCompression(object state)
         * {
         *  GzipAsyncResult asyncResult = state as GzipAsyncResult;
         *  if (asyncResult == null)
         *      throw new InvalidDataException("state");
         *  try
         *  {
         *      asyncResult.Result = Compress(asyncResult.SourceFileName, asyncResult.DestFileName, asyncResult.CancelToken);
         *  }
         *  finally
         *  {
         *      asyncResult.Complete();
         *  }
         * }
         *
         *
         * public IAsyncResult BeginCompress(string sourceFile, string destFile, CustomCancellationToken cancelToken, AsyncCallback callback, object asyncState)
         * {
         *  IAsyncResult result = new GzipAsyncResult(sourceFile, destFile, cancelToken, callback, asyncState);
         *  var waitCallback = new WaitCallback(RunCompression);
         *  ThreadPool.QueueUserWorkItem(waitCallback, result);
         *  return result;
         * }
         *
         * public int EndCompress(IAsyncResult result)
         * {
         *  if (null == result)
         *      throw new ArgumentNullException("result");
         *  GzipAsyncResult asyncResult = result as GzipAsyncResult;
         *  if (null == asyncResult)
         *      throw new ArgumentException("", "result");
         *  asyncResult.Validate();
         *  asyncResult.AsyncWaitHandle.WaitOne();
         *  int res = asyncResult.Result;
         *  asyncResult.Dispose();
         *  return res;
         * }
         *
         *
         * void RunDecompression(object state)
         * {
         *  GzipAsyncResult asyncResult = state as GzipAsyncResult;
         *  if (asyncResult == null)
         *      throw new InvalidDataException("state");
         *  try
         *  {
         *      asyncResult.Result = Decompress(asyncResult.SourceFileName, asyncResult.DestFileName, asyncResult.CancelToken);
         *  }
         *  finally
         *  {
         *      asyncResult.Complete();
         *  }
         * }
         *
         * public IAsyncResult BeginDecompress(string sourceFile, string destFile, CustomCancellationToken cancelToken, AsyncCallback callback, object asyncState)
         * {
         *  IAsyncResult result = new GzipAsyncResult(sourceFile, destFile, cancelToken, callback, asyncState);
         *  var waitCallback = new WaitCallback(RunDecompression);
         *  ThreadPool.QueueUserWorkItem(waitCallback, result);
         *  return result;
         * }
         *
         * public int EndDecompress(IAsyncResult result)
         * {
         *  if (null == result)
         *      throw new ArgumentNullException("result");
         *  GzipAsyncResult asyncResult = result as GzipAsyncResult;
         *  if (null == asyncResult)
         *      throw new ArgumentException("", "result");
         *  asyncResult.Validate();
         *  asyncResult.AsyncWaitHandle.WaitOne();
         *  int res = asyncResult.Result;
         *  asyncResult.Dispose();
         *  return res;
         * }
         */
        void WriteToStream(Stream from, Stream to, CustomCancellationToken cancelToken)
        {
            byte[] buffer         = new byte[Properties.Settings.Default.BufferSize];
            int    readBytesCount = 0;

            while ((readBytesCount = from.Read(buffer, 0, buffer.Length)) != 0)
            {
                if (!cancelToken.IsCancelled)
                {
                    to.Write(buffer, 0, readBytesCount);
                    if (OnProgressChanged != null)
                    {
                        OnProgressChanged(this, new CompressionProgressChangedEventArgs(from.Length, readBytesCount));
                    }
                }
                else
                {
                    throw new OperationCanceledException("Operation was cancelled");
                }
            }
        }
Пример #7
0
        void WriteToStream(Stream from, Stream to, CustomCancellationToken cancelToken, int readOffset = 0)
        {
            byte[] buffer       = new byte[BufferSize];
            long   readBytes    = 0;
            long   readBytesSum = 0;

            while ((readBytes = from.Read(buffer, readOffset, buffer.Length)) != 0)
            {
                if (!cancelToken.IsCancelled)
                {
                    to.Write(buffer, 0, (int)readBytes);
                    readBytesSum += readBytes;
                    if (OnCompressionProgressChanged != null)
                    {
                        OnCompressionProgressChanged(this, new CompressionProgressChangedEventArgs(from.Length, readBytesSum));
                    }
                }
                else
                {
                    throw new OperationCanceledException("Operation was cancelled");
                }
            }
        }
Пример #8
0
        void WriteToStreamMultiThread(Stream from, Stream to, CustomCancellationToken cancelToken)
        {
            GZipThreadQueueProvider.ResetAll();
            WaitHandle[] handles = new WaitHandle[ProcessorsCount];
            for (int i = 0; i < ProcessorsCount; i++)
            {
                GZipThread thr = new GZipThread(BufferSize, _readSync, _writeSync);
                thr.OnThreadWorkComplete      += thr_OnThreadWorkComplete;
                thr.OnThreadIterationComplete += thr_OnThreadIterationComplete;
                threads[i] = thr;
                handles[i] = thr.waitHandle;
            }

            foreach (var thr in threads)
            {
                thr.Start(from, to, cancelToken);
            }

            foreach (var thr in threads)
            {
                thr.waitHandle.WaitOne();
            }
        }
Пример #9
0
        public int Decompress(string sourceFile, string destFile, CustomCancellationToken cancelToken)
        {
            int operationResult = 0;

            using (var sourceFileStream = File.OpenRead(sourceFile))
            {
                using (var destFileStream = new FileStream(destFile, FileMode.Create, FileAccess.Write))
                {
                    using (var gzipStream = new GZipStream(sourceFileStream, CompressionMode.Decompress, false))
                    {
                        try
                        {
                            WriteToStream(gzipStream, destFileStream, cancelToken);
                            operationResult = 0;
                        }
                        catch (OperationCanceledException cancelledEx)
                        {
                            InnerException  = cancelledEx;
                            operationResult = 1;
                        }
                        catch (Exception ex)
                        {
                            InnerException  = ex;
                            operationResult = 1;
                        }
                    }
                }
            }
            if (operationResult != 0)
            {
                if (File.Exists(destFile))
                {
                    File.Delete(destFile);
                }
            }
            return(operationResult);
        }