Проблемы с распаковкой GZipStream
public void CompressFile(string fileName, string outFile)
{
var sync = new object();
using (var inStream =
new FileStream(fileName, FileMode.Open, FileAccess.Read))
using (var bReader = new BinaryReader(inStream))
using (var outStream =
new FileStream(outFile, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
var uncompressedBytes = new byte[1000000];
while (bReader.Read(uncompressedBytes, 0, uncompressedBytes.Length) > 0)
{
var thr = new Thread(
() =>
{
lock (sync)
{
CompressedBytes = CompressGzip(uncompressedBytes);
ThreadsCount++;
}
});
thr.Start();
thr.Join();
outStream.Write(CompressedBytes, 0, CompressedBytes.Length);
}
BeforeCompressionBytes = inStream.Length;
AfterCompressionBytes = outStream.Length;
inStream.Close();
outStream.Close();
}
}
public void DecompressFile(string fileName, string outFile)
{
var sync = new object();
using (var inStream =
new FileStream(fileName, FileMode.Open, FileAccess.Read))
using (var bReader = new BinaryReader(inStream))
using (var outStream =
new FileStream(outFile, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
var uncompressedBytes = new byte[1000000];
while (bReader.Read(uncompressedBytes, 0, uncompressedBytes.Length) > 0)
{
var thr = new Thread(
() =>
{
lock (sync)
{
CompressedBytes = DecompressGzip(uncompressedBytes);
ThreadsCount++;
}
});
thr.Start();
thr.Join();
outStream.Write(CompressedBytes, 0, CompressedBytes.Length);
}
BeforeCompressionBytes = inStream.Length;
AfterCompressionBytes = outStream.Length;
inStream.Close();
outStream.Close();
}
}
public byte[] CompressGzip(byte[] uncompressedBytes)
{
using (var memory = new MemoryStream())
{
using
(var gZipStream =
new GZipStream(memory, CompressionMode.Compress, true))
{
gZipStream.Write
(uncompressedBytes, 0, uncompressedBytes.Length);
}
return memory.ToArray();
}
}
public byte[] DecompressGzip(byte[] compressedBytes)
{
var decompressed = new byte[compressedBytes.Length];
using (var memory = new MemoryStream(compressedBytes))
{
using
(var gZipStream =
new GZipStream(memory, CompressionMode.Decompress, true))
{
gZipStream.Read
(decompressed, 0, compressedBytes.Length);
}
return memory.ToArray();
}
}
Вот мой код сжатия / распаковки. У меня есть задача читать файлы по блокам (размер блока 1 МБ) в другие потоки, сжимать и записывать каждый блок отдельно. И имейте "System.IO.InvalidDataException был пойман Сообщение = Магическое число в заголовке GZip не правильно. Убедитесь, что вы передаете в потоке GZip" при распаковке.
В чем дело?