源碼:windows檔案分割與合并

來源:互聯網
上載者:User

標籤:windows 檔案讀寫 檔案分割 合并

#include <Windows.h>#include <vector>#include <string>using namespace std;

//判斷檔案是否存在bool FileExistsW(const wstring &fn){    WIN32_FIND_DATAW fd;    HANDLE hFile = FindFirstFileW(fn.c_str(),&fd);    if (hFile != INVALID_HANDLE_VALUE)    {        ::FindClose(hFile);        return true;    }    return false;}//判斷目錄是否存在bool DirectoryExistsW(const wstring &fn){    //  return PathFileExistsA(fn.c_str());    DWORD Code = GetFileAttributesW(fn.c_str());    return (Code != INVALID_FILE_ATTRIBUTES) && ((FILE_ATTRIBUTE_DIRECTORY & Code) != 0);}//目錄+反斜線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;  }//擷取路徑的檔案名稱wstring ExtractFileNameW(const wstring &filestr){    if (filestr.empty())        return L"";    for(int i = filestr.length()-1; i>=0; --i)    {        if (filestr[i] == L'\\')        {            return filestr.substr(i+1);        }    }    return L"";}std::wstring IntToStrW( const int 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);}//分割檔案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;}//合并檔案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;}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.