C++分割文件并进行BASE64编码

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
#include <string>
using namespace std;
string Encode(char* Data,long DataByte)
{
    //编码表
    const char EncodeTable[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    //返回值
    string strEncode;
    unsigned char Tmp[4]={0};
    for(long i=0;i<DataByte / 3;i++)
    {
        Tmp[1] = *Data++;
        Tmp[2] = *Data++;
        Tmp[3] = *Data++;
        strEncode+= EncodeTable[Tmp[1] >> 2];
        strEncode+= EncodeTable[((Tmp[1] << 4) | (Tmp[2] >> 4)) & 0x3F];
        strEncode+= EncodeTable[((Tmp[2] << 2) | (Tmp[3] >> 6)) & 0x3F];
        strEncode+= EncodeTable[Tmp[3] & 0x3F];
    }
    //对剩余数据进行编码
    int Mod=DataByte % 3;
    if(Mod==1)
    {
        Tmp[1] = *Data++;
        strEncode+= EncodeTable[(Tmp[1] & 0xFC) >> 2];
        strEncode+= EncodeTable[((Tmp[1] & 0x03) << 4)];
        strEncode+= "==";
    }
    else if(Mod==2)
    {
        Tmp[1] = *Data++;
        Tmp[2] = *Data++;
        strEncode+= EncodeTable[(Tmp[1] & 0xFC) >> 2];
        strEncode+= EncodeTable[((Tmp[1] & 0x03) << 4) | ((Tmp[2] & 0xF0) >> 4)];
        strEncode+= EncodeTable[((Tmp[2] & 0x0F) << 2)];
        strEncode+= "=";
    }
    return strEncode;
}
        CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,_T("All Files(*.*)|*.*|"));
        if(dlg.DoModal()==IDOK)
        {
            CFile m_splitFile;
            if(m_splitFile.Open(dlg.GetPathName(),CFile::modeRead | CFile::shareDenyWrite))
            {
                CString targetPath="D:";
                CString m_fileTitle=m_splitFile.GetFileName();
                DWORD FileLength=m_splitFile.GetLength();
                DWORD PartLength=1024*512;
                int nCount=1;
                CString strName;
                CFile wrFile;
                DWORD ReadBytes;
                char* pBuf=new char[PartLength];
                while((ReadBytes=m_splitFile.Read(pBuf,PartLength))>=PartLength)
                {
                    strName.Format("%s\\%s%d.enc",targetPath,m_fileTitle,nCount++);
                    wrFile.Open(strName,CFile::modeWrite | CFile::modeCreate);
                    string encStr=Encode(pBuf,ReadBytes);
                    wrFile.Write(encStr.c_str(),encStr.size());
                    wrFile.Flush();
                    wrFile.Close();
                }
                if(nCount==1 && ReadBytes<PartLength)
                {
                    strName.Format("%s\\%s.enc",targetPath,m_fileTitle,nCount++);
                    wrFile.Open(strName,CFile::modeWrite | CFile::modeCreate);
                    string encStr=Encode(pBuf,ReadBytes);
                    wrFile.Write(encStr.c_str(),encStr.size());
                    wrFile.Flush();
                    wrFile.Close();
                }
                m_splitFile.Close();
                delete pBuf;
            }
        }

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:C++霍夫曼编码(Huffman Coding)

下一篇: 根据Date获取x分钟前/x小时前/昨天/x天前/x个月前/x年前的iOS代码