示例#1
0
 public static void RunCompression(string srcFile, string dstFile, int degreeOfParallelism)
 {
     using (var streamSource = new FileStream(srcFile, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None, 0x1000, useAsync: true))
         using (var streamDestination = new FileStream(dstFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, 0x1000, useAsync: true))
         {
             CompressionAndEncryptDataFlow.CompressAndEncrypt(streamSource, streamDestination, degreeOfParallelism).Wait();
             streamDestination.Close();
         }
 }
示例#2
0
        public static void CompressAndEncrypt(string srcFile, string dstFile, string rstFile)
        {
            try
            {
                var sw = System.Diagnostics.Stopwatch.StartNew();

                Console.WriteLine("CompressAndEncrypt ...");
                using (var streamSource = new FileStream(srcFile, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None, 0x1000, useAsync: true))
                    using (var streamDestination = new FileStream(dstFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, 0x1000, useAsync: true))
                    {
                        CompressionAndEncryptDataFlow.CompressAndEncrypt(streamSource, streamDestination).Wait();
                        streamDestination.Close();
                    }
                Console.WriteLine($"Done in {sw.ElapsedMilliseconds}");

                Console.WriteLine("Press Enter to continue");
                Console.ReadLine();
                sw.Restart();

                Console.WriteLine("DecryptAndDecompress ...");
                using (var streamSource = new FileStream(dstFile, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None, 0x1000, useAsync: true))
                    using (var streamDestination = new FileStream(rstFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, 0x1000, useAsync: true))
                        CompressionAndEncryptDataFlow.DecryptAndDecompress(streamSource, streamDestination).Wait();
                Console.WriteLine($"Done in {sw.ElapsedMilliseconds}");

                Console.WriteLine("Press Enter to continue");
                Console.ReadLine();

                Console.WriteLine("Verification ...");
                using (var f1 = System.IO.File.OpenRead(srcFile))
                    using (var f2 = System.IO.File.OpenRead(rstFile))
                    {
                        bool ok = false;
                        if (f1.Length == f2.Length)
                        {
                            ok = true;
                            int       count;
                            const int size = 0x1000000;

                            var buffer  = new byte[size];
                            var buffer2 = new byte[size];

                            while ((count = f1.Read(buffer, 0, buffer.Length)) > 0 && ok == true)
                            {
                                f2.Read(buffer2, 0, count);
                                ok = buffer2.SequenceEqual(buffer);
                                if (!ok)
                                {
                                    break;
                                }
                            }
                        }
                        Console.WriteLine($"Restored file isCorrect = {ok}");
                    }

                Console.WriteLine("Press Enter to continue");
                Console.ReadLine();
            }
            catch (AggregateException ex)
            {
                var q = new Queue <Exception>(new[] { ex });
                while (q.Count > 0)
                {
                    var e = q.Dequeue();
                    Console.WriteLine($"\t{e.Message}");
                    if (e is AggregateException)
                    {
                        foreach (var x in (e as AggregateException).InnerExceptions)
                        {
                            q.Enqueue(x);
                        }
                    }
                    else
                    {
                        if (e.InnerException != null)
                        {
                            q.Enqueue(e.InnerException);
                        }
                    }
                }
            }
        }