[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啦,你说是吧。哈哈
__________________________________________________________________________
高手真是少啊。。。少啊
__________________________________________________________________________
问题自行解决。
__________________________________________________________________________
相关文章推荐
- asp.net 中 使用 excel :检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005
- vs.net C# 连接数据库 出现错误 用户 '(null)' 登录失败。原因: 未与信任 SQL Server 连接相关联
- 关于安装.NET Framework 2.0时偶然碰到的问题(提示"Error 205015. 安装程序集..../System.EnterpriseServices.dll失败,原因是出现了系统错误:拒绝访问。")
- 安装SQL Server 2012过程中出现“启用windows功能NetFx3时出错”(错误原因、详细分析及解决方法)以及在Windows Server2012上安装.NET Framework 3
- 使用VB.net Express 2010开发AutoCAD.net插件调试时出现很多错误的解决办法
- VB.NET 多次读取ACCESS 建立树形时出现 链接关闭错误
- [VB.NET]将VB中文档写入Word时出现的错误,帮忙解决一下,谢谢
- 出现类不明确错误的原因及解决方法(aps.net)
- 【原创】asp.net导出word 检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误: 8000401a
- asp.net中ListView控件动态绑定数据源后,使用DataPager出现错误的原因及解决办法
- 【转】VB连接Excel2010数据库时出现“找不到可安装的 ISAM”错误的原因
- 在编译php事务时候出现如下错误,具体原因不知,不过解决了
- 关于VB.NET出现“集合已修改;可能无法执行枚举操作”错误的解决
- 安装SQL Server 2012过程中出现“启用windows功能NetFx3时出错”(错误原因、详细分析及解决方法)以及在Windows Server2012上安装.NET Framework 3.5的详细分析及安装过程
- error 25015安装程序集“C:\Windows\Microsoft .NET\Framework\v2.0.50727\xxx.dll”失败,原因是出现了系统错误:另一个程序正在使用此文件,进程无法访问
- 系统启动出现0x000000F4错误是由什么原因引起的及分析?
- VB.net 2005内GZipStream类别可以针对档案做压缩、解压缩
- 在asp.net(vb)中,应用ajaxpro.dll操作Session出现错误的解决方法
- 安装SQL Server 2012过程中出现“启用windows功能NetFx3时出错”(错误原因、详细分析及解决方法)以及在Windows Server2012上安装.NET Framework 3
- 笔记13:vb.net网络编程中出现的错误