分享
 
 
 

VB 实现大文件的分割与恢复,引用 ADODB.Stream 提供一个过程代码

王朝vb·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

'VB 实现大文件的分割与恢复,引用 ADODB.Stream 提供一个过程:

'要引用 Microsoft ActiveX Data Objects 2.5 Libary

'或 Microsoft ActiveX Data Objects 2.6 Libary

Public Sub StreamSplit(SourceFile As String, DestinationFile As String, ChunkSize As Long, Optional BufferSize As Long = 64# * 1024#, Optional ShowFinishMessage As Boolean)

'ChunkSize 为 BufferSize 的倍数

Dim adoStreamS As New ADODB.Stream

adoStreamS.Type = adTypeBinary

adoStreamS.Open

adoStreamS.LoadFromFile SourceFile

Dim lFileSize As Long

lFileSize = adoStreamS.Size

Dim i As Long

Dim adoStreamT As New ADODB.Stream

adoStreamT.Type = adTypeBinary

Do While lFileSize >= ChunkSize * BufferSize

adoStreamT.Open

adoStreamT.Write adoStreamS.Read(ChunkSize * BufferSize)

adoStreamT.SaveToFile DestinationFile & "." & Format(i, "000"), IIf(Len(Trim(Dir(DestinationFile & "." & Format(i, "000")))) > 0, adSaveCreateOverWrite, adSaveCreateNotExist)

adoStreamT.Close

lFileSize = lFileSize - ChunkSize * BufferSize

i = i + 1

Loop

If lFileSize > 0 Then

adoStreamT.Open

adoStreamT.Write adoStreamS.Read(lFileSize)

adoStreamT.SaveToFile DestinationFile & "." & Format(i, "000"), IIf(Len(Trim(Dir(DestinationFile & "." & Format(i, "000")))) > 0, adSaveCreateOverWrite, adSaveCreateNotExist)

End If

If ShowFinishMessage Then

MsgBox "Finished!"

End If

End Sub

Public Sub StreamRestore(SourceFile As String, DestinationFile As String, Chunks As Long, Optional BufferSize As Long = 64# * 1024#, Optional ShowFinishMessage As Boolean)

Dim lFileSize As Long

Dim adoStreamT As New ADODB.Stream

adoStreamT.Type = adTypeBinary

adoStreamT.Open

Dim adoStreamS As New ADODB.Stream

adoStreamS.Type = adTypeBinary

Dim i As Long

For i = 0 To Chunks - 1 'Chunks 块数

adoStreamS.Open

adoStreamS.LoadFromFile SourceFile & "." & Format(i, "000")

adoStreamT.Write adoStreamS.Read

adoStreamS.Close

Next i

adoStreamT.SaveToFile DestinationFile, IIf(Len(Trim(Dir(DestinationFile))) > 0, adSaveCreateOverWrite, adSaveCreateNotExist)

If ShowFinishMessage Then

MsgBox "Finished!"

End If

End Sub

'VB 实现大文件的分割与恢复,采用读写二进制数据的传统经典代码:

Public Sub FileSplit(SourceFile As String, DestinationFile As String, ChunkSize As Long, Optional BufferSize As Long = 64# * 1024#, Optional ShowFinishMessage As Boolean)

'ChunkSize 为 BufferSize 的倍数

Dim FileBuffer() As Byte

Dim FileNumberS As Long

Dim FileNumberT As Long

FileNumberS = FreeFile

Open SourceFile For Binary Access Read As #FileNumberS

Dim lFileLen As Long

lFileLen = FileLen(SourceFile)

FileNumberT = FreeFile

Dim i As Long

Dim j As Long

ReDim FileBuffer(1 To (BufferSize)) As Byte

Open DestinationFile & "." & Format(i, "000") For Binary Access Write As #FileNumberT

Do While lFileLen >= BufferSize

Get #FileNumberS, , FileBuffer

If i = ChunkSize Then

i = 0

j = j + 1

Close #FileNumberT

FileNumberT = FreeFile

Open DestinationFile & "." & Format(j, "000") For Binary Access Write As #FileNumberT

End If

i = i + 1

Put #FileNumberT, , FileBuffer

lFileLen = lFileLen - BufferSize

Loop

If lFileLen > 0 Then

ReDim FileBuffer(1 To lFileLen) As Byte

Get #FileNumberS, , FileBuffer

Put #FileNumberT, , FileBuffer

End If

Close #FileNumberT

If ShowFinishMessage Then

MsgBox "Finished!"

End If

End Sub

Public Sub FileRestore(SourceFile As String, DestinationFile As String, Chunks As Long, Optional BufferSize As Long = 64# * 1024#, Optional ShowFinishMessage As Boolean)

Dim FileBuffer() As Byte

Dim FileNumberS As Long

Dim FileNumberT As Long

Dim i As Long

Dim lFileLen As Long

FileNumberT = FreeFile

Open DestinationFile For Binary Access Write As #FileNumberT

For i = 0 To Chunks - 1

FileNumberS = FreeFile

Open SourceFile & "." & Format(i, "000") For Binary Access Read As #FileNumberS

lFileLen = FileLen(SourceFile & "." & Format(i, "000"))

ReDim FileBuffer(1 To BufferSize) As Byte

Do While lFileLen >= BufferSize

Get #FileNumberS, , FileBuffer

Put #FileNumberT, , FileBuffer

lFileLen = lFileLen - BufferSize

Loop

If lFileLen > 0 Then

ReDim FileBuffer(1 To lFileLen) As Byte

Get #FileNumberS, , FileBuffer

Put #FileNumberT, , FileBuffer

End If

Close #FileNumberS

Next i

Close #FileNumberT

If ShowFinishMessage Then

MsgBox "Finished!"

End If

End Sub

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有