vc++开启文件ntfs压缩属性

bool ATMHelper::NTFSCompression(LPCSTR szFilePath, bool bCmprs)
{
    CFilePath filepath(szFilePath);
    HANDLE hFile = ::CreateFile(filepath
        ,GENERIC_READ|GENERIC_WRITE
        ,FILE_SHARE_READ|FILE_SHARE_WRITE
        ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
    if (hFile==INVALID_HANDLE_VALUE) return false;
    
    USHORT us = 0;
    DWORD	dwSizeRet = 0;
    BOOL ret=::DeviceIoControl((HANDLE)hFile,FSCTL_GET_COMPRESSION,NULL,0,&us,sizeof(us),&dwSizeRet,NULL);
    do
    {
        if(!ret || dwSizeRet==0)	break;
        
        //不压缩
        if (!bCmprs && us== COMPRESSION_FORMAT_NONE)	break;
        
        //已压缩
        if (bCmprs && us!= COMPRESSION_FORMAT_NONE) break;
        
        if (bCmprs )	us = COMPRESSION_FORMAT_LZNT1;
        else        us = COMPRESSION_FORMAT_NONE;
        
        //设置/取消压缩
        ret= ::DeviceIoControl((HANDLE)hFile, FSCTL_SET_COMPRESSION, &us, sizeof(us),NULL,0, &dwSizeRet, NULL);
    } while (false);
    
    ::CloseHandle(hFile);
    return ret;
}

 

发表评论

电子邮件地址不会被公开。