Source: Windows file Segmentation and merging

Source: Internet
Author: User
Tags readfile

#include <Windows.h> #include <vector> #include <string>using namespace std;
Determine if the file exists bool Fileexistsw (const wstring &AMP;FN) {Win32_find_dataw fd;    HANDLE hfile = Findfirstfilew (Fn.c_str (), &AMP;FD);        if (hfile! = Invalid_handle_value) {:: FindClose (hfile);    return true; } return false;}    Determine if the directory exists bool DIRECTORYEXISTSW (const wstring &AMP;FN) {//Return Pathfileexistsa (Fn.c_str ());    DWORD Code = GETFILEATTRIBUTESW (Fn.c_str ()); Return (Code! = invalid_file_attributes) && ((File_attribute_directory & Code)! = 0);}    Directory + backslash wstring includetrailingpathdelimiterw (const wstring &path) {wstring s = path;    if (S.empty ()) return s;    if (S[s.length ()-1]! = L ' \ \ ') return s+l "\ \";  else return s;    }//gets the file name of the path wstring Extractfilenamew (const wstring &AMP;FILESTR) {if (Filestr.empty ()) return L ""; for (int i = Filestr.length ()-1; i>=0;-I.) {if (filestr[i] = = L ' \ \ ') {return filestr.subs        TR (i+1); }} return L "";} Std::wstring INTTOSTRW (constint i) {wchar_t buf[16]={0};    _itow_s (i,buf,10); Return wstring (BUF);} inline void incptr (void **p,int i) {*p = (void*) ((int) *p + i);} Split file bool Breakfile (const wchar_t *fn,unsigned long block_size,const wchar_t *save_path) {if (!    FILEEXISTSW (FN)) return false; if (!    DIRECTORYEXISTSW (Save_path)) return false;    if (Block_size < 1) return false;    HANDLE HF = Createfilew (fn,generic_read,file_share_read,null,open_existing,file_attribute_normal,0);        if (Invalid_handle_value = = HF) return false;    int iblock = 1;    Wstring Fblock = includetrailingpathdelimiterw (save_path) +extractfilenamew (FN) +l ". Part" +INTTOSTRW (Iblock); HANDLE HFW = Createfilew (Fblock.c_str (), generic_read|    generic_write,0,null,create_new,file_attribute_normal,0);        if (Invalid_handle_value = = HFW) {CloseHandle (HF);    return false;    } DWORD dwwrited = 0,dwwritedtemp = 0;    DWORD dwtowrite = 0;    DWORD dwremain = 0;    unsigned char buf[8192];DWORD dwreaded = 0;    int iseek = 0;    void *p = NULL;        while (true) {dwreaded = 0;        if (FALSE = = ReadFile (hf,buf,sizeof (BUF), &dwreaded,null)) break;        if (0 = = dwreaded) break;        iseek = 0;        p = (void*) buf;            Dwremain = Dwreaded;label_rewrite:if (dwwrited >= block_size) {:: CloseHandle (HFW);            ++iblock;            Fblock = Includetrailingpathdelimiterw (save_path) + Extractfilenamew (FN) +l ". Part" +INTTOSTRW (Iblock); HFW = Createfilew (Fblock.c_str (), generic_read|            generic_write,0,null,create_new,file_attribute_normal,0);                if (Invalid_handle_value = = HFW) {CloseHandle (HF);            return false;        } dwwrited = 0;        } incptr (&p,iseek);        dwtowrite = min (dwremain,block_size-dwwrited);        dwwritedtemp = 0;     if (FALSE = = WriteFile (hfw,p,dwtowrite,&dwwritedtemp,null)) break;   dwwrited + = dwwritedtemp;        if (Dwremain > dwwritedtemp) dwremain = dwremain-dwwritedtemp;        else Dwremain = 0;        iseek = dwwritedtemp;        if (Dwremain > 0) goto label_rewrite;    dwreaded = 0;    }:: CloseHandle (HFW);    :: CloseHandle (HF); return true;} Merge file bool Combinefiles (const vector<wstring> &files,const wstring &destfile) {if (FILEEXISTSW (destfile    )) return false;    if (Files.empty ()) return false; HANDLE HF = Createfilew (Destfile.c_str (), generic_read|    generic_write,0,null,create_new,file_attribute_normal,0);    if (Invalid_handle_value = = HF) return false;    HANDLE HFR = Invalid_handle_value;    unsigned char buf[8192];    DWORD dwreaded = 0,dwtowrite = 0,dwwrited = 0,dwremain = 0,dwwritedtemp = 0;    Longlong dwfilesize = 0,dwdestsize = 0;    Large_integer Li;    void *p=null;    int iseek = 0; for (size_t i = 0; I<files.size (); ++i) {if (! Fileexistsw (FILes[i]) continue;        HFR = Createfilew (Files[i].c_str (), generic_read,file_share_read,null,open_existing,file_attribute_normal,0);                if (Invalid_handle_value = = HFR) continue; Li.        QuadPart = 0;            if (FALSE = = GetFileSizeEx (Hfr,&li)) {CloseHandle (HF);        return false; } dwdestsize + = li.            QuadPart; } li.    QuadPart = dwdestsize;        if (FALSE = = SetFilePointerEx (hf,li,null,file_begin)) {CloseHandle (HF);    return false;        } if (FALSE = = SetEndOfFile (HF)) {CloseHandle (HF);    return false;    } CloseHandle (HF); HF = Createfilew (Destfile.c_str (), generic_read|    generic_write,0,null,open_existing,file_attribute_normal,0);        if (Invalid_handle_value = = HF) {HF = 0;    return false;    } HFR = Invalid_handle_value; for (size_t i = 0; I<files.size (); ++i) {if (!        FILEEXISTSW (Files[i]) continue; if (invalid_Handle_value! = HFR) CloseHandle (HFR);        HFR = Createfilew (Files[i].c_str (), generic_read,file_share_read,null,open_existing,file_attribute_normal,0);        if (Invalid_handle_value = = HFR) continue;label_read:dwreaded = 0;        if (FALSE = = ReadFile (hfr,buf,sizeof (BUF), &dwreaded,null)) continue;        if (dwreaded = = 0) continue;        iseek = 0;        p = (void*) buf;        Dwremain = Dwreaded;label_rewrite:incptr (&p,iseek);        Dwtowrite = Dwremain;        dwwritedtemp = 0;                if (FALSE = = WriteFile (hf,p,dwtowrite,&dwwritedtemp,null)) continue;        dwwrited + = dwwritedtemp;        if (Dwremain > dwwritedtemp) dwremain = Dwremain = Dwwritedtemp;        else Dwremain = 0;        iseek = dwwritedtemp;           if (dwremain>0) goto label_rewrite;    Goto Label_read;    } if (INVALID_HANDLE_VALUE!=HFR) CloseHandle (HFR); ClosehanDLE (HF); return true;}

Related Article

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.