[WinApi]超大文件分割[10G+]

不多说了,10G+的Sql测试分割成功,每块大小不要超过1G就好了。

  
  
  
  
  1. //Code by Pnig0s1992  
  2. //Date:2012,3,18  
  3. #include <stdio.h>  
  4. #include <Windows.h>  
  5.  
  6. #define MAXLENGTH 30  
  7.  
  8. int main(int argc,char * argv[])  
  9. {  
  10.     LPTSTR lpFileName = "xxx.sql";  
  11.     HANDLE hFile;  
  12.     struct FILEINFO  
  13.     {  
  14.         LONGLONG llFileSize;  
  15.         LONG llPartSize;  
  16.         DWORD dwPartNum;  
  17.     };  
  18.  
  19.     FILEINFO fi;  
  20.     fi.llFileSize = 0;  
  21.     fi.llPartSize = 0;  
  22.     fi.dwPartNum = 0;  
  23.     fi.llPartSize = 1024*1024;  
  24.     hFile = CreateFile(lpFileName,GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);  
  25.     if(hFile == INVALID_HANDLE_VALUE)  
  26.     {  
  27.         printf("\n获得文件句柄失败%d。",GetLastError());  
  28.         return -1;  
  29.     }  
  30.  
  31.     LARGE_INTEGER uli = {0};  
  32.     SYSTEM_INFO SysInfo;  
  33.     GetSystemInfo(&SysInfo);  
  34.     DWORD dwGran = SysInfo.dwAllocationGranularity;  
  35.  
  36.     LONGLONG qwFileOffset = 0;  
  37.     LONGLONG dwBlockBytes = fi.llPartSize;  
  38.     if(GetFileSizeEx(hFile,&uli))  
  39.     {  
  40.         fi.llFileSize = uli.QuadPart;  
  41.         fi.dwPartNum = fi.llFileSize/dwBlockBytes;  
  42.         if((fi.llFileSize%dwBlockBytes) != 0)  
  43.         {  
  44.             fi.dwPartNum+=1;  
  45.         }  
  46.         printf("\n文件:%s 大小为:%I64d KB",lpFileName,uli.QuadPart/1024);  
  47.         printf("\n分块大小:%u KB 分块数:%u",(dwBlockBytes/1024),fi.dwPartNum);  
  48.     }  
  49.  
  50.     HANDLE hFileMap;  
  51.     hFileMap = CreateFileMapping(hFile,NULL,PAGE_READONLY,0,0,NULL);//超大文件时该处失败  
  52.     if(hFileMap == NULL)  
  53.     {  
  54.         printf("\n创建文件映射对象句柄失败:%d。",GetLastError());  
  55.         CloseHandle(hFile);  
  56.         return -1;  
  57.     }  
  58.  
  59.     LPSTR sTempStr = (LPSTR)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,MAXLENGTH);  
  60.     for(int index = 0;index<fi.dwPartNum;index++)  
  61.     {  
  62.         LPVOID lpMapStartPos;  
  63.         if(fi.llFileSize < dwBlockBytes)  
  64.         {  
  65.             dwBlockBytes = fi.llFileSize;  
  66.         }  
  67.         lpMapStartPos = MapViewOfFile(hFileMap,FILE_MAP_READ,0,qwFileOffset,dwBlockBytes);  
  68.         if(lpMapStartPos == NULL)  
  69.         {  
  70.             printf("\n映射目标文件到内存失败:%d。",GetLastError());  
  71.             CloseHandle(hFileMap);  
  72.             CloseHandle(hFile);  
  73.             return -1;  
  74.         }  
  75.         qwFileOffset += dwBlockBytes;  
  76.         fi.llFileSize  -=  dwBlockBytes;  
  77.  
  78.         sprintf(sTempStr,"%s_%d",lpFileName,index+1);  
  79.         HANDLE hNewFile = CreateFile(sTempStr,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);  
  80.         if(hNewFile == INVALID_HANDLE_VALUE)  
  81.         {  
  82.             printf("\n获取分块文件对象句柄失败:%d。",GetLastError());  
  83.             UnmapViewOfFile(lpMapStartPos);  
  84.             CloseHandle(hFileMap);  
  85.             CloseHandle(hFile);  
  86.             return -1;  
  87.         }  
  88.         HANDLE hNewFileMap;  
  89.         hNewFileMap = CreateFileMapping(hNewFile,NULL,PAGE_READWRITE,0,dwBlockBytes,NULL);  
  90.         if(hNewFile == NULL)  
  91.         {  
  92.             printf("\n获取分块文件内存映射对象句柄失败:%d。",GetLastError());  
  93.             UnmapViewOfFile(lpMapStartPos);  
  94.             CloseHandle(hNewFile);  
  95.             CloseHandle(hFileMap);  
  96.             CloseHandle(hFile);  
  97.             return -1;  
  98.         }  
  99.         LPVOID lpNewFilePos = MapViewOfFile(hNewFileMap,FILE_MAP_WRITE,0,0,0);  
  100.         if(lpNewFilePos == NULL)  
  101.         {  
  102.             printf("\n映射分块文件到内存失败:%d。",GetLastError());  
  103.             UnmapViewOfFile(lpMapStartPos);  
  104.             CloseHandle(hNewFileMap);  
  105.             CloseHandle(hNewFile);  
  106.             CloseHandle(hFileMap);  
  107.             CloseHandle(hFile);  
  108.             return -1;  
  109.         }  
  110.  
  111.         CopyMemory(lpNewFilePos,lpMapStartPos,dwBlockBytes);  
  112.         printf("\n分块文件%s成功生成。",sTempStr);  
  113.         CloseHandle(hNewFileMap);  
  114.         CloseHandle(hNewFile);  
  115.         UnmapViewOfFile(lpMapStartPos);  
  116.         UnmapViewOfFile(lpNewFilePos);  
  117.  
  118.     }  
  119.  
  120.     CloseHandle(hFileMap);  
  121.     CloseHandle(hFile);  
  122.     system("pause");  
  123.     return 0;  

 

你可能感兴趣的:(职场,休闲,内存映射,大文件分割)