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

📄 chzipdlg.cpp

📁 一个C语言实现的压缩解压的工具代码
💻 CPP
字号:
// CHzipDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CHzip.h"
#include "CHzipDlg.h"
#include "MyFile.h"

/* 错误代码 */
#define ERROR_OK				0x0			/* 无错误 */
#define ERROR_OPEN_SRC			0xff01		/* 不能打开源文件 */
#define ERROR_OPEN_OBJ			0xff02		/* 不能打开目标文件 */
#define ERROR_NO_MEM			0xff03		/* 没有内存 */

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


/* 全局变量 */
CString			gstr_Src, gstr_Obj;			/* 源文件和目标文件名		*/
bool			gb_Src, gb_Obj;				/* 是否打开源文件和目标文件 */
bool			gb_ZipEnd;					/* 是否已经压缩完毕			*/
unsigned char	guc_Progress;				/* 整体进度					*/
unsigned char	guc_Semaphore;				/* 进程使用的信号量			*/
unsigned int	gui_ErrorCode;				/* 错误代码					*/

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

/* 解析程序 */
static void ParseZip( LPVOID pParam );
extern void LZARI_UnCompress( unsigned char* pInBuffer,			// 入口缓冲指针
					 unsigned int ui_InSize,			// 入口文件大小
					 unsigned char* pOutBuffer,			// 出口缓冲指针
					 unsigned int *pi_OutSize			// 出口文件大小
					);
extern void LZARI_Compress( unsigned char* pInBuffer,			// 入口缓冲指针
					 unsigned int ui_InSize,			// 入口文件大小
					 unsigned char* pOutBuffer,			// 出口缓冲指针
					 unsigned int *pi_OutSize			// 出口文件大小
					);
/*
 * Name: sema_wait
 * Desc: 等待信号量
 */
void sema_wait( void )
{
	while( 0 == guc_Semaphore )
	{
		Sleep( 50 );
	}

	guc_Semaphore --;
}

/*
 * Mame: sema_signal
 * Desc: 通知信号量
 */
void sema_signal( void )
{
	guc_Semaphore ++;
}

/*
 * Name: sema_init
 * Desc: 初始化信号量
 */
void sema_init( void )
{
	guc_Semaphore = 1;
}

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCHzipDlg dialog

