按指定数量字节分块读取文件

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

Option Explicit
Private Const OFS_MAXPATHNAME = 128

Private Type OFSTRUCT
cBytes As Byte
fFixedDisk As Byte
nErrCode As Integer
Reserved1 As Integer
Reserved2 As Integer
szPathName(OFS_MAXPATHNAME) As Byte
End Type

Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
Private Declare Function SetFilePointer Lib "kernel32" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long
Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Any) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Const OF_READ = &H0
Private Const FILE_SHARE_READ = &H1
Private Const MAXLONG = &H7FFFFFFF

Private Function ReadBlocks(filePath As String) As Boolean
On Error GoTo Err
Dim fHandle As Long
Dim OF As OFSTRUCT
fHandle = OpenFile(filePath, OF, OF_READ)
If fHandle <> -1 Then
Dim nSize As Long
nSize = GetFileSize(fHandle, 0)
If nSize > 0 Then
Dim Rtn As Long,filePos As Long
Rtn= MAXLONG/16
ReDim bBytes(Rtn - 1) As Byte
Do While Rtn = Len(bBytes(0))
SetFilePointer hFile, filePos , 0, FILE_BEGIN
ReadFile hFile, bBytes(0), Rtn, Rtn, 0
filePos= filePos+Rtn
Loop
ReadBlocks = True
End If
CloseHandle fHandle
End If
If Err Then
Err.Clear
ReadBlocks = False
End If
Exit Function
Err:
CloseHandle fHandle
ReadBlocks = False
End Sub