随笔 - 58  文章 - 14 评论 - 10 trackbacks - 0
<2008年4月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

与我联系

搜索

 

常用链接

留言簿

我参加的小组

随笔档案(58)

文章分类(11)

Watch

最新评论

  • 1. re: APACHE+ASP.NET 出现问题
  • 发现原来不是apache的原因,/joe/后面的那一串字符是sessionid,在web.config中sessionState的cookieless设置为UseUri就回出现这个问题。 这几天都在...
  • --josephshi
  • 2. re: compiere/adempiere+pgsql8.2+RHEL4+jdk1.5
  • compiere是不支持postgresql的,只支持postgresql plus AD server。adempiere是支持oracle postgresql的,若在linux下使用postgr...
  • --yuzifu

阅读排行榜

评论排行榜

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
posted on 2008-04-02 13:06 josephshi 阅读(114) 评论(0)  编辑 收藏

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
"五向定位"职业成长路线公开课(上海、南京、大连)
Google站内搜索


相关链接: