经过之前的几次试验 决定使用json记录打包文件信息
#include "Package.h" #include "json/json.h" #include <string> #include <iostream> /************************************************************** 技术博客 http://www.cnblogs.com/itdef/ 技术交流群 群号码:324164944 欢迎c c++ windows驱动爱好者 服务器程序员沟通交流 **************************************************************/ bool Package::CreatePackage( const char * packFileName) { bool bRet = false ; fpPackage_ = fopen (packFileName, "wb+" ); if (NULL == fpPackage_) return bRet; bRet = true ; return bRet; } void Package::AddFileToList( const char * fileName) { fileList.push_back(fileName); } bool Package::OpenPackageForUnPack( const char * packFileName) { if (NULL != fpPackage_) fclose (fpPackage_); fpPackage_ = fopen (packFileName, "rb" ); if (NULL == fpPackage_) return false ; return true ; } bool Package::CopyFileContent( FILE * src, FILE * dsc) { char * buf = new char [CopyBufSize]; if (NULL == buf) return false ; while (1) { int nReadBytes = fread (buf,1, CopyBufSize, src); //写入包裹 fwrite (buf, 1, nReadBytes, dsc); //如果读取到的数据比缓冲区小,那么说明读取结束了 if (nReadBytes < sizeof (buf)) { break ; } } //fflush是确保我们的数据写入到磁盘上了 fflush (dsc); return true ; } bool Package::PackageOneFile( const char *name) { bool bRet = false ; FILE * fp = fopen (name, "rb" ); if (NULL == fp) return bRet; //获取文件长度 _fseeki64(fp, 0, SEEK_END); //定位到文件末 __int64 filelength = _ftelli64(fp); _fseeki64(fp, 0, SEEK_SET); //获取PACKAGE文件当前偏移 __int64 currentOffset = _ftelli64(fpPackage_); //开始打包 CopyFileContent(fp, fpPackage_); FILEINFO fileInfo; fileInfo.filename = name; fileInfo.fileSize = filelength; fileInfo.offsetInPackFile = currentOffset; fileInfoList.push_back(fileInfo); fclose (fp); return bRet; } bool Package::GetUnpackFileInfo() { fileInfoList.clear(); __int64 jsonOffset = 0; fread (&jsonOffset,1, sizeof (jsonOffset), fpPackage_); //获取文件长度 _fseeki64(fpPackage_, 0, SEEK_END); //定位到文件末 __int64 filelength = _ftelli64(fpPackage_); _fseeki64(fpPackage_, 0, SEEK_SET); _fseeki64(fpPackage_, jsonOffset, SEEK_SET); char * jsonBuf = new char [filelength - jsonOffset]; if (NULL == jsonBuf) return false ; fread (jsonBuf,filelength - jsonOffset,1, fpPackage_); //解析json字符串 Json::Reader pJsonParser;; Json::Value tempVal; if (!pJsonParser.parse(jsonBuf, tempVal)) { std::cout << "parse error" << std::endl; delete [] jsonBuf; return false ; } Json::Value array = tempVal[ "array" ]; for ( int i = 0; i < array.size(); i++) { std::cout << array[i][ "Filename" ].asString() << std::endl; std::cout << array[i][ "FileSize" ].asInt64() << std::endl; std::cout << array[i][ "Offset" ].asInt64() << std::endl; } delete [] jsonBuf; return true ; } bool Package::WriteFileInfoToPackFile() { //获取PACKAGE文件当前偏移 写入JSON文件信息的偏移 __int64 currentOffset = _ftelli64(fpPackage_); //生成JSON Json::Value root; Json::Value array; Json::FastWriter writer; Json::Value fileInfo; for (std::deque<FILEINFO>::iterator it = fileInfoList.begin(); it != fileInfoList.end(); it++) { fileInfo[ "Filename" ] = it->filename; fileInfo[ "FileSize" ] = it->fileSize; fileInfo[ "Offset" ] = it->offsetInPackFile; array.append(fileInfo); } root[ "array" ] = array; std::string s = writer.write(root); //写入文件信息 fwrite (s.c_str(),1,s.size(), fpPackage_); //跳转到文件头 写入JOSN偏移 _fseeki64(fpPackage_, 0, SEEK_SET); fwrite (¤tOffset,1, sizeof (currentOffset), fpPackage_); fflush (fpPackage_); return true ; } bool Package::PackFile() { bool bRet = false ; //预留 打包文件的信息的偏移位置 __int64 jsonOffset = 0; fwrite (&jsonOffset, 1, sizeof (jsonOffset), fpPackage_); for (std::deque<std::string>::iterator it = fileList.begin(); it != fileList.end(); it++) { std::string fileName = *it; PackageOneFile(fileName.c_str()); } fileList.clear(); WriteFileInfoToPackFile(); return bRet; } |
打包文件格式如下
文件信息格式类似
代码运行结果
具体代码见
http://www.oschina.net/code/snippet_614253_56938