程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> 調用WinApi實現超大文件分割

調用WinApi實現超大文件分割

編輯:關於C++

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved