C/C++解析二进制文件

一、判断文件后缀

1. C语言版本

(1)源码

const char * Filename = ".........";
char* dis = Filename + strlen(Filename) - 4;
if (strcmp(dis, ".bz2") == 0)
{
	/*对应的解析程序*/
}
else if (strcmp(dis, ".zip") == 0)
{
 	/*对应的解析程序*/
}
else
{
	/*对应的解析程序*/
}

2.C++语言版本

(1)源码

 暂时无

二、解析普通二进制文件

1.C语言版本

(1)源码

const char * Filename = ".........";
char * buffer = NULL;/*定义文件缓冲区*/
FILE * file = NULL;
file = fopen(Filename , "rb");
/*将文件指针偏移到文件末尾*/
fseek(file, 0, SEEK_END);
int size = ftell(file);
fclose(file);
file = NULL;
file = fopen(Filename, "rb");
ALLOT_MEM_CHAR(buffer, size, "memory allot error\n");
int filesize = fread(buffer, size, 1, file);
filesize *= size;
/*容错函数*/
READ_FILE_ERROR(filesize, "read this data fail\n");
fclose(file);
file = NULL;

(2)注意事项

1.如果 fread 函数返回值为 0 ;请使用C++版进行数据解析。
2.fread 函数返回值为 0 的具体原因暂时没有理清,大概了解。

2.C++语言版本

(1)源码

const char * Filename = ".........";
char * buffer = NULL;/*定义文件缓冲区*/
std::ifstream infile(Filename, std::ios::binary);
if (!infile.is_open())
{
   printf("connot open the model file: %s\n", Filename);
   return -1;
}
std::filebuf * pbuf = infile.rdbuf();
// 获取文件大小
long filesize = static_cast<long>((pbuf->pubseekoff(0, std::ios::end, std::ios::in)));
pbuf->pubseekpos(0, std::ios::in);
ALLOT_MEM_CHAR(buffer, filesize, "malloc error!\n");
// 读入文件内容
pbuf->sgetn((char*)buffer, filesize);
infile.close();
pbuf = NULL;

(2)注意事项

如果 fread 函数返回值为 0 ;请使用C++版进行数据解析。

三、解析zip二进制文件

1.C语言版本

(1)源码

#include "unzip.h"/*该头文件使用的是zip解析库中的*/
const char * Filename = ".........";
char * buffer = NULL;/*定义文件缓冲区*/
unzFile zFile = NULL;
zFile = unzOpen64(Filename);
unz_file_info finfo;
unzGetCurrentFileInfo(zFile, &finfo, NULL, 0, NULL, 0, NULL,0);
int filesize = finfo.uncompressed_size;
unzOpenCurrentFile(zFile);
/*容错函数*/
OPEN_FILE_ERROR(zFile, "bzFile is failed\n");
/*申请缓冲区空间*/
ALLOT_MEM_CHAR(buffer, filesize, "memory allot error\n");
unzReadCurrentFile(zFile, buffer, filesize);
unzCloseCurrentFile(zFile);
zFile = NULL;

(2)注意事项

  1. zip版本是 X64 release
  2. 调用 zip解析库

2.python语言版本

(1)源码

import zipfile
import os
import shutil
def unzip_file(path,newpath):
    filenames = os.listdir(path)#获取目录下所有文件名
    for filename in filenames:
        filepath = os.path.join(path,filename)
        zip_file = zipfile.ZipFile(filepath) #获取压缩文件
        newfilepath = filename.split(".",1)[0] #获取压缩文件的文件名
        newfilepath = os.path.join(path,newfilepath)
        for name in zip_file.namelist():# 解压文件
            zip_file.extract(name,newpath)
        zip_file.close()
if __name__ == '__main__':
    unzip_file(r'待解压的文件目录',r'解压后的文件目录')

(2)注意事项

需要安装对应的python库

四、解析bz2二进制文件

1.源码

#include "bzlib.h"/*该头文件使用的是bz2解析库中的*/
const char * Filename = ".........";
char * buffer = NULL;/*定义文件缓冲区*/
int bufferlen = 1024;/*注意int字节的大小*/
int bzErr = 0;
BZFILE* bzfile = NULL;
bzfile = BZ2_bzopen(Filename , "rb");
/*容错函数*/
OPEN_FILE_ERROR(bzfile, "open the file fail\n");
/*1024:事先定义好缓冲区的大小*/
ALLOT_MEM_CHAR(buffer, bufferlen, "memory allot error\n");
int filesize = BZ2_bzRead(&bzErr, bzfile, buffer, bufferlen);
READ_FILE_ERROR(filesize, "read this data fail\n");
BZ2_bzclose(bzfile);
bzfile = NULL;

2.注意事项

  1. bz2版本是 X64 release或者 X86 debug
  2. 调用 bz2解析库
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值