Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.48 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.IO.Compression;
  4. using System.Linq;
  5. using System.Threading;
  6.  
  7. namespace MultithreadingSample
  8. {
  9.     class FileCompressor
  10.     {
  11.         private const int BufferSize = 1 << 16;
  12.         private readonly BlockingQueue<byte[]> _readingQueue = new BlockingQueue<byte[]>(8);
  13.         //private readonly BlockingQueue<byte[]> _writingQueue = new BlockingQueue<byte[]>(8);
  14.  
  15.         public void Compress(string input, string output = null)
  16.         {
  17.             if (string.IsNullOrWhiteSpace(output))
  18.             {
  19.                 output = input + ".gz";
  20.             }
  21.  
  22.             try
  23.             {
  24.                 using (var queue = new BlockingQueue<byte[]>(8))
  25.                 {
  26.                     if (!File.Exists(input)) return;
  27.  
  28.                     Thread reader = new Thread(() =>
  29.                     {
  30.                         using (var inputStream = new FileStream(input, FileMode.Open))
  31.                         {
  32.                             var buffer = new byte[BufferSize];
  33.                             while (inputStream.Read(buffer, 0, BufferSize) != 0)
  34.                             {
  35.                                 _readingQueue.Enqueue(buffer.ToArray());
  36.                             }
  37.                         }
  38.                     })
  39.                     {
  40.                         Name = "Reader"
  41.                     };
  42.                     reader.Start();
  43.  
  44.                     for (int i = 0; i < 7; i++)
  45.                     {
  46.                         Thread compressor = new Thread(() =>
  47.                         {
  48.                             using (var outputStream = new FileStream(output, FileMode.Create))
  49.                             using (var gZipStream = new GZipStream(outputStream, CompressionMode.Compress))
  50.                             {
  51.                                 while (true)
  52.                                 {
  53.                                     gZipStream.Write(_readingQueue.Dequeue(), 0, BufferSize);
  54.                                 }
  55.                             }
  56.                         })
  57.                         {
  58.                             Name = "Compressor" + i
  59.                         };
  60.                         compressor.Start();
  61.                     }
  62.                 }
  63.             }
  64.             catch (Exception e)
  65.             {
  66.                 Console.WriteLine(e);
  67.                 throw;
  68.             }
  69.         }
  70.  
  71.         public void Decompress(string input)
  72.         {
  73.  
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement