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

2015-07-04 0 523
按指定数量字节分块读取文件
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

遇见资源网 ASP/Basic 按指定数量字节分块读取文件 http://www.ox520.com/13441.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务