I try to write a compression application using Deflate Algorithm. However, when my compressed file is larger than my original file. Below is my code. I use window form so here are some clarifications. I use openFileDialog to choose a file, so openFileDialog.FileName hold the path. I write the compressed bytes to a memory stream. When it done, it check the length of my FileStream and my MemoryStream. The length of my MemoryStream is a lot bigger, which does not make any sense at all
infile = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
byte[] buffer = new byte[infile.Length];
//Read From the file
int count1 = infile.Read(buffer, 0, buffer.Length);
if (count1 != buffer.Length)
{
MessageBox.Show("Cannot Read From File");
infile.Close();
return;
}
infile.Close();
MemoryStream ms = new MemoryStream();
//Use this newly memory stream for the compressed data
DeflateStream compressedZipStream = new DeflateStream(ms, CompressionMode.Compress, true);
//Write data from the buffer array into the stream
compressedZipStream.Write(buffer, 0, buffer.Length);
//Close the stream
compressedZipStream.Close();
txtOriginal.Text = buffer.Length.ToString();
txtCompressed.Text = ms.Length.ToString();