Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.IO.Compression;
- using System.Linq;
- using System.Threading;
- namespace MultithreadingSample
- {
- class FileCompressor
- {
- private const int BufferSize = 1 << 16;
- private readonly BlockingQueue<byte[]> _readingQueue = new BlockingQueue<byte[]>(8);
- //private readonly BlockingQueue<byte[]> _writingQueue = new BlockingQueue<byte[]>(8);
- public void Compress(string input, string output = null)
- {
- if (string.IsNullOrWhiteSpace(output))
- {
- output = input + ".gz";
- }
- try
- {
- using (var queue = new BlockingQueue<byte[]>(8))
- {
- if (!File.Exists(input)) return;
- Thread reader = new Thread(() =>
- {
- using (var inputStream = new FileStream(input, FileMode.Open))
- {
- var buffer = new byte[BufferSize];
- while (inputStream.Read(buffer, 0, BufferSize) != 0)
- {
- _readingQueue.Enqueue(buffer.ToArray());
- }
- }
- })
- {
- Name = "Reader"
- };
- reader.Start();
- for (int i = 0; i < 7; i++)
- {
- Thread compressor = new Thread(() =>
- {
- using (var outputStream = new FileStream(output, FileMode.Create))
- using (var gZipStream = new GZipStream(outputStream, CompressionMode.Compress))
- {
- while (true)
- {
- gZipStream.Write(_readingQueue.Dequeue(), 0, BufferSize);
- }
- }
- })
- {
- Name = "Compressor" + i
- };
- compressor.Start();
- }
- }
- }
- catch (Exception e)
- {
- Console.WriteLine(e);
- throw;
- }
- }
- public void Decompress(string input)
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement