加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

[VB.NET]压缩解压缩出现错误,但不知此错误引起原因

发布时间:2020-12-16 23:44:05 所属栏目:大数据 来源:网络整理
导读:压缩解压缩出现错误,但不知此错误引起原因 以下两个函数是我自己根据msdn写的,可有时会出错(看清楚,只是有时) 请各位高手帮忙看下: Imports System.IO.Compression Imports System.IO Public Class Form1 Private Sub Form1_Load(ByVal sender As Syst
压缩解压缩出现错误,但不知此错误引起原因 以下两个函数是我自己根据msdn写的,可有时会出错(看清楚,只是有时) 请各位高手帮忙看下: Imports System.IO.Compression Imports System.IO Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load End Sub Function ZipFile(ByVal sourceFile As String,ByVal destinationFile As String) As Boolean On Error GoTo 999 ''源文件,目的文件 ''On Error Resume Next Dim buffer() As Byte Dim sourceStream As FileStream Dim destinationStream As FileStream Dim compressedStream As DeflateStream sourceStream = New FileStream(sourceFile,FileMode.Open,FileAccess.Read,FileShare.Read) ReDim buffer(sourceStream.Length) sourceStream.Read(buffer,buffer.Length) destinationStream = New FileStream(destinationFile,FileMode.Create,FileAccess.Write) compressedStream = New DeflateStream(destinationStream,CompressionMode.Compress,True) compressedStream.Write(buffer,buffer.Length) sourceStream.Close() compressedStream.Close() destinationStream.Close() Return True Exit Function 999: Return False End Function Public Function unZipFile(ByVal sourceFile As String,ByVal destinationFile As String) As Boolean ''源文件,目的文件 ''On Error GoTo 999 Dim sourceStream,destinationstream As FileStream Dim decompressedStream As DeflateStream Dim quartetbuffer() As Byte sourceStream = New FileStream(sourceFile,FileMode.Open) decompressedStream = New DeflateStream(sourceStream,CompressionMode.Decompress,True) ReDim quartetbuffer(4) Dim posiTion As Long = CInt(sourceStream.Length) - 4 sourceStream.Position = posiTion sourceStream.Read(quartetbuffer,4) sourceStream.Position = 0 Dim checkLength = BitConverter.ToInt32(quartetbuffer,0) Dim buffer(checkLength + 100) As Byte Dim offset As Long = 0 Dim total As Long = 0 destinationstream = New FileStream(destinationFile,FileMode.Create) While (True) Dim bytesread As Long = decompressedStream.Read(buffer,offset,100) ''此句出错,提示:偏移量加上计数大于目标数组的长度。 If bytesread = 0 Then Exit While offset += bytesread total += bytesread End While destinationstream.Write(buffer,total) destinationstream.Flush() sourceStream.Close() decompressedStream.Close() destinationstream.Close() Return True Exit Function 999: Return False End Function ''以下是调用示例 Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click ZipFile( "e:/all.mdb ","e:/abc.rar ") unZipFile( "e:/abc.rar ","e:/aa.mdb ") End Sub End Class 出错语句位于解压那函数,这情况我实在不知道到底是压缩出错了还是解压出错了? __________________________________________________________________________ 出错的时候是出什么错?? __________________________________________________________________________ vb啊....头大 __________________________________________________________________________ 好久没看vb了 __________________________________________________________________________ d __________________________________________________________________________ 2楼3楼,你们不看vb来这里干什么 __________________________________________________________________________ mark __________________________________________________________________________ lz不要那么激动。。。帮你顶还挨你骂? __________________________________________________________________________ While (True) Dim bytesread As Long = decompressedStream.Read(buffer,total) --------------------------- 我觉得楼主在这里的代码逻辑有问题,你这样写或许可以消除这个问题。 While (True) Dim buffer(100) as byte Dim bytesread As Long = decompressedStream.Read(buffer,0 100) If bytesread = 0 Then Exit While destinationstream.Write(buffer,bytesread) End While 这样有数据就直接写在文件流里面,而不要去拼接buffer。 __________________________________________________________________________ 继续顶吧,问题没有解决 __________________________________________________________________________ bejon(阿牛[如果我懂,必坦诚相告;如果您懂,请不吝赐教。]) ( ) 信誉:93 Blog 加为好友 2007-05-16 15:13:07 得分: 0 继续顶吧,问题没有解决 --------------------------------------------------------- 到底是什么问题呢??? __________________________________________________________________________ On Error GoTo 999 ''源文件,目的文件 ''On Error Resume Next 汗你的变成习惯,VB来的吧? __________________________________________________________________________ 编程习惯,需要与时俱进了。 __________________________________________________________________________ wzd24(牧野)(衣带渐宽终不悔,为伊消得人憔悴) ( ) 信誉:100 到底是什么问题呢??? ------------------------------------------- 昏,问题就出在解压时有时会出错啊。。。。你没看出来吗? __________________________________________________________________________ 如果要忽略所有错误的话我不会用try,只有需要确定是哪一句语句出错时才可能用try,感觉写这么多.net程序这样也不会有什么问题。 __________________________________________________________________________ Dim buffer(100) as byte Dim bytesread As Long = decompressedStream.Read(buffer,100) While (bytesread > 0) destinationstream.Write(buffer,bytesread) bytesread = decompressedStream.Read(buffer,100) End While 这样写比while(True)合理 __________________________________________________________________________ 楼上到底是什么异常 我觉得应该是ArgumentException 这个异常吧 __________________________________________________________________________ ''On Error GoTo 999 ''On Error Resume Next 这两种写法,是没有问题——绝对没有运行上的问题 可是程序的结构花会遭到破坏,写这两行的,基本上都是VB或者Asp转过来的程序员。 个人不赞成这么写,虽然我也是从这两种语言转过来的。 __________________________________________________________________________ 对大文件进行压缩时发生“System.OutOfMemoryException”异常 Dim buffer(checkLength + 100) As Byte 这句先出错 别讨论编写习惯了,你有你的习惯,我有我的习惯,已经习惯了。假如有一天我会觉得确实要改的话那也是简单的事,你说是么?不知道楼上有没有故意用error产生错误的情况,不知道楼上的是否会可能是碰到在一个方法出现N个无关紧要要的错误而写上一大堆try... vb8与vb6有很大的区别,但却依然保留了on error goto 等错误捕捉方法不是没有道理的,如果你能说服微软把这个去掉我就不得不用try啦,你说是吧。哈哈 __________________________________________________________________________ 高手真是少啊。。。少啊 __________________________________________________________________________ 问题自行解决。 __________________________________________________________________________

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读