您的位置:首页 > 编程语言 > VB

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

2008-12-28 13:54 295 查看



<script type="text/javascript"><!--
google_ad_client = "pub-8333940862668978";
/* 728x90, 创建于 08-11-30 */
google_ad_slot = "4485230109";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

压缩解压缩出现错误,但不知此错误引起原因
以下两个函数是我自己根据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, 0, buffer.Length)
destinationStream = New FileStream(destinationFile, FileMode.Create, FileAccess.Write)
compressedStream = New DeflateStream(destinationStream, CompressionMode.Compress, True)
compressedStream.Write(buffer, 0, 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, 0, 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, 0, 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, offset, 100) ''此句出错,提示:偏移量加上计数大于目标数组的长度。
If bytesread = 0 Then Exit While
offset += bytesread
total += bytesread
End While
destinationstream.Write(buffer, 0, 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, 0, 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, 0 ,100)

While (bytesread > 0)

destinationstream.Write(buffer, 0, bytesread)
bytesread = decompressedStream.Read(buffer, 0 ,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啦,你说是吧。哈哈
__________________________________________________________________________
高手真是少啊。。。少啊
__________________________________________________________________________
问题自行解决。
__________________________________________________________________________
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