Try this code:
try replacing .Zip with .rar in second line of code
FileStream sourceFile = File.OpenRead(@"C:\sample.xml");
FileStream destFile = File.Create(@"C:\sample.zip");
GZipStream compStream = new GZipStream(destFile, CompressionMode.Compress);
try
{
int theByte = sourceFile.ReadByte();
while (theByte != -1)
{
compStream.WriteByte((byte)theByte);
theByte = sourceFile.ReadByte();
}
}
finally
{
compStream.Dispose();
}