解压7z文件windows代码.
2011-01-29 10:59
239 查看
#include "decode7z.h" #include <stdio.h> #include <string.h> #include <windows.h> #include <tchar.h> #include <atlbase.h> extern "C" { #include "7z/7z.h" #include "7z/7zAlloc.h" #include "7z/7zCrc.h" #include "7z/7zFile.h" #include "7z/7zVersion.h" } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // int cat_path( LPTSTR lpszPath, LPCWSTR lpSubPath ) { USES_CONVERSION; PathAppend( lpszPath, CW2CT( lpSubPath ) ); return 0; } int save_file( LPCTSTR lpszFile, const void * lpBuf, DWORD dwSize ) { int nRet = 0; HANDLE hFile = INVALID_HANDLE_VALUE; if( 0 == nRet ) { hFile = CreateFile( lpszFile, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0 ); if( INVALID_HANDLE_VALUE == hFile ) { nRet = 1; } } if( 0 == nRet ) { DWORD dwWrite = 0; WriteFile( hFile, lpBuf, dwSize, &dwWrite, 0 ); if( dwWrite != dwSize ) { nRet = 1; } } if( INVALID_HANDLE_VALUE != hFile ) { CloseHandle( hFile ); hFile = INVALID_HANDLE_VALUE; } return nRet; } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // int unzip_7z_file( LPCTSTR lpszInputFile, LPCTSTR lpszOutputPath ) { USES_CONVERSION; int nRet = 0; CFileInStream archiveStream = { 0 }; CLookToRead lookStream = { 0 }; CSzArEx db = { 0 }; ISzAlloc allocImp = { 0 }; ISzAlloc allocTempImp = { 0 }; UInt16 *temp = NULL; size_t tempSize = 0; UInt32 blockIndex = 0xFFFFFFFF; // it can have any value before first call (if outBuffer = 0) Byte *outBuffer = 0; // it must be 0 before first call for each new archive. size_t outBufferSize = 0; // it can have any value before first call (if outBuffer = 0) //////////////////////////////////////////////////////////// // init allocImp.Alloc = SzAlloc; allocImp.Free = SzFree; allocTempImp.Alloc = SzAllocTemp; allocTempImp.Free = SzFreeTemp; if( 0 == nRet ) { nRet = InFile_Open( &archiveStream.file, CT2CA( lpszInputFile ) ); } if( 0 == nRet ) { FileInStream_CreateVTable(&archiveStream); LookToRead_CreateVTable(&lookStream, False); lookStream.realStream = &archiveStream.s; LookToRead_Init(&lookStream); CrcGenerateTable(); SzArEx_Init(&db); } if( 0 == nRet ) { nRet = SzArEx_Open( &db, &lookStream.s, &allocImp, &allocTempImp ); } // 如果没有打开成功,就不会有文件 if( 0 == nRet ) { for( unsigned int i = 0; i < db.db.NumFiles; ++i ) { TCHAR szFile[MAX_PATH] = { 0 }; _tcscpy_s( szFile, lpszOutputPath ); size_t offset = 0; size_t outSizeProcessed = 0; const CSzFileItem * f = db.db.Files + i; size_t len = 0; len = SzArEx_GetFileNameUtf16( &db, i, NULL ); if( len > tempSize ) { SzFree( NULL, temp ); tempSize = len; temp = (UInt16 *)SzAlloc(NULL, tempSize * sizeof(temp[0])); if (temp == 0) { nRet = 1; break; } } SzArEx_GetFileNameUtf16( &db, i, temp ); cat_path( szFile, ( LPCWSTR )temp ); if( !f->IsDir ) { nRet = SzArEx_Extract( &db, &lookStream.s, i, &blockIndex, &outBuffer, &outBufferSize, &offset, &outSizeProcessed, &allocImp, &allocTempImp ); if( 0 != nRet ) { break; } } for( size_t j = 0; szFile[j] != 0; j++) { if( (szFile[j] == '/') || ( szFile[j] == '//' ) ) { szFile[j] = 0; CreateDirectory( szFile, 0 ); szFile[j] = CHAR_PATH_SEPARATOR; } } if( f->IsDir ) { CreateDirectory( szFile, 0 ); } else { nRet = save_file( szFile, outBuffer + offset, outSizeProcessed ); } if( 0 != nRet ) { break; } } IAlloc_Free( &allocImp, outBuffer ); } SzArEx_Free( &db, &allocImp ); SzFree( NULL, temp ); File_Close( &archiveStream.file ); return nRet; }
相关文章推荐
- 解压7z文件windows代码.
- Linux 自解压文件修正版代码
- Ubuntu Linux 安装 .7z 解压和压缩文件
- CentOS7——解压7z文件——p7zip
- ubuntu 7z 文件 解压
- 基于WDF的PCI/PCIe接口卡Windows驱动程序(3)- 驱动程序代码(头文件)
- [转]Ubuntu Linux 安装 .7z 解压和压缩文件
- 用U盘安装系统提示Windows 无法安装所需的文件。文件可能损坏或丢失错误代码:0x80070570解决办法
- 在windows下压缩的文件,在linux下解压会乱码的解决方案
- Windows IIS服务器.7z文件不能下载解决办法(转载)
- 利用windows计划任务实现定时运行ASP文件代码
- ubuntu下7z文件的解压方法
- 关于windows下C++实现解压、压缩 .tar.gz文件
- 在Windows命令行窗口中输入并运行PHP代码片段(不需要php文件)的方法
- VBS脚本代码(手工编写---在windows 7上调用系统对话框,来选择文件)
- CompressUtils相关代码,java中文件压缩或解压已经文件的基本操作等方法
- 利用windows计划任务实现定时运行ASP文件代码
- 基于visual c++之windows核心编程代码分析(64)现有的exe文件中添加自己的代码
- Windows写字板HTML帮助文件处理远程代码执行漏洞
- 升级Win10无法找到安装文件$WINDOWS.~BT 错误代码0x80240020的解决办法