Pnig0s p.s:大文件分割,使用內存映射文件,比傳統分割方式快很多,目前支持最大3G文件的分割,下個版本改進支持64位最大16EB的文件。其實主要就是實踐了下內存映射文件的方法。小Demo,寫的糙了點兒,偶也是自己來分割Sql文件用的,莫笑。
使用方法在-h裡寫的很清楚了,大小是按KB來計算的,使用時稍微注意下。
- #include <stdio.h>
- #include <iostream>
- #include <Windows.h>
- #include <time.h>
- #include "head.h"
- using namespace std;
- int main(int argc,char * argv[]){
- HANDLE hInFile = INVALID_HANDLE_VALUE; //remain
- HANDLE hInMap;
- FILEINFO fi;
- BOOL bHelp = FALSE;
- DWORD dwPartSize;
- for(int i = 0;i<argc;i++){
- if(argc == 1){
- bHelp = TRUE;
- }
- LPSTR lpChkCmd= argv[i];
- LPSTR lpValue = argv[i+1];
- if(lstrcmp(lpChkCmd,"-f") == 0){
- fi.lpFileName = lpValue;
- }else if(lstrcmp(lpChkCmd,"-s") == 0){
- dwPartSize = atol(lpValue)*1024;
- }else if(lstrcmp(lpChkCmd,"-h") == 0){
- bHelp = TRUE;
- }
- }
- if(bHelp){
- printf("\n --------------------------------");
- printf("\n|\tLarge file dividing\t|");
- printf("\n|\t\tby:Pnig0s1992\t|");
- printf("\n|\t\t2011,11,25\t|");
- printf("\n --------------------------------");
- printf("\nUsage:\n"
- " %s -f div.txt -s 1024 \n"
- "Options:\n"
- " -f Specify the file u want to divide.\n"
- " -s Specify the size for each part(KB).\n"
- " -h Display the usage.\n",argv[0]);
- exit(1);
- }
- hInFile = CreateFile(fi.lpFileName,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
- if(hInFile == INVALID_HANDLE_VALUE){
- printf("\n打開文件失敗(%d).",GetLastError());
- return 0;
- }
- fi.dwFileLowSize = GetFileSize(hInFile,&fi.dwFileHighSize);
- fi.dwFileSize = fi.dwFileLowSize;
- hInMap = CreateFileMapping(hInFile,NULL,PAGE_READWRITE,0,0,NULL);
- if(hInMap == NULL){
- printf("\n創建源文件映射失敗(%d).",GetLastError());
- CloseHandle(hInFile);
- return 0;
- }
- fi.dwPartNum = fi.dwFileSize / dwPartSize;
- if((fi.dwFileSize % dwPartSize ) != 0 ){
- fi.dwPartNum+=1;
- }
- printf("\n文件大小為:%uKB\t分塊數為:%u\n",fi.dwFileSize/1024,fi.dwPartNum);
- DWORD dwCurAddr = 0;
- DWORD dwCurSize = 0;
- system("pause");
- DWORD dwBegin,dwFinish;
- dwBegin = GetTickCount();
- for(int index = 0;index<fi.dwPartNum;index++){
- dwCurSize = fi.dwFileSize - dwCurAddr;
- if(dwCurSize > dwPartSize){
- dwCurSize = dwPartSize;
- }
- LPVOID lpInMapContext;
- lpInMapContext = MapViewOfFile(hInMap,FILE_MAP_READ|FILE_MAP_WRITE,0,dwCurAddr,dwCurSize);
- if(!lpInMapContext){
- printf("\n創建源文件視圖失敗.(%d)",GetLastError());
- CloseHandle(hInFile);
- return 0;
- }
- dwCurAddr += dwCurSize;
- char cIndex[4];
- itoa(index+1,cIndex,10);
- HANDLE hOutFile;
- hOutFile = CreateFile(cIndex,GENERIC_ALL,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
- if(hOutFile == INVALID_HANDLE_VALUE){
- printf("\n創建目標文件失敗(%d).",GetLastError());
- return 0;
- }
- HANDLE hOutMap;
- hOutMap = CreateFileMapping(hOutFile,NULL,PAGE_READWRITE,0,dwCurSize,NULL);
- if(hOutMap == NULL){
- printf("\n創建目標文件映射失敗(%d).",GetLastError());
- return 0;
- }
- LPVOID lpOutMapContext;
- lpOutMapContext = MapViewOfFile(hOutMap,FILE_MAP_WRITE,0,0,0);
- if(hOutMap == NULL){
- printf("\n創建目標文件視圖失敗.(%d)",GetLastError());
- return 0;
- }
- CopyMemory(lpOutMapContext,lpInMapContext,dwCurSize);
- printf("\n[MSG]分塊%d完成.",index+1);
- UnmapViewOfFile(lpInMapContext);
- UnmapViewOfFile(lpOutMapContext);
- CloseHandle(hOutMap);
- CloseHandle(hOutFile);
- }
- CloseHandle(hInMap);
- CloseHandle(hInFile);
- dwFinish = GetTickCount();
- DWORD dwRunTime =dwFinish - dwBegin;
- printf("\n用時:%u毫秒",dwRunTime);
- return 1;
- }
本文出自 “About:Blank H4cking” 博客,請務必保留此出處http://pnig0s1992.blog.51cto.com/393390/724509