Can anyone spot the error why it gives me the wrong value every time, never the correct one?
private long Main(string bzip2FilePath)
{
long totalUncompressedSize = 0;
try
{
var compressedDataByteArray = File.ReadAllBytes(bzip2FilePath);
using (var mstream = new MemoryStream(compressedDataByteArray))
using (var unzipstream = new BZip2InputStream(mstream))
using (var reader = new StreamReader(unzipstream))
{
char[] buffer = new char[4096];
int bytesRead;
while ((bytesRead = reader.Read(buffer, 0, buffer.Length)) > 0)
{
totalUncompressedSize += bytesRead;
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Error calculating BZip2 uncompressed size: {ex.Message}");
}
return totalUncompressedSize;
}
StreamReader
to read text and then counting bytes read? Text is not bytes. It's characters. If you want to know how many bytes there are then you should be reading the raw data. Character count and byte count will only be the same if every character is one byte, which may be the case but probably isn't.