⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bzip.cpp

📁 手机数据备份软件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Bzip.cpp: implementation of the CBzip class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "resource.h"
#include "CeFileFind.h"
#include "BaseType.h"
//#include "testdlg.h"

#define BZ_IMPORT
#include "Bzip.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

#define MAX_SIZE 0X1000


//#define NON_OK_FLAG             MAX_PATH;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CBzip::CBzip()
{
	m_hDllInstance	= NULL;
	m_bHasZipFlag   = FALSE;
	m_FirstDirFlag  = FALSE;
}

CBzip::~CBzip()
{
	if(m_hDllInstance != NULL)
		FreeLibrary(m_hDllInstance);
	m_hDllInstance = NULL;
}
//////////////////////////////////////////////////////////////////
// Initialize a dll and load a dll refer to szBzipDllName,it must 
// be called before all operation!!!
// PARAMETER: szBzipDllName ---- compress/decompress dll name 
// RETURN VALUE: 
//     TRUE if Initizlize successfully,otherwise FALSE
// AUTOTHOR: YANXINHAI (yanxh@mic.com.tw)
// VERSION: 1.0        
//////////////////////////////////////////////////////////////////
BOOL CBzip::Initialize(LPCTSTR szBzipDllName)
{
	if(m_hDllInstance != NULL)
		return TRUE;
	m_hDllInstance = LoadLibrary(szBzipDllName);
	if(m_hDllInstance == NULL)
	{
		DWORD dwError = GetLastError();
		return FALSE;
	}

	bzopen =(void * (WINAPI *)(const char *,const char *))GetProcAddress(m_hDllInstance,TEXT("bzopen"));
	bzread =(int (WINAPI *)(void *,void *,int ))GetProcAddress(m_hDllInstance,TEXT("bzread"));
	bzwrite=(int (WINAPI *)(void *,void *,int ))GetProcAddress(m_hDllInstance,TEXT("bzwrite"));
	bzclose=(void (WINAPI *)(void * ))GetProcAddress(m_hDllInstance,TEXT("bzclose"));
	bzBuffToBuffCompress   = (int (WINAPI *) ( char*,unsigned int*,char*,unsigned int,int,int,int))GetProcAddress(m_hDllInstance,TEXT("bzBuffToBuffCompress"));
    bzBuffToBuffDecompress = (int (WINAPI *) ( char*,unsigned int*,char*, unsigned int,int,int))GetProcAddress(m_hDllInstance,TEXT("bzBuffToBuffDecompress"));

	return TRUE;
}// End of Initialize

//////////////////////////////////////////////////////////////////
// Compress a file as special level same as bzip2
// PARAMETER:
//		 szSource ---- source name to be compressed 
//       szTarget ---- target name 
//       nLevel   ---- Compress level,pls bzip2 in Linux
// RETURN VALUE: 
//     TRUE if Compress successfully,otherwise FALSE
// AUTOTHOR: YANXINHAI (yanxh@mic.com.tw)
// VERSION: 1.0        
//////////////////////////////////////////////////////////////////
BOOL CBzip::Compress(LPCSTR szSource, LPCSTR szTarget,int nLevel/*=9*/)
{	
	ASSERT((szSource != NULL) && (szTarget != NULL));
	if((szSource == NULL) || (szTarget == NULL))
		return FALSE;
	
	FILE *fp_r = fopen(szSource,"rb");	
	if(fp_r == NULL)
		return FALSE;

	char mode[5];
	mode[0]='w';
	mode[1] = '0' + nLevel;
	mode[2] = '\0';

	void *BZ2fp_w = bzopen(szTarget,mode);
	if(BZ2fp_w == NULL)
	{
		fclose(fp_r);
		return FALSE;
	}

	char buff[MAX_SIZE];
	int nLen = 1; 
	while(!feof(fp_r))
	{
		nLen = fread(buff,1,MAX_SIZE,fp_r);
		bzwrite(BZ2fp_w,buff,nLen);
	}

	bzclose(BZ2fp_w);
	fclose(fp_r);
	
	return TRUE;
}// End of compress

