首页 > 其他专区 > Access >

vb.net在内存中解压文件

Access 2023-01-11

Public Class Form1

 

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

'保存压缩文件

Dim fso As New SaveFileDialog

If fso.ShowDialog = Windows.Forms.DialogResult.OK Then

GzipFile(fso.FileName)

End If

 

End Sub

Private Sub GzipFile(ByVal FileName As String)

Try

'定义文件流,用于写入压缩流

Dim fs As FileStream = New FileStream(FileName, FileMode.Create, FileAccess.Write)

'定义压缩流,用于压缩数据

Dim compressStream = New GZipStream(fs, CompressionMode.Compress, False)

'定义写入操作流,用于向压缩流写入数据

Dim sw As StreamWriter = New StreamWriter(compressStream, System.Text.Encoding.GetEncoding("GB2312"))

sw.WriteLine(Me.RichTextBox1.Text)

sw.Close()

MsgBox("压缩文件成功")

Catch ex As Exception

MsgBox(ex.ToString)

End Try

End Sub

Private Function Decompress(ByVal FileName As String)

Dim DeCompressStr As String = ""

'定义文件流,用于读取数据

Dim fs As New FileStream(FileName, FileMode.Open, FileAccess.Read)

'定义压缩流,用于解压缩,生成解压流

Dim DecomprssStream As New GZipStream(fs, CompressionMode.Decompress, False)

'定义读取操作流,用于从压缩流读取数据

Dim sr As New StreamReader(DecomprssStream, System.Text.Encoding.GetEncoding("GB2312"))

'返回读取出的内容()

DeCompressStr = sr.ReadToEnd

sr.Close()

Return DeCompressStr

End Function

 

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click

'打开压缩的文件,以读取其内容

Dim fso As New OpenFileDialog

If fso.ShowDialog = Windows.Forms.DialogResult.OK Then

Me.RichTextBox1.Text = Decompress(fso.FileName)

MsgBox("解压内容完成")

End If

End Sub

End Class


Copyright © 2016-2023 office学习教程网 office.tqzw.net.cn. All Rights Reserved.