示例#1
0
 public override void Read()
 {
     try
     {
         using (FileStream sourceStream = new FileStream(SourceFile, FileMode.Open, FileAccess.Read))
         {
             byte[] blockSize;
             while (sourceStream.Position < sourceStream.Length)
             {
                 blockSize = new byte[Size];
                 sourceStream.Read(blockSize, 0, Size);                         //информация о размере сжатого кусочка
                 int readID = GetFromByteSize(blockSize);
                 blockSize = new byte[Size];
                 int readLenght = sourceStream.Read(blockSize, 0, Size);
                 if (readLenght != Size)
                 {
                     throw new CompressionException("File is corrupted");
                 }
                 byte[] sliceFile = new byte[GetFromByteSize(blockSize)];
                 int    read      = sourceStream.Read(sliceFile, 0, sliceFile.Length);
                 if (read == 0)
                 {
                     throw new CompressionException("File is corrupted");
                 }
                 GzipSlice gz = new GzipSlice(readID, sliceFile);
                 readerQueue.Enqueue(gz);
             }
             readerQueue.Close();
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.ToString());
     }
 }
示例#2
0
 public void Enqueue(GzipSlice slice)
 {
     lock (locker)
     {
         queue.Enqueue(slice);
     }
 }
示例#3
0
 private void DecompressStreamParallel(GzipSlice gz)
 {
     byte[] buffer = new byte[BufferSize];
     using (MemoryStream mstream = new MemoryStream(gz.Slice))
         using (GZipStream gzstream = new GZipStream(mstream, CompressionMode.Decompress, true))
         {
             int lenght = gzstream.Read(buffer, 0, buffer.Length);
             gz.Slice = new byte[lenght];
             Array.Copy(buffer, gz.Slice, lenght);
             decompress.Add(gz.ID, gz.Slice);
         }
 }
示例#4
0
        private void CompressStreamParall(GzipSlice gz)
        {
            GzipSlice _out;

            using (MemoryStream mStream = new MemoryStream())
            {
                using (GZipStream gzStream = new GZipStream(mStream, CompressionLevel.Optimal))
                {
                    gzStream.Write(gz.Slice, 0, gz.Slice.Length);
                }
                _out = new GzipSlice(gz.ID, mStream.ToArray());
                writerQueue.Enqueue(_out);
            }
        }
示例#5
0
 private void Decompress(object i)
 {
     try
     {
         while (!readerQueue.closed || !readerQueue.IsEmpty())
         {
             GzipSlice gz = readerQueue.Dequeue();
             if (gz != null)
             {
                 DecompressStreamParallel(gz);
             }
         }
         waits[(int)i].Set();
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.ToString());
     }
 }
示例#6
0
 public override void Read()
 {
     try
     {
         using (FileStream sourceStream = new FileStream(SourceFile, FileMode.Open, FileAccess.Read))
         {
             while (sourceStream.Position < sourceStream.Length)
             {
                 byte[]    slices = new byte[BufferSize];
                 int       lenght = sourceStream.Read(slices, 0, BufferSize);
                 GzipSlice gz     = new GzipSlice(lenght);
                 Array.Copy(slices, gz.Slice, lenght);
                 readerQueue.Enqueue(gz);
             }
             readerQueue.Close();
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.ToString());
     }
 }
示例#7
0
 public override void Write()
 {
     try
     {
         using (FileStream targetStream = new FileStream(TargetFile, FileMode.Create))
         {
             while (!canceled || !writerQueue.IsEmpty())
             {
                 GzipSlice gz = writerQueue.Dequeue();
                 if (gz != null)
                 {
                     targetStream.Write(GetSizeInBytes((int)gz.ID), 0, (GetSizeInBytes((int)gz.ID)).Length);
                     targetStream.Write(GetSizeInBytes((int)gz.Slice.Length), 0, (GetSizeInBytes((int)gz.Slice.Length)).Length);
                     targetStream.Write(gz.Slice, 0, gz.Slice.Length);
                 }
             }
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.ToString());
     }
 }