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

📄 bzip.cpp

📁 手机数据备份软件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		 return FALSE;
	  }	  
	  OnExtractProgress(CString(bzFileData.szFilePathName),bzFileData.dwOriginFileSize);
	  delete [] szFileData;
   } 
   //eof 	
   cExtract.Close();
   return TRUE;	
}// End of ExtractArchive

/////////////////////////////////////////////////////////////////
// Extract the compressed buffer to the file
// PARAMETER:
//	     szFileName		----- target file to be filled
//		 buffer			----- buffer being compressed
//		 nLen			----- original size of file
//       pszFileBuffer  ----- origianl infomation of file, default is NULL
// RETURN VALUE: 
//     TRUE if ExtractBufferToFile successfully,otherwise FALSE
// AUTOTHOR: YANXINHAI (yanxh@mic.com.tw)
// VERSION: 1.0        
//////////////////////////////////////////////////////////////////
BOOL CBzip::ExtractBufferToFile(LPCTSTR szFileName, char *buffer,UINT nLen,BZFILEDATA *pszFileBuffer/*=NULL*/)
{
	ASSERT(buffer != NULL);
	if(buffer == NULL)
		return FALSE;

	CString szFN(szFileName);
	szFN = szFN.Left(pszFileBuffer->wRealFilePathNameLen);
	int nPahtRootLen = szFN.ReverseFind('\\');
	if(nPahtRootLen == -1)
		return FALSE;
	CString szFNPath = szFN.Left(nPahtRootLen);
	
	if(!CreateDir(szFNPath))
		return FALSE;

	BOOL bSuccess ;
	CFile file;
	CFileException Err;
	SetFileAttributes(szFN,FILE_ATTRIBUTE_ARCHIVE);
	bSuccess = file.Open(szFN,CFile::modeCreate|CFile::modeWrite,&Err);
	if(!bSuccess)
	{
		int tmp = GetLastError();
		if ((tmp == ERROR_SHARING_VIOLATION) || (tmp == ERROR_ACCESS_DENIED))
			return TRUE;
//		Err.ReportError();
		return FALSE;
	}

//	TRY
//	{
		if (file.Write(buffer,nLen) == -1)
		{
			file.Close();
			return FALSE;
		}
	    file.Close();
//	}
//	CATCH(CFileException,e)
//	{
//		e->ReportError();
//		e->Delete();
//		return FALSE;
//	}
//	END_CATCH

	if(pszFileBuffer != NULL)
    {
		// set the attributes of the file
		SetFileAttributes(szFN,pszFileBuffer->dwFileAttributes);
		// set the time of the file
	 	SetFileTime((HANDLE)(file.m_hFile),&pszFileBuffer->ftCreateTime,&pszFileBuffer->ftLastAccessedTime,&pszFileBuffer->ftLastModifiedTime); 		
	}
	return TRUE;
}//End of ExtractBufferToFile