//////////////////////////////////////////////////////////////////
// Decompress a file as special level same as bzip2
// PARAMETER:
//		 szSource ---- source name being compressed 
//       szTarget ---- target name 
// RETURN VALUE: 
//     TRUE if DeCompress successfully,otherwise FALSE
// AUTOTHOR: YANXINHAI (yanxh@mic.com.tw)
// VERSION: 1.0        
//////////////////////////////////////////////////////////////////
BOOL CBzip::DeCompress(LPCSTR szSource, LPCSTR szTarget)
{
	ASSERT((szSource != NULL) && (szTarget != NULL));
	if((szSource == NULL) || (szTarget == NULL))
		return FALSE;
	
	FILE *fp_w = fopen(szTarget,"wb");
	if(fp_w == NULL)
		return FALSE;
	
	void *BZ2fp_r = bzopen((LPCSTR)szSource,"rb");
	if(BZ2fp_r == NULL)
	{
		fclose(fp_w);
		return FALSE;
	}
	
	int nLen = 1;
	char buff[MAX_SIZE];
	while(nLen >0)
	{
		nLen=bzread(BZ2fp_r,buff,MAX_SIZE);
		fwrite(buff,1,nLen,fp_w);
	}
	bzclose(BZ2fp_r);
	fclose(fp_w);
	
	return TRUE;
}// End of Decompress

//////////////////////////////////////////////////////////////////
// Compress a buffer to another buffer as special level same as bzip2
// PARAMETER:
//		 szDestBuf ---- Target buffer being compressed 
//		 nDestLen  ---- size of Target buffer	
//       szSource  ---- Source buffer to be compressed
//       nSourceLen---- size of Source buffer
//		 nLevel    ---- compress level
// RETURN VALUE: 
//     TRUE if BufferCompress successfully,otherwise FALSE
// AUTOTHOR: YANXINHAI (yanxh@mic.com.tw)
// VERSION: 1.0        
//////////////////////////////////////////////////////////////////
BOOL CBzip::BufferCompress(char *szDestBuf, UINT *nDestLen, char *szSource, UINT nSourceLen, int nLevel)
{
   return (BZ_OK == bzBuffToBuffCompress(szDestBuf,nDestLen,szSource, nSourceLen, nLevel,4,250));
}//End of BufferCompress

//////////////////////////////////////////////////////////////////
// Decompress a buffer to another buffer 
// PARAMETER:
//		 szDestBuf ---- Target buffer being compressed 
//		 nDestLen  ---- size of Target buffer	
//       szSource  ---- Source buffer to be compressed
//       nSourceLen---- size of Source buffer
// RETURN VALUE: 
//     TRUE if BufferDeCompress successfully,otherwise FALSE
// AUTOTHOR: YANXINHAI (yanxh@mic.com.tw)
// VERSION: 1.0        
//////////////////////////////////////////////////////////////////
BOOL CBzip::BufferDeCompress(char *szDestBuf, UINT *nDestLen, char *szSource, UINT nSourceLen)
{
   return (BZ_OK == bzBuffToBuffDecompress(szDestBuf,nDestLen,szSource, nSourceLen, 0,0));
}//End of BufferDecompress

//////////////////////////////////////////////////////////////////
// Compress a file to buffer 
// PARAMETER:
//		 szFileName ---- file to be compressed
//		 buffer		---- Target buffer	
//       nLen		---- first it is original size of file,
//					  	 if successfully,compressed size
// RETURN VALUE: 
//     TRUE if ZipFileToBuffer successfully,otherwise FALSE
// AUTOTHOR: YANXINHAI (yanxh@mic.com.tw)
// VERSION: 1.0        
//////////////////////////////////////////////////////////////////
BOOL CBzip::ZipFileToBuffer(LPCSTR szFileName, char *buffer, int& nLen)
{
	ASSERT((szFileName != NULL)&&(buffer != NULL));
	if((szFileName == NULL)||(buffer== NULL)) 
		return FALSE;
	LPCSTR szTmpFile = "\\TEMP\\FILE0.TMP";
	CString szTmp(szTmpFile);
	BOOL bSuccess = Compress(szFileName,szTmpFile);
	if(!bSuccess)
		return FALSE;
	CFile file;
//	TRY
//	{
	CFileException e;
	if (file.Open(szTmp,CFile::modeRead,&e))
	{
		ASSERT(file.m_hFile != CFile::hFileNull);
		nLen = file.GetLength();
//		file.ReadHuge(buffer, nLen);
		int nBlockLen = 0;
		int nTmpLen = 0;
		do
		{
			nTmpLen =file.ReadHuge(buffer+nBlockLen,4096);
			nBlockLen +=nTmpLen; 
		}while(nTmpLen==4096);
		buffer[nLen+1] = '\0';
		file.Close();
		::DeleteFile(szTmp);
	}
//	CATCH(CFileException, e)
	else
	{
//		e->Delete();
//		file.Close();
		e.ReportError();
		::DeleteFile(szTmp); 
		return FALSE;
	}
//	END_CATCH
	return TRUE;
}//End of ZipFileToBuffer

