public const int BLOCK_SIZE = 900000;
private void buttonCalculate_Click(object sender, EventArgs e)
{
string x = richTextBoxStringX.Text.ToString();
string y = richTextBoxStringY.Text.ToString();
string xy = x + y;
Directory.CreateDirectory(@"\NCD");
// Note: Encoding must be the same for text comparison
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
byte[] bytes_x = enc.GetBytes(x.ToCharArray());
byte[] bytes_y = enc.GetBytes(y.ToCharArray());
byte[] bytes_xy = enc.GetBytes(xy.ToCharArray());
// Throw exception when xy > blocksize
if (bytes_xy.Length > BLOCK_SIZE)
{
throw new
Exception("Concatenation exceeds block size of compressor.");
}
GZipOutputStream bzos;
// Compress x
bzos = new GZipOutputStream(File.Create(@"\NCD\x.zip"), BLOCK_SIZE);
bzos.Write(bytes_x, 0, bytes_x.Length);
bzos.Close();
// Get file size compressed x
System.IO.FileInfo fileInfo;
fileInfo = new System.IO.FileInfo(@"\NCD\x.zip");
long bytes_Cx = fileInfo.Length;
// Compress y
bzos = new GZipOutputStream(File.Create(@"\NCD\y.zip"), BLOCK_SIZE);
bzos.Write(bytes_y, 0, bytes_y.Length);
bzos.Close();
// Get file size compressed y
fileInfo = new System.IO.FileInfo(@"\NCD\y.zip");
long bytes_Cy = fileInfo.Length;
// Compress xy
bzos = new GZipOutputStream(File.Create(@"\NCD\xy.zip"),
BLOCK_SIZE);
bzos.Write(bytes_xy, 0, bytes_xy.Length);
bzos.Close();
// Get file size compressed xy
fileInfo = new System.IO.FileInfo(@"\NCD\xy.zip");
long bytes_Cxy = fileInfo.Length;
// Determine C(xy)-min{C(x),C(y)}
long max_C;
if (bytes_Cx > bytes_Cy)
{
max_C = bytes_Cxy - bytes_Cy;
}
else
{
max_C = bytes_Cxy - bytes_Cx;
}
// Determine max{C(x), C(y)}
long max_Cx_Cy;
if (bytes_Cx > bytes_Cy)
{
max_Cx_Cy = bytes_Cx;
}
else
{
max_Cx_Cy = bytes_Cy;
}
// NCD = C(xy)-min{C(x),C(y)} / max{C(x), C(y)}
float NCD = (float)max_C /(float)max_Cx_Cy;
textBoxNCD.Text = NCD.ToString();
}