typedef struct _FILE_ITEM_INFO {
DWORD dwLowFileSize ; // 文件大小低字段
DWORD dwHighFileSize ; // 文件大小高字段
DWORD dwPartSize ; // 文件块大小
DWORD dwPartNum ; // 文件块数量
CString szFileName ; // 源文件名
} FILE_ITEM_INFO ;
typedef FILE_ITEM_INFO* PFILE_ITEM_INFO ;
DWORD CFD::FDFileDivide ( PFILE_ITEM_INFO pItem )
{
// 删除该文件原有的分块文件
this->DeleteAllPartFiles ( pItem->szFileName ) ;
// 创建信息文件
HANDLE hFile = CreateFile (
pItem->szFileName + ".INFO",
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL ) ;
if ( hFile == INVALID_HANDLE_VALUE )
return GetLastError() ;
// 写入信息文件
DWORD dwWriteByte ;
WriteFile ( hFile, &(pItem->dwLowFileSize), sizeof(DWORD), &dwWriteByte, NULL ) ;
WriteFile ( hFile, &(pItem->dwPartSize), sizeof(DWORD), &dwWriteByte, NULL ) ;
WriteFile ( hFile, &(pItem->dwPartNum), sizeof(DWORD), &dwWriteByte, NULL ) ;
CloseHandle ( hFile ) ;
// 打开目标文件
hFile = CreateFile (
pItem->szFileName,
GENERIC_READ,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL ) ;
if ( hFile == INVALID_HANDLE_VALUE )
return GetLastError() ;
// 创建文件内存映射内核对象
HANDLE hMapFile = CreateFileMapping (
hFile,
NULL,
PAGE_READONLY,
0,
0,
NULL ) ;
if ( hMapFile == NULL )
{
CloseHandle ( hFile ) ;
return GetLastError() ;
}
CString TempStr ;
DWORD dwCurAddr = 0, dwCurPart = 0 ;
LPVOID lpMapAddr = 0 ;
// 分块循环映射文件
for ( UINT i = 1; i <= pItem->dwPartNum; i++ )
{
dwCurPart = pItem->dwLowFileSize - dwCurAddr ;
if ( dwCurPart > pItem->dwPartSize )
dwCurPart = pItem->dwPartSize ;
lpMapAddr = MapViewOfFile ( hMapFile, FILE_MAP_READ, 0, dwCurAddr, dwCurPart ) ;
if ( lpMapAddr == NULL )
{
CloseHandle ( hMapFile ) ;
CloseHandle ( hFile ) ;
return GetLastError() ;
}
dwCurAddr += dwCurPart ;
TempStr.Format ( "%s.PART_%d", pItem->szFileName, i ) ;
DeleteFile ( TempStr ) ;
HANDLE hNewFile = CreateFile (
TempStr,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL ) ;
if ( hFile == INVALID_HANDLE_VALUE )
return GetLastError() ;
HANDLE hNewMapFile = CreateFileMapping (
hNewFile,
NULL,
PAGE_READWRITE,
0,
dwCurPart,
NULL ) ;
if ( hNewMapFile == NULL )
{
CloseHandle ( hNewFile ) ;
return GetLastError() ;
}
LPVOID lpNewMapAddr = MapViewOfFile ( hNewMapFile, FILE_MAP_WRITE, 0, 0, 0 ) ;
if ( lpMapAddr == NULL )
{
CloseHandle ( hNewMapFile ) ;
CloseHandle ( hNewFile ) ;
return GetLastError() ;
}
memcpy ( lpNewMapAddr, lpMapAddr, dwCurPart ) ;
UnmapViewOfFile ( lpMapAddr ) ;
UnmapViewOfFile ( lpNewMapAddr ) ;
CloseHandle ( hNewMapFile ) ;
CloseHandle ( hNewFile ) ;
}
CloseHandle ( hMapFile ) ;
CloseHandle ( hFile ) ;
return 0 ;
}
DWORD CFD::FDFileConnect ( PFILE_ITEM_INFO pItem )
{
DeleteFile ( pItem->szFileName ) ;
// 打开目标文件
HANDLE hFile = CreateFile (
pItem->szFileName,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL ) ;
if ( hFile == INVALID_HANDLE_VALUE )
return GetLastError() ;
// 创建文件内存映射内核对象
HANDLE hMapFile = CreateFileMapping (
hFile,
NULL,
PAGE_READWRITE,
0,
pItem->dwLowFileSize,
NULL ) ;
if ( hMapFile == NULL )
{
CloseHandle ( hFile ) ;
return GetLastError() ;
}
CString TempStr ;
DWORD dwCurAddr = 0, dwCurPart = 0 ;
LPVOID lpMapAddr = 0 ;
// 分块循环映射文件
for ( UINT i = 1; i <= pItem->dwPartNum; i++ )
{
dwCurPart = pItem->dwLowFileSize - dwCurAddr ;
if ( dwCurPart > pItem->dwPartSize )
dwCurPart = pItem->dwPartSize ;
lpMapAddr = MapViewOfFile ( hMapFile, FILE_MAP_WRITE, 0, dwCurAddr, dwCurPart ) ;
if ( lpMapAddr == NULL )
{
CloseHandle ( hMapFile ) ;
CloseHandle ( hFile ) ;
return GetLastError() ;
}
dwCurAddr += dwCurPart ;
TempStr.Format ( "%s.PART_%d", pItem->szFileName, i ) ;
HANDLE hNewFile = CreateFile (
TempStr,
GENERIC_READ,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL ) ;
if ( hFile == INVALID_HANDLE_VALUE )
{
UnmapViewOfFile ( lpMapAddr ) ;
CloseHandle ( hMapFile ) ;
CloseHandle ( hFile ) ;
return GetLastError() ;
}
HANDLE hNewMapFile = CreateFileMapping (
hNewFile,
NULL,
PAGE_READONLY,
0,
0,
NULL ) ;
if ( hNewMapFile == NULL )
{
UnmapViewOfFile ( lpMapAddr ) ;
CloseHandle ( hMapFile ) ;
CloseHandle ( hFile ) ;
CloseHandle ( hNewFile ) ;
return GetLastError() ;
}
LPVOID lpNewMapAddr = MapViewOfFile ( hNewMapFile, FILE_MAP_READ, 0, 0, 0 ) ;
if ( lpMapAddr == NULL )
{
UnmapViewOfFile ( lpMapAddr ) ;
CloseHandle ( hMapFile ) ;
CloseHandle ( hFile ) ;
CloseHandle ( hNewMapFile ) ;
CloseHandle ( hNewFile ) ;
return GetLastError() ;
}
memcpy ( lpMapAddr, lpNewMapAddr, dwCurPart ) ;
FlushViewOfFile ( lpMapAddr, dwCurPart ) ;
UnmapViewOfFile ( lpMapAddr ) ;
UnmapViewOfFile ( lpNewMapAddr ) ;
CloseHandle ( hNewMapFile ) ;
CloseHandle ( hNewFile ) ;
}
CloseHandle ( hMapFile ) ;
CloseHandle ( hFile ) ;
this->DeleteAllPartFiles ( pItem->szFileName ) ;
return 0 ;
}
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!
上传的附件: