This is my code:
static string GenerateContent()
{
var rnd = new Random();
var sb = new StringBuilder();
for (int i = 0; i < 10000000; i++)
{
sb.Append(rnd.Next(0, 100));
}
return sb.ToString();
}
static void Compress(string input, string output)
{
using (var originalFileStream = File.OpenRead(input))
using (var compressedFileStream = File.OpenWrite(output))
using (var compressor = new GZipStream(compressedFileStream, CompressionMode.Compress))
originalFileStream.CopyTo(compressor);
}
static bool AreFilesEqual_Chunk(string input, string gzip)
{
var bytesToRead = 4096;
var one = new byte[bytesToRead];
var two = new byte[bytesToRead];
using (var gs = new GZipStream(File.OpenRead(gzip), CompressionMode.Decompress))
using (var fs = File.OpenRead(input))
{
int file1byte;
int file2byte;
do
{
file1byte = fs.ForceRead(one);
file2byte = gs.ForceRead(two);
}
while (one.SequenceEqual(two) && (file1byte != 0));
return file1byte == file2byte && file1byte == 0;
}
}
static void Main(string[] args)
{
var input = @"c:\logs\input.txt";
var output = @"c:\logs\output.gz";
// create input
File.WriteAllText(input, GenerateContent());
// compress input
Compress(input, output);
// compare files
var areFilesEqual = AreFilesEqual_Chunk(input, output);
Console.WriteLine(areFilesEqual);
// .NET 6.0 -> files aren't equal
// .NET core 3.1 -> files are equal
}
public static class Extensions
{
public static int ForceRead(this Stream fs, byte[] buffer)
{
var totalReadBytes = 0;
do
{
var readBytes = fs.Read(buffer, totalReadBytes, buffer.Length - totalReadBytes);
if (readBytes == 0)
return totalReadBytes;
totalReadBytes += readBytes;
}
while (totalReadBytes < buffer.Length);
return totalReadBytes;
}
}
If I run this with .NET 6.0
, then areFilesEqual
is false
. If I run this with .NET core 3.1
, then areFilesEqual
is true
. For some reason, when reading bytes from GZipStream
with .NET 6.0
I am not getting requested number of bytes (4096) all the time. Why does that happen? I noticed, that when I read bytes from GZipStream
on .NET 6.0
sometimes I get less bytes than requested, for example:
4096
4096
4096
4096
770
4096
4096
4096
4096
4096
665
Edit
I finally get things working. I added method ForceRead
to force missing bytes to be read.