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;
- }
附件:http://down.51cto.com/data/2359360
本文转hackfreer51CTO博客,原文链接:http://blog.51cto.com/pnig0s1992/724509,如需转载请自行联系原作者