調用WinApi實現超大檔案分割

來源:互聯網
上載者:User

不多說了,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;  }

本文出自 “About:Blank H4cking” 部落格,請務必保留此出處http://pnig0s1992.blog.51cto.com/393390/809886

相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。