包含头文件 unrar.hpp 以及 unrar.lib 这是Splayer 旗下的项目
该测试是测试rar文件中的数据 与 你指定的文件进行比较
现在把代码贴出来
//不用解释
// Convert a std::wstring to a std::string
std::string WStringToString(const std::wstring& s)
{
char* ch;
UINT bytes = WideCharToMultiByte(CP_ACP, 0, s.c_str(), -1, NULL, 0,
NULL, NULL);
ch = new char[bytes];
if(ch)
bytes = WideCharToMultiByte(CP_ACP, 0, s.c_str(), -1, ch, bytes,
NULL, NULL);
std::string str = ch;
delete[] ch;
return str;
}
//进行二进制文件比较
int compare_filebinary(FILE* filehandle, unsigned long long filepos, char* buff, unsigned long buffsize, int debug)
{
char *fbuf = (char *)malloc(buffsize);
_fseeki64(filehandle, filepos, SEEK_SET);
fread_s(fbuf, buffsize, sizeof(char), buffsize, filehandle);
int ret = memcmp(buff, fbuf, buffsize);
if (debug)
{
for (int i = 0;i < 100;i++)
wprintf(L"%02x", (DWORD)buff[i] & 0xff);
wprintf(L"\n");
for (int i = 0;i < 100;i++)
wprintf(L"%02x", (DWORD)fbuf[i] & 0xff);
wprintf(L"\n");
}
free(fbuf);
return ret;
}
// 程序入口
{
HANDLE rar_handle=NULL;
std::wstring fn_rar=L"C:\\Users\\Administrator\\Desktop\\身份证\\shenfen.rar";
std::wstring tem_dir=L"C:\\Users\\Administrator\\Desktop\\身份证\\";
struct RAROpenArchiveDataEx ArchiveDataEx;
memset(&ArchiveDataEx,0,sizeof(ArchiveDataEx));
ArchiveDataEx.ArcNameW=(wchar_t*)fn_rar.c_str();
wprintf(L"\n %s",ArchiveDataEx.ArcNameW);
//打开文件
ArchiveDataEx.OpenMode=RAR_OM_EXTRACT;
ArchiveDataEx.CmtBuf=0;
rar_handle=RAROpenArchiveEx(&ArchiveDataEx);
const long buffsize=1000;
char testbuff[buffsize];
//文件打开成功
if(rar_handle)
{
wprintf(L"\n RAROpenArchiveEx open successed\n");
struct RARHeaderDataEx HeaderDataEx;
while(RARReadHeaderEx(rar_handle,&HeaderDataEx)==0) //循环 读取rar文件中的文件信息
{
//正常解压该文件
unsigned long long filesize=((unsigned long long)HeaderDataEx.UnpSizeHigh<<32)+HeaderDataEx.UnpSize;
int err=0;
std::wstring unp_tmpfile=tem_dir+HeaderDataEx.FileNameW;
err=RARProcessFileW(rar_handle,RAR_EXTRACT,(wchar_t*)tem_dir.c_str(),HeaderDataEx.FileNameW);
wprintf(L"RARProcessFileW to %s return %d size %lld %x %x\n", unp_tmpfile.c_str(), err, filesize, HeaderDataEx.UnpVer, HeaderDataEx.Method);
}
RARCloseArchive(rar_handle);
}
rar_handle=RAROpenArchiveEx(&ArchiveDataEx); //打开rar中的文件
if(rar_handle)
{
wprintf(L"\n RAROpenArchiveEx open successed\n");
struct RARHeaderDataEx HeaderDataEx;
HeaderDataEx.CmtBuf = NULL;
while (RARReadHeaderEx(rar_handle, &HeaderDataEx) == 0)//循环 操作rar文件中的文件
{
//文件大小
unsigned long long filesize = ((unsigned long long)HeaderDataEx.UnpSizeHigh << 32) + HeaderDataEx.UnpSize;
int err = 0;
std::wstring unp_tmpfile = tem_dir + HeaderDataEx.FileNameW;
//rar 提取
err = RARExtractChunkInit(rar_handle, HeaderDataEx.FileName);
if (err != 0)
{
wprintf(L"\n RARExtractChunkInit return error %d\n", err);
continue;
}
//打开提出来的文件
FILE* unp_filehandle = NULL;
err = _wfopen_s(&unp_filehandle, unp_tmpfile.c_str(), L"rb");
if (err)
{
wprintf(L"\n open extracted file fail %d %d\n", err, unp_filehandle);
continue;
}
// 顺序测试
int iExtractRet = 0;
unsigned long long fpos = 0;
//顺序遍历 比较文件
do
{
iExtractRet = RARExtractChunk(rar_handle, (char*)testbuff, buffsize);
// Compare
if (compare_filebinary(unp_filehandle, fpos, testbuff, iExtractRet, 0))
{
wprintf(L"\n Sequence compare difference found at %lld for %d\n", fpos, buffsize);
break;
}
else
wprintf(L"\n Sequence compare is same %lld %d\n", fpos, iExtractRet);
fpos += iExtractRet;
} while(iExtractRet > 0);
// 随机测试 随机遍历比较文件
for (int i = 0; i < 100; i++)
{
unsigned long long ll_pos = rand() * filesize/RAND_MAX;
RARExtractChunkSeek(rar_handle, ll_pos, SEEK_SET);
RARExtractChunk(rar_handle, (char*)testbuff, buffsize);
// Compare
if (compare_filebinary(unp_filehandle, ll_pos, testbuff, iExtractRet, 0))
{
wprintf(L"\n Random compare difference found at %lld\n", ll_pos);
break;
}
else
wprintf(L"\n Random compare is same %lld\n", ll_pos);
}
wprintf(L"RARExtractChunk test for %s finished\n", unp_tmpfile.c_str());
}
RARCloseArchive(rar_handle);
}
wprintf(L"\n Test finished\n");
}
该测试是测试rar文件中的数据 与 你指定的文件进行比较
现在把代码贴出来
//不用解释
// Convert a std::wstring to a std::string
std::string WStringToString(const std::wstring& s)
{
char* ch;
UINT bytes = WideCharToMultiByte(CP_ACP, 0, s.c_str(), -1, NULL, 0,
NULL, NULL);
ch = new char[bytes];
if(ch)
bytes = WideCharToMultiByte(CP_ACP, 0, s.c_str(), -1, ch, bytes,
NULL, NULL);
std::string str = ch;
delete[] ch;
return str;
}
//进行二进制文件比较
int compare_filebinary(FILE* filehandle, unsigned long long filepos, char* buff, unsigned long buffsize, int debug)
{
char *fbuf = (char *)malloc(buffsize);
_fseeki64(filehandle, filepos, SEEK_SET);
fread_s(fbuf, buffsize, sizeof(char), buffsize, filehandle);
int ret = memcmp(buff, fbuf, buffsize);
if (debug)
{
for (int i = 0;i < 100;i++)
wprintf(L"%02x", (DWORD)buff[i] & 0xff);
wprintf(L"\n");
for (int i = 0;i < 100;i++)
wprintf(L"%02x", (DWORD)fbuf[i] & 0xff);
wprintf(L"\n");
}
free(fbuf);
return ret;
}
// 程序入口
{
HANDLE rar_handle=NULL;
std::wstring fn_rar=L"C:\\Users\\Administrator\\Desktop\\身份证\\shenfen.rar";
std::wstring tem_dir=L"C:\\Users\\Administrator\\Desktop\\身份证\\";
struct RAROpenArchiveDataEx ArchiveDataEx;
memset(&ArchiveDataEx,0,sizeof(ArchiveDataEx));
ArchiveDataEx.ArcNameW=(wchar_t*)fn_rar.c_str();
wprintf(L"\n %s",ArchiveDataEx.ArcNameW);
//打开文件
ArchiveDataEx.OpenMode=RAR_OM_EXTRACT;
ArchiveDataEx.CmtBuf=0;
rar_handle=RAROpenArchiveEx(&ArchiveDataEx);
const long buffsize=1000;
char testbuff[buffsize];
//文件打开成功
if(rar_handle)
{
wprintf(L"\n RAROpenArchiveEx open successed\n");
struct RARHeaderDataEx HeaderDataEx;
while(RARReadHeaderEx(rar_handle,&HeaderDataEx)==0) //循环 读取rar文件中的文件信息
{
//正常解压该文件
unsigned long long filesize=((unsigned long long)HeaderDataEx.UnpSizeHigh<<32)+HeaderDataEx.UnpSize;
int err=0;
std::wstring unp_tmpfile=tem_dir+HeaderDataEx.FileNameW;
err=RARProcessFileW(rar_handle,RAR_EXTRACT,(wchar_t*)tem_dir.c_str(),HeaderDataEx.FileNameW);
wprintf(L"RARProcessFileW to %s return %d size %lld %x %x\n", unp_tmpfile.c_str(), err, filesize, HeaderDataEx.UnpVer, HeaderDataEx.Method);
}
RARCloseArchive(rar_handle);
}
rar_handle=RAROpenArchiveEx(&ArchiveDataEx); //打开rar中的文件
if(rar_handle)
{
wprintf(L"\n RAROpenArchiveEx open successed\n");
struct RARHeaderDataEx HeaderDataEx;
HeaderDataEx.CmtBuf = NULL;
while (RARReadHeaderEx(rar_handle, &HeaderDataEx) == 0)//循环 操作rar文件中的文件
{
//文件大小
unsigned long long filesize = ((unsigned long long)HeaderDataEx.UnpSizeHigh << 32) + HeaderDataEx.UnpSize;
int err = 0;
std::wstring unp_tmpfile = tem_dir + HeaderDataEx.FileNameW;
//rar 提取
err = RARExtractChunkInit(rar_handle, HeaderDataEx.FileName);
if (err != 0)
{
wprintf(L"\n RARExtractChunkInit return error %d\n", err);
continue;
}
//打开提出来的文件
FILE* unp_filehandle = NULL;
err = _wfopen_s(&unp_filehandle, unp_tmpfile.c_str(), L"rb");
if (err)
{
wprintf(L"\n open extracted file fail %d %d\n", err, unp_filehandle);
continue;
}
// 顺序测试
int iExtractRet = 0;
unsigned long long fpos = 0;
//顺序遍历 比较文件
do
{
iExtractRet = RARExtractChunk(rar_handle, (char*)testbuff, buffsize);
// Compare
if (compare_filebinary(unp_filehandle, fpos, testbuff, iExtractRet, 0))
{
wprintf(L"\n Sequence compare difference found at %lld for %d\n", fpos, buffsize);
break;
}
else
wprintf(L"\n Sequence compare is same %lld %d\n", fpos, iExtractRet);
fpos += iExtractRet;
} while(iExtractRet > 0);
// 随机测试 随机遍历比较文件
for (int i = 0; i < 100; i++)
{
unsigned long long ll_pos = rand() * filesize/RAND_MAX;
RARExtractChunkSeek(rar_handle, ll_pos, SEEK_SET);
RARExtractChunk(rar_handle, (char*)testbuff, buffsize);
// Compare
if (compare_filebinary(unp_filehandle, ll_pos, testbuff, iExtractRet, 0))
{
wprintf(L"\n Random compare difference found at %lld\n", ll_pos);
break;
}
else
wprintf(L"\n Random compare is same %lld\n", ll_pos);
}
wprintf(L"RARExtractChunk test for %s finished\n", unp_tmpfile.c_str());
}
RARCloseArchive(rar_handle);
}
wprintf(L"\n Test finished\n");
}