1. 程式人生 > >LZO詞典壓縮器實現(含介面)

LZO詞典壓縮器實現(含介面)

1、介面效果:


   (測試)壓縮結果:


    

    (測試)解壓縮結果:


2、關鍵實現程式碼

(1)選擇待壓縮檔案按鈕

void CcompressDlg::OnBnClickedButton3()
{
	// TODO: 在此新增控制元件通知處理程式程式碼
	CString strFile = _T("");

	CFileDialog    dlgFile(TRUE, NULL, NULL, OFN_HIDEREADONLY, _T("Describe Files (*.cfg)|*.cfg|All Files (*.*)|*.*||"), NULL);

	if (dlgFile.DoModal())
	{
		strFile = dlgFile.GetPathName();
	}

	UpdateData(true);          // 獲取資料
	path = strFile;
	UpdateData(false);         // 更新資料
}


(2)選擇解壓縮檔案按鈕

void CcompressDlg::OnBnClickedButton4()
{
	// TODO: 在此新增控制元件通知處理程式程式碼
	CString strFile = _T("");

	CFileDialog    dlgFile(TRUE, NULL, NULL, OFN_HIDEREADONLY, _T("Describe Files (*.cfg)|*.cfg|All Files (*.*)|*.*||"), NULL);

	if (dlgFile.DoModal())
	{
		strFile = dlgFile.GetPathName();
	}

	UpdateData(true);          // 獲取資料
	path_decompress = strFile;
	UpdateData(false);         // 更新資料
}


(3)壓縮按鈕

void CcompressDlg::OnBnClickedButton1()
{
	// TODO: 在此新增控制元件通知處理程式程式碼
#ifdef _UNICODE
	LONG len1;
	len1 = WideCharToMultiByte(CP_ACP, 0, path, -1, NULL, 0, NULL, NULL);
	char *textPath = (char *)malloc(sizeof(char)*(len1 + 1));
	memset(textPath, 0, len1 + 1);
	WideCharToMultiByte(CP_ACP, 0, path, -1, textPath, len1 + 1, NULL, NULL);
#else
	ptr = new char[str.GetAllocLength() + 1];
#endif
	err = fopen_s(&ifp, textPath, "rb");
	if (err == 0)
	{
		printf("Thefile'crt_fopen_s.c'wasopened\n");
	}
	fseek(ifp, 0, SEEK_END);
	unsigned long len = ftell(ifp);
	myInLen = len;
	fseek(ifp, 0, SEEK_SET);
	cpTemp = (char*)malloc(len * sizeof(char));
	fread(cpTemp, sizeof(char), len, ifp);
	char *in = cpTemp;
	char *out = (char*)malloc((len + len / 16 + 64 + 3) * sizeof(char));
	
	if (lzo_init() != LZO_E_OK)
	{
		printf("internal error - lzo_init() failed !!!\n");
		printf("(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable '-DLZO_DEBUG' for diagnostics)\n");
		
	}
	
	lzo_uint in_len;
	in_len = len;
	lzo_memset(in, 0, in_len);
	lzo_uint out_len;

	int r;
	
	r = lzo1x_1_compress((unsigned char*)in, in_len, (unsigned char*)out, &out_len, wrkmem);
	char mess[256];

	if (r == LZO_E_OK)
	{
		sprintf_s(mess, sizeof(mess),  "壓縮成功!\n\n原檔案大小:%lu bytes\n壓縮後大小:%lu bytes\n\n解壓得到的檔案“compress.txt”保存於工程目錄下",
			(unsigned long)in_len, (unsigned long)out_len);
		MessageBox(CString(mess));
	
	}
	else
	{
		/* this should NEVER happen */
		sprintf_s(mess, sizeof(mess), "壓縮失敗!\n %d\n", r);
		MessageBox(CString(mess));
	}
	/* check for an incompressible block */
	if (out_len >= in_len)
	{
		sprintf_s(mess, sizeof(mess), "未壓縮!\n");
		MessageBox(CString(mess));
	}

	err = fopen_s(&ofp, ".\\compress.txt", "wb");
	fwrite(out, sizeof(char), out_len, ofp);
	
	fclose(ifp);
	fclose(ofp);
	free(cpTemp);
	free(out);
	free(textPath);

}

(4)解壓縮按鈕

void CcompressDlg::OnBnClickedButton2()
{
	// TODO: 在此新增控制元件通知處理程式程式碼
#ifdef _UNICODE
	LONG len1;
	len1 = WideCharToMultiByte(CP_ACP, 0, path_decompress, -1, NULL, 0, NULL, NULL);
	char *textPath = (char *)malloc(sizeof(char)*(len1 + 1));
	memset(textPath, 0, len1 + 1);
	WideCharToMultiByte(CP_ACP, 0, path_decompress, -1, textPath, len1 + 1, NULL, NULL);
#else
	ptr = new char[str.GetAllocLength() + 1];
#endif
	err = fopen_s(&ifp, textPath, "rb");
	if (err == 0)
	{
		printf("Thefile'crt_fopen_s.c'was opened\n");
	}
	fseek(ifp, 0, SEEK_END);
	unsigned long len = ftell(ifp);
	fseek(ifp, 0, SEEK_SET);
	cpTemp = (char*)malloc(len * sizeof(char));
	fread(cpTemp, sizeof(char), len, ifp);
	char *in = cpTemp;
	char *out = (char *)malloc(len*100* sizeof(char));
	memset(out, 0, len * 100 * sizeof(char));
	
	int r;
	lzo_uint new_len;
	r = lzo1x_decompress((unsigned char*)in, len, (unsigned char*)out, &new_len, NULL);

	char mess[256];
	if (r == LZO_E_OK && new_len == myInLen)
	{
		sprintf_s(mess, sizeof(mess), "解壓成功!\n\n待解壓檔案大小: %lu bytes\n解壓後文件大小: %lu bytes\n\n解壓得到的檔案“decompress.txt”保存於工程目錄下",
			(unsigned long)len, (unsigned long)new_len);
		MessageBox(CString(mess));
	}
	else
	{
		/* this should NEVER happen */
		sprintf_s(mess, sizeof(mess), "解壓失敗! %d\n", r);
		MessageBox(CString(mess));
	}
	err = fopen_s(&ofp, ".\\decompress.txt", "wt+");
	fwrite(out, sizeof(char), new_len, ofp);

	fclose(ifp);
	fclose(ofp);
	free(cpTemp);
	free(textPath);
	free(out);
}


(5)LZO壓縮演算法