////////////////////////////////////////////////////////////////////////////////////////
// Extract the archive 
// PARAMETER:
//	     szZipFileName ----- compressed file to be decompressed
// RETURN VALUE: 
//     TRUE if ExtractArchive successfully,otherwise FALSE
// AUTOTHOR: YANXINHAI (yanxh@mic.com.tw)
// VERSION: 1.0        
//////////////////////////////////////////////////////////////////
BOOL CBzip::ExtractArchive(LPCTSTR szZipFileName)
{
   CFile cExtract;
   CFileException e;
   BOOL bSuccess;
//   TRY
//   {
	   bSuccess = cExtract.Open(szZipFileName,CFile::modeRead,&e);
	   if(!bSuccess)
	   {
		   e.ReportError();
		   return FALSE;
	   }
//   }   
//   CATCH(CFileException,e)
//   {
//	   e->ReportError(); 
//	   e->Delete();
//	   return FALSE;
//   }
//   END_CATCH
   
   int nEofFlag = sizeof(DWORD)+sizeof(WORD);
   cExtract.Seek(-nEofFlag,CFile::end);
   DWORD dwWholeLen;
   cExtract.Read(&dwWholeLen,sizeof(DWORD));
   WORD  wOK;
   cExtract.Read(&wOK,sizeof(WORD));
   if(wOK != 0x4B4F)
   {
	  CString Msg;
	  Msg.Format(MSG_FILENOOK,szZipFileName);
	  SetCursor(LoadCursor(NULL, IDC_ARROW));  
	  if (MessageBox(NULL,Msg, RESTORETITLE,MB_YESNO|MB_ICONERROR) == IDNO)
	  {
		cExtract.Close();
		return FALSE;
	  }
	  else
	  {
		  SetCursor(LoadCursor(NULL, IDC_WAIT)); 
		  cExtract.Close();
		  return TRUE;
	  }
   }
	   
   cExtract.SeekToBegin();
   BOOL bEof = FALSE;
   char buf[4];
   memset(buf,0,4);
   DWORD nLen = cExtract.Read(buf,3);
   if(strcmp(buf,"BZ2") != 0)
   {
	   //Error format file
	   cExtract.Close();
	   return FALSE;
   }   
   CString usedFile,filename;
   while(!bEof)
   {
      BZFILEDATA bzFileData;
	  ZeroMemory(&bzFileData,sizeof(bzFileData));
	  // Get the File Information
	  nLen = 3*(sizeof(DWORD)+sizeof(FILETIME))+sizeof(WORD);
	  if(cExtract.ReadHuge(&bzFileData,nLen)<nLen)
	  {
		  bEof = TRUE;
	  	  continue;	
	  }
	  cExtract.ReadHuge(bzFileData.szFilePathName,bzFileData.wRealFilePathNameLen+1);
	  if(bzFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
	  {
		  CString str(bzFileData.szFilePathName);
		  CreateDir(CString(bzFileData.szFilePathName));
		  continue;
	  }

//	  char *szFileData = new char [bzFileData.dwZipFileSize]; 
	  char *szFileData = new char [bzFileData.dwOriginFileSize];
	  ASSERT(szFileData != NULL);
	  if(szFileData == NULL)
	  {
		  cExtract.Close();
		  return FALSE;
	  }
      //Get the compressed data to buffer
//	  nLen = cExtract.ReadHuge(szFileData,bzFileData.dwZipFileSize);
//	  if(nLen < bzFileData.dwZipFileSize)	  
	  nLen = cExtract.ReadHuge(szFileData,bzFileData.dwOriginFileSize);
	  if(nLen < bzFileData.dwOriginFileSize)
	  {
		  bEof = TRUE;
	  	  delete [] szFileData;
		  continue;
	  }	  

	  bSuccess = ExtractBufferToFile(bzFileData.szFilePathName,szFileData,bzFileData.dwOriginFileSize,&bzFileData); 
	  if(!bSuccess)
	  {
		 filename = bzFileData.szFilePathName;
		 usedFile.Format(MSG_UNKNOWN_ERROR,filename);
		 MessageBox(NULL,usedFile, RESTORETITLE,MB_OK| MB_ICONERROR);
	     cExtract.Close();
	     delete [] szFileData;	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -