不多说了,10G+的Sql测试分割成功,每块大小不要超过1G就好了。
- //Code by Pnig0s1992
- //Date:2012,3,18
- #include <stdio.h>
- #include <Windows.h>
- #define MAXLENGTH 30
- int main(int argc,char * argv[])
- {
- LPTSTR lpFileName = "xxx.sql";
- HANDLE hFile;
- struct FILEINFO
- {
- LONGLONG llFileSize;
- LONG llPartSize;
- DWORD dwPartNum;
- };
- FILEINFO fi;
- fi.llFileSize = 0;
- fi.llPartSize = 0;
- fi.dwPartNum = 0;
- fi.llPartSize = 1024*1024;
- hFile = CreateFile(lpFileName,GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
- if(hFile == INVALID_HANDLE_VALUE)
- {
- printf("\n获得文件句柄失败%d。",GetLastError());
- return -1;
- }
- LARGE_INTEGER uli = {0};
- SYSTEM_INFO SysInfo;
- GetSystemInfo(&SysInfo);
- DWORD dwGran = SysInfo.dwAllocationGranularity;
- LONGLONG qwFileOffset = 0;
- LONGLONG dwBlockBytes = fi.llPartSize;
- if(GetFileSizeEx(hFile,&uli))
- {
- fi.llFileSize = uli.QuadPart;
- fi.dwPartNum = fi.llFileSize/dwBlockBytes;
- if((fi.llFileSize%dwBlockBytes) != 0)
- {
- fi.dwPartNum+=1;
- }
- printf("\n文件:%s 大小为:%I64d KB",lpFileName,uli.QuadPart/1024);
- printf("\n分块大小:%u KB 分块数:%u",(dwBlockBytes/1024),fi.dwPartNum);
- }
- HANDLE hFileMap;
- hFileMap = CreateFileMapping(hFile,NULL,PAGE_READONLY,0,0,NULL);//超大文件时该处失败
- if(hFileMap == NULL)
- {
- printf("\n创建文件映射对象句柄失败:%d。",GetLastError());
- CloseHandle(hFile);
- return -1;
- }
- LPSTR sTempStr = (LPSTR)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,MAXLENGTH);
- for(int index = 0;index<fi.dwPartNum;index++)
- {
- LPVOID lpMapStartPos;
- if(fi.llFileSize < dwBlockBytes)
- {
- dwBlockBytes = fi.llFileSize;
- }
- lpMapStartPos = MapViewOfFile(hFileMap,FILE_MAP_READ,0,qwFileOffset,dwBlockBytes);
- if(lpMapStartPos == NULL)
- {
- printf("\n映射目标文件到内存失败:%d。",GetLastError());
- CloseHandle(hFileMap);
- CloseHandle(hFile);
- return -1;
- }
- qwFileOffset += dwBlockBytes;
- fi.llFileSize -= dwBlockBytes;
- sprintf(sTempStr,"%s_%d",lpFileName,index+1);
- HANDLE hNewFile = CreateFile(sTempStr,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
- if(hNewFile == INVALID_HANDLE_VALUE)
- {
- printf("\n获取分块文件对象句柄失败:%d。",GetLastError());
- UnmapViewOfFile(lpMapStartPos);
- CloseHandle(hFileMap);
- CloseHandle(hFile);
- return -1;
- }
- HANDLE hNewFileMap;
- hNewFileMap = CreateFileMapping(hNewFile,NULL,PAGE_READWRITE,0,dwBlockBytes,NULL);
- if(hNewFile == NULL)
- {
- printf("\n获取分块文件内存映射对象句柄失败:%d。",GetLastError());
- UnmapViewOfFile(lpMapStartPos);
- CloseHandle(hNewFile);
- CloseHandle(hFileMap);
- CloseHandle(hFile);
- return -1;
- }
- LPVOID lpNewFilePos = MapViewOfFile(hNewFileMap,FILE_MAP_WRITE,0,0,0);
- if(lpNewFilePos == NULL)
- {
- printf("\n映射分块文件到内存失败:%d。",GetLastError());
- UnmapViewOfFile(lpMapStartPos);
- CloseHandle(hNewFileMap);
- CloseHandle(hNewFile);
- CloseHandle(hFileMap);
- CloseHandle(hFile);
- return -1;
- }
- CopyMemory(lpNewFilePos,lpMapStartPos,dwBlockBytes);
- printf("\n分块文件%s成功生成。",sTempStr);
- CloseHandle(hNewFileMap);
- CloseHandle(hNewFile);
- UnmapViewOfFile(lpMapStartPos);
- UnmapViewOfFile(lpNewFilePos);
- }
- CloseHandle(hFileMap);
- CloseHandle(hFile);
- system("pause");
- return 0;
- }