/////////////////////////////////////////////////////////////////
// Compress a directory including subdirectory to the file as cFile opens
// PARAMETER:
//	     cFile		     ----- a VALIDATE target file handle,
//		 szDirectoryName ----- directory name being compressed
// RETURN VALUE: 
//     TRUE if  AddDirToArchive successfully,otherwise FALSE
// AUTOTHOR: YANXINHAI (yanxh@mic.com.tw)
// VERSION: 1.0        
//////////////////////////////////////////////////////////////////
BOOL CBzip::AddDirToArchive(CFile &cFile, LPCTSTR szDirectoryName)
{
	CString strDir(szDirectoryName);
	CCeFileFind m_FileFind;
	CString szPathName;

	if(!IsExistedDirectory(strDir))
	  return FALSE;
	if (IsEmptyDirectory(strDir))
	{
		m_FileFind.FindFile(strDir);
		szPathName = strDir;  
		int nLen = szPathName.GetLength();
		BZFILEDATA bfData;
		ZeroMemory(&bfData,sizeof(bfData));
		bfData.dwFileAttributes = m_FileFind.GetFirstFileAttribute();
		bfData.dwOriginFileSize = m_FileFind.GetLength();
		m_FileFind.GetCreationTime(&bfData.ftCreateTime); 
		m_FileFind.GetLastAccessTime(&bfData.ftLastAccessedTime);
		m_FileFind.GetLastWriteTime (&bfData.ftLastModifiedTime);
		bfData.wRealFilePathNameLen = nLen * 2;

//		char *buf = new char[nLen];
//		memset(buf,0,sizeof(buf));
//		wcstombs(buf,szPathName,nLen);
//		strcpy(bfData.szFilePathName,buf);
//		delete [] buf;
		wcscpy(bfData.szFilePathName,szPathName);
		if (!AddToArchive(cFile, bfData))
			return FALSE;
		return TRUE;

	}
	if( strDir.Right(1) != '\\' )
	  strDir += '\\';
	strDir += L"*.*";
	
	BOOL bFound = m_FileFind.FindFile(strDir);
	while(bFound)
	{
		bFound = m_FileFind.FindNextFile ();
		szPathName = m_FileFind.GetFilePath();
		BZFILEDATA bfData;
		ZeroMemory(&bfData,sizeof(bfData));
		bfData.dwFileAttributes = m_FileFind.GetFileAttributes();
		bfData.dwOriginFileSize = m_FileFind.GetLength();
		m_FileFind.GetCreationTime(&bfData.ftCreateTime); 
		m_FileFind.GetLastAccessTime(&bfData.ftLastAccessedTime);
		m_FileFind.GetLastWriteTime(&bfData.ftLastModifiedTime);
		
		int nLen = szPathName.GetLength();
		bfData.wRealFilePathNameLen = nLen * 2;
		
//		char *buf = new char[nLen];
//		memset(buf,0,sizeof(buf));
//		wcstombs(buf,szPathName,nLen);
//		strcpy(bfData.szFilePathName,buf);
//		delete [] buf;			
		wcscpy(bfData.szFilePathName,szPathName);
		
		if(m_FileFind.IsDirectory())
		{
			if(!IsEmptyDirectory(szPathName))//recursive call
			{
				if (!AddDirToArchive(cFile,szPathName))
					return FALSE;
			}
			else // empty directory
			{
				if (!AddToArchive(cFile, bfData))
					return FALSE;
			}
		}//end if(m_FileFind.IsDirectory())
		else // file 
		{
			if (!AddToArchive(cFile, bfData))
				return FALSE;
			OnExtractProgress(CString(bfData.szFilePathName),bfData.dwOriginFileSize);
		}
	}//end while 		

	return TRUE; // success
}// End of AddDirToArchive