CCHzipDlg::CCHzipDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCHzipDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCHzipDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCHzipDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCHzipDlg)
	DDX_Control(pDX, IDC_PROGRESS1, m_Progress);
	DDX_Control(pDX, IDC_EDIT_SRC, m_editSrc);
	DDX_Control(pDX, IDC_EDIT_OBJ, m_editObj);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCHzipDlg, CDialog)
	//{{AFX_MSG_MAP(CCHzipDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTT_SRC, OnButtSrc)
	ON_BN_CLICKED(IDC_BUTT_OBJ, OnButtObj)
	ON_BN_CLICKED(IDC_BUTT_START, OnButtStart)
	ON_BN_CLICKED(IDC_BUTT_START2, OnButtStart2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCHzipDlg message handlers

BOOL CCHzipDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	gstr_Src	= _T( "" );
	gstr_Obj	= _T( "" );

	gb_Src		= false;
	gb_Obj		= false;
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCHzipDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CCHzipDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCHzipDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

/*
 * Name: OnButtSrc
 * Desc: 打开Bin文件
 */
void CCHzipDlg::OnButtSrc() 
{
	MyFile dlgSrc( TRUE, "*.bin", "*.bin" );

	if( IDOK !=  dlgSrc.DoModal() )
		return;

	gstr_Src	= dlgSrc.GetPathName();
	gb_Src		= true;
	this->m_editSrc.SetWindowText( gstr_Src );
}



/*
 * Name: OnButtObj
 * Desc: 保存cys文件
 */
void CCHzipDlg::OnButtObj()
{
	MyFile dlgObj( FALSE, "*.*", "*.*" );

	if( IDOK != dlgObj.DoModal() )
		return;

	gstr_Obj	= dlgObj.GetPathName();
	gb_Obj		= true;
	this->m_editObj.SetWindowText( gstr_Obj );
}


/*
 * Name: OnButtStart
 * Desc: 开始压缩
 */
void CCHzipDlg::OnButtStart() 
{
	LPVOID			nouse;
	unsigned char	*pSrc, *pObj;
	unsigned int	ui_SrcSize, ui_ObjSize;
	HANDLE			hSrc, hObj;
	DWORD			uAct;

	if( false == gb_Src ||
		false == gb_Obj
		)
	{
		MessageBox( "信息", "请打开文件!", MB_OK );
		return;
	}

	hSrc = CreateFile( gstr_Src, GENERIC_READ, FILE_SHARE_READ, NULL,
						OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, (HANDLE)NULL );

	if( INVALID_HANDLE_VALUE == hSrc )
	{
		MessageBox( "信息", "不能打开读文件!", MB_OK );
		return;
	}

	hObj = CreateFile( gstr_Obj, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS,
						FILE_ATTRIBUTE_NORMAL, (HANDLE)NULL );

	if( INVALID_HANDLE_VALUE == hObj )
	{
		MessageBox( "信息", "不能打开写文件!", MB_OK );
		return;
	}

	// 得到读文件大小
	ui_SrcSize = GetFileSize( hSrc, NULL );

	pSrc = (unsigned char*)malloc( ui_SrcSize );
	pObj = (unsigned char*)malloc( ui_SrcSize );

	if( NULL == pSrc || NULL == pObj )
	{
		MessageBox( "信息", "内存不足!", MB_OK );
		return;
	}

	ui_ObjSize = 0;
	ReadFile( hSrc, pSrc, ui_SrcSize, &uAct, NULL );

	memset( pObj, 0, ui_SrcSize );

	// 压缩
	LZARI_Compress( pSrc, ui_SrcSize, pObj, &ui_ObjSize );

	// 写入目标文件
	WriteFile( hObj, pObj, ui_ObjSize, &uAct, NULL );

	CloseHandle( hSrc );
	CloseHandle( hObj );

	free( pSrc );
	free( pObj );

	/* 提示错误 */
	if( ERROR_OK == gui_ErrorCode )
	{
		MessageBox( "成功", "信息", MB_OK );
		return;
	}

	switch( gui_ErrorCode )
	{
	case ERROR_OPEN_SRC:	/* 不能打开源文件 */
		MessageBox( "不能打开源文件", "错误", MB_OK );
		break;
	case ERROR_OPEN_OBJ:	/* 不能打开目标文件 */
		MessageBox( "不能打开目标文件", "错误", MB_OK );
		break;
	case ERROR_NO_MEM:		/* 内存不足 */
		MessageBox( "内存不足", "错误", MB_OK );
		break;
	default:
		MessageBox( "错误", "未知错误!", MB_OK );
		break;
	}

	return;
}

/*
 * Name: SetProgress
 * Desc: 显示压缩进度
 */
void CCHzipDlg::SetProgress( unsigned char uc_Progress )
{
	this->m_Progress.SetPos( uc_Progress );

	return;
}

/*
 * Name: ParseZip
 * Desc: 压缩进程
 */
void ParseZip( LPVOID pParam )
{
}

void CCHzipDlg::OnButtStart2() 
{
	LPVOID			nouse;

	unsigned char	*pSrc, *pObj;
	unsigned int	ui_SrcSize, ui_ObjSize;
	HANDLE			hSrc, hObj;
	DWORD			uAct;


	if( false == gb_Src ||
		false == gb_Obj
		)
	{
		MessageBox( "信息", "请打开文件!", MB_OK );
		return;
	}

	hSrc = CreateFile( gstr_Src, GENERIC_READ, FILE_SHARE_READ, NULL,
						OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, (HANDLE)NULL );

	if( INVALID_HANDLE_VALUE == hSrc )
	{
		MessageBox( "信息", "不能打开读文件!", MB_OK );
		return;
	}

	hObj = CreateFile( gstr_Obj, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS,
						FILE_ATTRIBUTE_NORMAL, (HANDLE)NULL );

	if( INVALID_HANDLE_VALUE == hObj )
	{
		MessageBox( "信息", "不能打开写文件!", MB_OK );
		return;
	}

	// 得到读文件大小
	ui_SrcSize = GetFileSize( hSrc, NULL );

	pSrc = (unsigned char*)malloc( ui_SrcSize );

	if( NULL == pSrc )
	{
		MessageBox( "信息", "内存不足!", MB_OK );
		CloseHandle( pSrc );
		CloseHandle( pObj );
		return;
	}

	ui_ObjSize = 0;
	ReadFile( hSrc, pSrc, ui_SrcSize, &uAct, NULL );


	ui_ObjSize = pSrc[0] | pSrc[1] << 8 | pSrc[2] << 16 | pSrc[3] << 24;
	pObj = (unsigned char*)malloc( ui_ObjSize );
	if( NULL == pObj )
	{
		MessageBox( "信息1", "内存不足!", MB_OK );
		CloseHandle( pSrc );
		CloseHandle( pObj );
		free( pSrc );
		return;
	}
	memset( pObj, 0, ui_ObjSize );

	// 压缩
	LZARI_UnCompress( pSrc, ui_SrcSize, pObj, &ui_ObjSize );

	// 写入目标文件
	WriteFile( hObj, pObj, ui_ObjSize, &uAct, NULL );
	CloseHandle( hSrc );
	CloseHandle( hObj );
	free( pSrc );
	free( pObj );

	/* 提示错误 */
	if( ERROR_OK == gui_ErrorCode )
	{
		MessageBox( "成功", "信息", MB_OK );
		return;
	}

	switch( gui_ErrorCode )
	{
	case ERROR_OPEN_SRC:	/* 不能打开源文件 */
		MessageBox( "不能打开源文件", "错误", MB_OK );
		break;
	case ERROR_OPEN_OBJ:	/* 不能打开目标文件 */
		MessageBox( "不能打开目标文件", "错误", MB_OK );
		break;
	case ERROR_NO_MEM:		/* 内存不足 */
		MessageBox( "内存不足", "错误", MB_OK );
		break;
	default:
		MessageBox( "错误", "未知错误!", MB_OK );
		break;
	}

	return;	
}

⌨️ 快捷键说明

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