首页 > 代码库 > 源码:windows文件分割与合并

源码: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 =http://www.mamicode.com/= hf)>