Read and Write compressed data to a binary file using ASP.NET
The
BinaryReader and BinaryWriter in System.IO namespace read and write
primitive types in binary, to a stream. The
System.IO.Compression.GZipStream provides methods and properties to
compress and decompress the stream. In this article, we will learn how
to use compression techniques to compress the stream and write it to a
binary file. We will then use the BinaryReader and the GZipStream class
to decompress and read the file to display it on the webpage.
We have different types of files on our computer. The Binary file is one such format. You cannot read these binary files by just opening them in the notepad.
In this article, we will use the classes BinaryWriter and BinaryReader
to add information to the file and to retrieve information from it. We
will be using a FileStream constructor to open and modify the file if
it already exists, or create a new file if it does not exist. You can
then pass the FileStreamobject and use it to construct your
BinaryReader and BinaryWriter. Let us see some code:
Read and Writing Binary Files Without Compression
Note: Use the namespaces System.IO
Writing Binary data
C#
string str = "Read and Write compressed data to a binary file”;
// Write to a binary file
FileStream fs = null;
BinaryWriter bw = null;
try
{
fs = new FileStream(@"C:"test.bin", FileMode.OpenOrCreate);
bw = new BinaryWriter(fs);
bw.Write(str);
}
catch (IOException ex)
{
}
finally
{
bw.Flush();
bw.Close();
fs.Close();
}
VB.NET
Dim str as string = "Read and Write compressed data to a binary file”
' Write to a binary file
Dim fs As FileStream = Nothing
Dim bw As BinaryWriter = Nothing
Try
fs = New FileStream("C:"test.bin", FileMode.OpenOrCreate)
bw = New BinaryWriter(fs)
bw.Write(str)
Catch ex As IOException
Finally
bw.Flush()
bw.Close()
fs.Close()
End Try
Reading Binary Data
C#
// Read from a binary file
FileStream fs1 = null;
BinaryReader br = null;
try
{
fs1 = new FileStream(@"C:"test.bin", FileMode.Open);
br = new BinaryReader(fs1);
string str1;
str1 = br.ReadString();
Response.Write(str1);
}
catch (IOException ex)
{
}
finally
{
br.Close();
fs1.Close();
}
VB.NET
' Read from a binary file
Dim fs1 As FileStream = Nothing
Dim br As BinaryReader = Nothing
Try
fs1 = New FileStream("C:"test.bin", FileMode.Open)
br = New BinaryReader(fs1)
Dim str1 As String
str1 = br.ReadString()
Response.Write(str1)
Catch ex As IOException
Finally
br.Close()
fs1.Close()
End Try
Read and Writing Binary Files Using Compression
Note:
Use the namespaces System.IO.Compression. Here instead of passing the
file stream object directly to the BinaryWriter, we are passing the
stream to GZipStream to compress it. The compressed stream is then
passed to BinaryWriter. Similarly while reading the file, we pass the
stream to GZipStream to decompress and then use BinaryReader to read
the file and display its contents.
Writing Binary data
C#
// Compress and write to a binary file
FileStream fs = null;
BinaryWriter bw = null;
try
{
fs = new FileStream(@"C:"test1.bin", FileMode.OpenOrCreate);
GZipStream cmp = new GZipStream(fs, CompressionMode.Compress);
bw = new BinaryWriter(cmp);
bw.Write(str);
}
catch (IOException ex)
{
}
finally
{
bw.Flush();
bw.Close();
fs.Close();
}
VB.NET
' Compress and write to a binary file
Dim fs As FileStream = Nothing
Dim bw As BinaryWriter = Nothing
Try
fs = New FileStream("C:"test1.bin", FileMode.OpenOrCreate)
Dim cmp As GZipStream = New GZipStream(fs, CompressionMode.Compress)
bw = New BinaryWriter(cmp)
bw.Write(str)
Catch ex As IOException
Finally
bw.Flush()
bw.Close()
fs.Close()
End Try
Reading Binary Data
C#
// Decompress and read from a binary file
FileStream fs1 = null;
BinaryReader br = null;
try
{
fs1 = new FileStream(@"C:"test1.bin", FileMode.Open);
GZipStream dcmp = new GZipStream(fs1,
CompressionMode.Decompress);
br = new BinaryReader(dcmp);
string str1;
str1 = br.ReadString();
Response.Write(str1);
}
catch (IOException ex)
{
}
finally
{
br.Close();
fs1.Close();
}
VB.NET
' Decompress and read from a binary file
Dim fs1 As FileStream = Nothing
Dim br As BinaryReader = Nothing
Try
fs1 = New FileStream("C:"test1.bin", FileMode.Open)
Dim dcmp As GZipStream = New GZipStream(fs1, CompressionMode.Decompress)
br = New BinaryReader(dcmp)
Dim str1 As String
str1 = br.ReadString()
Response.Write(str1)
Catch ex As IOException
Finally
br.Close()
fs1.Close()
End Try