Call WINAPI to implement large file segmentation

Source: Internet
Author: User
Tags implement printf

Not much said, 10g+ 's SQL Test segmentation success, each block size not more than 1G good.

Code by pnig0s1992//date:2012,3,18 #include <stdio.h> #include <Windows.h> #define MAXLENGTH  
    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) get file handle failed%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 File:%s size:%i64d KB, Lpfilename,uli.)  
        quadpart/1024);  
    printf ("\ n chunking Size:%u KB chunking:%u", (dwblockbytes/1024), fi.dwpartnum);  
    } HANDLE Hfilemap; Hfilemap = createfilemapping (hfile,null,page_readonly,0,0,null)///If the file fails if (Hfilemap = = NULL) {p rintf (\ n Create file Map object handle failed:%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) failed to map target file to memory:%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) Get the block file object handle failed:%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 get chunk file memory mapped object handle failed:%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) failed to map block file to memory:%d.  
            ", GetLastError ());  
            UnmapViewOfFile (Lpmapstartpos);  
            CloseHandle (HNEWFILEMAP);  
            CloseHandle (Hnewfile);  
            CloseHandle (HFILEMAP);  
            CloseHandle (hfile);  
        return-1;  
        } copymemory (Lpnewfilepos,lpmapstartpos,dwblockbytes); printf ("\ n block file%s was successfully generated.)  
        ", STEMPSTR);  
        CloseHandle (HNEWFILEMAP);  
        CloseHandle (Hnewfile);  
        UnmapViewOfFile (Lpmapstartpos);  
     
    UnmapViewOfFile (Lpnewfilepos);  
    } closehandle (Hfilemap);  
    CloseHandle (hfile); SystEM ("pause");  
return 0; }

This article is from the "About:blank h4cking" blog, please be sure to keep this source http://pnig0s1992.blog.51cto.com/393390/809886

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.