如下代码内容是关于C++分割文件并进行BASE64编码的代码。
#include <string>
using namespace std;
{
const char EncodeTable[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
string strEncode;
unsigned char Tmp[4]={0};
for(long i=0;i<DataByte / 3;i++)
{
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)
{
strEncode+= EncodeTable[(Tmp[1] & 0xFC) >> 2];
strEncode+= EncodeTable[((Tmp[1] & 0x03) << 4)];
strEncode+= "==";
}
else if(Mod==2)
{
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;
}
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();
int nCount=1;
CString strName;
CFile wrFile;
DWORD ReadBytes;
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;
}
}
网友评论