/////////////////////////////////////////////////////////////////
// Compress file or empty directory to the file as cFile opens
// PARAMETER:
//	     cFile		     ----- a VALIDATE target file handle,
//		 szDirectoryName ----- file name or empty directory name being compressed
// RETURN VALUE: 
//     TRUE if  AddToArchive successfully,otherwise FALSE
// AUTOTHOR: YANXINHAI (yanxh@mic.com.tw)
// VERSION: 1.0        
//////////////////////////////////////////////////////////////////
BOOL CBzip::AddToArchive(CFile &cTargetFile, BZFILEDATA &pBFData)
{
	ASSERT(cTargetFile.m_hFile != CFile::hFileNull);
	if(cTargetFile.m_hFile == CFile::hFileNull)
		return FALSE;
   // write file flag to a compressed file
	if(!m_bHasZipFlag)
	{
		m_bHasZipFlag = TRUE;
		char buf[] = "BZ2";
		cTargetFile.Write(buf,3);
	}
	pBFData.szFilePathName[pBFData.wRealFilePathNameLen] = '\0';
	//if it is directory, only write header info into zip file 
	if(pBFData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
		cTargetFile.WriteHuge(&pBFData,3*(sizeof(DWORD)+sizeof(FILETIME))+sizeof(WORD)+pBFData.wRealFilePathNameLen +1 );
	else
	{
/*		int nLen = pBFData.dwOriginFileSize;
		char *buffer = new char[nLen];
		ASSERT(buffer != NULL);
		if(buffer == NULL)
			return FALSE;
		// compreess file to struct and nLen will be changed with real compressed size 
		ZipFileToBuffer(pBFData.szFilePathName,buffer,nLen);
		pBFData.dwZipFileSize = nLen;
		cTargetFile.WriteHuge(&pBFData,3*(sizeof(DWORD)+sizeof(FILETIME))+sizeof(WORD) + pBFData.wRealFilePathNameLen +1);											
		//Fill compressed data to data struct
		cTargetFile.WriteHuge(buffer,nLen);
		delete [] buffer;
		buffer = NULL;
		return TRUE;
*/
		cTargetFile.Write(&pBFData,3*(sizeof(DWORD)+sizeof(FILETIME))+sizeof(WORD)+pBFData.wRealFilePathNameLen +1 );
		return WriteFileToZip(CString(pBFData.szFilePathName),cTargetFile);
	}
	return TRUE;	
}//End of AddToArchive

/////////////////////////////////////////////////////////////////
// Create a directory (inner function)
// PARAMETER:
//		 szDirName ----- Directory name being created
// RETURN VALUE: 
//     TRUE if  CreateDir successfully,otherwise FALSE
// AUTOTHOR: YANXINHAI (yanxh@mic.com.tw)
// VERSION: 1.0        
//////////////////////////////////////////////////////////////////
BOOL CBzip::CreateDir(LPCTSTR szDirName)
{
	CString strDir(szDirName);
	// if directory is root,don't create,return true
	if (strDir.Compare(L"\\") == 0)
		return TRUE;

	//Create parent directories
	int nFound = strDir.Find(L"\\",1);
	while(nFound != -1)
	{
		if(!CreateDirectory(strDir.Left(nFound),NULL))
		{
			DWORD dwError = GetLastError();
			if(ERROR_NOT_ENOUGH_MEMORY == GetLastError())
			{
				return FALSE;
			}
		}
		nFound = strDir.Find(L"\\",nFound+1);
	}
 //avoid strDir to be created outside with other method
	if(!CreateDirectory(strDir,NULL))
	{
		if(ERROR_ALREADY_EXISTS == GetLastError())
			return TRUE;
		if(ERROR_NOT_ENOUGH_MEMORY == GetLastError())
			return FALSE;
//		return FALSE;
	}
	return TRUE;
}//End of CreateDir

/////////////////////////////////////////////////////////////////
// Sure if a directory is empty without children
// PARAMETER:
//		 szDirectory ----- Directory name being queried
// RETURN VALUE: 
//     TRUE if  szDirectory is empty,otherwise FALSE
// AUTOTHOR: YANXINHAI (yanxh@mic.com.tw)
// VERSION: 1.0        
//////////////////////////////////////////////////////////////////
BOOL CBzip::IsEmptyDirectory(LPCTSTR szDirectory)
{
	CString strDir(szDirectory);
	CCeFileFind m_FileFind;

	if( strDir.Right(1) != L"\\")
		strDir += L"\\";
	strDir += L"*.*";
//	BOOL bExisted = m_FileFind.FindFile(strDir);
//	ASSERT(bExisted);
//	if(!bExisted)
//		return TRUE;

//	return (m_FileFind.FindNextFile() == FALSE);
	return (m_FileFind.FindFile(strDir) == FALSE);
}// End of IsEmptyDirectory

//////////////////////////////////////////////////////////////
// this is a CALLBACK function for extract progress 
// if you use it ,you'd better derive a new class based CBzip
// and in the function add your code
// PARAMETER:
//     szExtractFile  ---- Current extracted file name
//     nError         ---- 0 for failure,other expresses 
//                            extracted file size
// RETURN VALUE: NONE
// AUTHOR: YANXINHAI (yanxh@mic.com.tw)
///////////////////////////////////////////////////////////////
void CBzip::OnExtractProgress(LPCTSTR szExtractFile,DWORD nErr)
{
	//add you code here
	
}// End of OnExtractProgress

BOOL CBzip::WriteFileToZip(LPCTSTR szSource, CFile &cTarget)
{
	CFile cfSource;
	int nLen;
#define MAX_BUFF   32768	
	CHAR  buf[MAX_BUFF];
//	if (m_FirstDirFlag)
//	{
//		CTestDlg testDlg;
//		testDlg.DoModal();
//		m_FirstDirFlag = FALSE;
//		AfxGetMainWnd()->GetActiveWindow()->UpdateWindow();
//	}
//	TRY
//	{
		BOOL bSuccess = cfSource.Open(szSource,CFile::modeRead);
		if(!bSuccess)
		{				
			if (GetLastError() == ERROR_SHARING_VIOLATION)
				return ERROR_SHARING_VIOLATION;
			return FALSE;
		}
		do
		{
			nLen = cfSource.Read(buf,MAX_BUFF);
			cTarget.Write(buf,nLen);
			OnExtractProgress(szSource,nLen);
		}while(nLen == MAX_BUFF);

		cfSource.Close();
//	}
//	CATCH(CFileException ,e)
//	{
//		e->ReportError();
//		e->Delete();
//		if(cfSource.m_hFile != CFile::hFileNull)
//			cfSource.Close();
//		return FALSE;
//	}
//	END_CATCH
	return TRUE;
}

BOOL CBzip::IsExistedDirectory(LPCTSTR szDirectory)
{
	CCeFileFind m_FileFind;
	CString strDir(szDirectory);
	int nLen = strDir.GetLength();
	if( strDir.Right(1) == '\\' )
		strDir = strDir.Left(nLen -1);
	return m_FileFind.FindFile(strDir);
}

⌨️ 快捷键说明

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