资源预览内容
第1页 / 共7页
第2页 / 共7页
第3页 / 共7页
第4页 / 共7页
第5页 / 共7页
第6页 / 共7页
第7页 / 共7页
亲,该文档总共7页全部预览完了,如果喜欢就下载吧!
资源描述
文档供参考,可复制、编制,期待您的好评与关注! /测试通过#include #include #include typedef unsigned int uint; typedef unsigned char byte; / 包文件中最大可容纳的文件个数 #define MAX_FILE_COUNT 10 / 全局包文件指针 FILE* g_pMasFile = NULL; / 资源包文件头结构 typedef struct SMaseFileHeader uint uFileFlag; / 包文件头标记: MASE uint uFileCount; / 包内文件个数 uint uFileListOfs; / 文件列表偏移 uint uMaxFileCount; / 最大子文件个数 uint uFileSize; / 包文件的大小 MaseHeader; / 包内文件信息结构 typedef struct SFilesMessage uint uFileOfs; / 本文件在包内的偏移 uint uFileSize; / 本文件的大小 char szFileName260; / 本文件的路径 FilesMsg; / 打开包文件 int OpenMasFile(const char* path, const byte onlyOpen) uint uWriteCount; / 写入文件信息次数 byte bIsNew = 0; / 是否新建的 MaseHeader header; / 文件头结构定义 FilesMsg msg; / 文件信息结构定义 g_pMasFile = fopen(path, rb); / 用来判断是否存在 if (g_pMasFile = NULL) if (onlyOpen = 1) / 只打开不新建 return -1; bIsNew = 1; g_pMasFile = fopen(path, wb); if (g_pMasFile = NULL) return -1; /先关闭,然后在用rb+方式打开 二进制读写打开文件 fclose( g_pMasFile ); g_pMasFile = fopen(path, rb+); if (g_pMasFile = NULL) return -1; if(bIsNew = 1)/ 新建的文件 header.uFileFlag = ESAM; header.uFileCount = 0; header.uFileListOfs = sizeof(MaseHeader); /紧跟着就是文件列表 header.uMaxFileCount = MAX_FILE_COUNT; header.uFileSize = sizeof(MaseHeader) + (MAX_FILE_COUNT * sizeof(FilesMsg); /写入头信息 fwrite(&header, sizeof(MaseHeader), 1, g_pMasFile); memset(&msg, 0, sizeof(FilesMsg); uWriteCount = MAX_FILE_COUNT; /写入文件列表用0占位 while(uWriteCount-) fwrite(&msg, sizeof(FilesMsg), 1, g_pMasFile); else/文件存在 /则读取头文件信息 fread(&header, sizeof(MaseHeader), 1, g_pMasFile); /检查文件头标记 if (header.uFileFlag != ESAM) fclose(g_pMasFile); printf(文件头标记不对,错误!n); return -1; /检查数据是否完整 if (header.uMaxFileCount != MAX_FILE_COUNT) fclose(g_pMasFile); printf(数据不完整,错误!n); return -1; return 0; /写文件到包里 int WriteFileToPak(const char* path) FilesMsg fileMsg; /此文件的文件信息结构 MaseHeader header; /包文件头结构定义 uint uFileSize; uint uFileListEndOfs; byte* pBuff; FILE* pFile = NULL; if (g_pMasFile = NULL) return -1; memset(&fileMsg, 0, sizeof(FilesMsg); fseek(g_pMasFile, 0, SEEK_SET); /定位到文件头,读取头文件信息 /则读取头文件信息 fread(&header,sizeof(MaseHeader), 1, g_pMasFile); uFileListEndOfs = header.uFileCount * sizeof(FilesMsg) + header.uFileListOfs; pFile = fopen(path, rb); if(pFile = NULL) return -1; fseek(pFile, 0, SEEK_END); uFileSize = ftell(pFile); fseek(pFile, 0, SEEK_SET); /文件名长度不能超过260 strcpy(fileMsg.szFileName,path); fileMsg.uFileOfs = header.uFileSize; fileMsg.uFileSize = uFileSize; / 写入文件信息 / 将文件指针定位到uFileListEndOfs处,以便写入新的文件信息结构 fseek(g_pMasFile, uFileListEndOfs, SEEK_SET); fwrite(&fileMsg,sizeof(FilesMsg),1,g_pMasFile); / 申请空间 pBuff = (byte*)malloc(uFileSize); fread(pBuff,uFileSize,1,pFile); / 写数据到包文件里 fseek(g_pMasFile,header.uFileSize,SEEK_SET); fwrite(pBuff, uFileSize, 1, g_pMasFile); / 释放内存 free(pBuff); /重新填充header header.uFileCount += 1; header.uFileSize += uFileSize; fseek( g_pMasFile,0,SEEK_SET); / 重新写入包文件头 fwrite(&header,sizeof(MaseHeader),1,g_pMasFile); return 0; /从包文件里读数据 int ReadFileFromPak(const FilesMsg msg, byte* _dst) if ( g_pMasFile = NULL ) return -1; fseek(g_pMasFile, msg.uFileOfs,SEEK_SET); fread(_dst, msg.uFileSize, 1, g_pMasFile); return 0; /获取包中某个文件的信息 int GetFileMessage( const char* path, FilesMsg* msg) FilesMsg fileMsg; / 此文件的文件信息结构 MaseHeader header; / 包头结构 uint uFileCount; / 文件个数 if ( g_pMasFile = NULL | msg = NULL ) return -1; / 则读取头文件信息 fseek(g_pMasFile, 0, SEEK_SET); fread(&header, sizeof(MaseHeader), 1, g_pMasFile); uFileCount = header.uFileCount; while (uFileCount-) fread(&fileMsg, sizeof(FilesMsg), 1,g_pMasFile); / 判断是否是要获取的文件 if (stricmp(fileMsg.szFileName, path) = 0 ) *msg = fileMsg; return 0; return -1; / 关闭包文件 int CloseMasFile( void ) if ( g_pMasFile = NULL ) return -1; fclose( g_pMasFile ); g_pMasFile = NULL; return 0; /这是打包主函数int main( void ) int ret; ret = OpenMasFile( E:PhotoPak.bin,0); if ( ret = -1 ) goto _exit; WriteFileToPak( E:珍贵.jpg ); WriteFileToPak( E:123.docx ); WriteFileToPak( E:456.txt
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号