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

📄 lzwdoc.cpp

📁 使用LZ77算法实现文件压缩 点击文件-压缩
💻 CPP
字号:
// LZWDoc.cpp : implementation of the CLZWDoc class
//

#include "stdafx.h"
#include "LZW.h"
#include "lz77.h"
#include "LZWCompression.h"
#include "LZWDoc.h"

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

/////////////////////////////////////////////////////////////////////////////
// CLZWDoc

IMPLEMENT_DYNCREATE(CLZWDoc, CDocument)

BEGIN_MESSAGE_MAP(CLZWDoc, CDocument)
	//{{AFX_MSG_MAP(CLZWDoc)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLZWDoc construction/destruction

CLZWDoc::CLZWDoc()
{
}

CLZWDoc::~CLZWDoc()
{
}

BOOL CLZWDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CLZWDoc serialization

void CLZWDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CLZWDoc diagnostics

#ifdef _DEBUG
void CLZWDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CLZWDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CLZWDoc commands

CStringArray * CLZWDoc::GetLog()
{
	return &m_Log;
}

void CLZWDoc::Log(CString entry)
{
	m_Log.Add(entry);
	UpdateAllViews(NULL);
}

void CLZWDoc::ClearLog()
{
	m_Log.RemoveAll();
	UpdateAllViews(NULL);
}

void CLZWDoc::Compress()
{
	ClearLog();
	CFileDialog cFD(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "All files (*.*)|*.*|");
	if (cFD.DoModal() == IDCANCEL)
	{
		Log("你必须选择一个文件才能进行压缩!");
		return;
	}
	//	Set the files names and data
	m_SourceFile = cFD.GetPathName();
	m_TargetFile = m_SourceFile.Left(m_SourceFile.ReverseFind('.') + 1) + "lz77";

	Log("压缩开始!");
	Log("准备压缩文件位置: " + m_SourceFile);
	Log("压缩文件保存位置: " + m_TargetFile);
	if(lz77(m_SourceFile.GetBuffer(0),m_TargetFile.GetBuffer(0),true))
	{
		ClearLog();
		Log("程序压缩失败!");
		Log("程序在压缩过程中出错!");
	}
	else
		Log("压缩完成!");

	m_SourceFile.ReleaseBuffer();
	m_TargetFile.ReleaseBuffer();
}

void CLZWDoc::Decompress()
{
	BYTE extlen;
	char fileext[10];
	//	Clear the view log
	ClearLog();
	Log("请打开需要解压的文件!");

	//	Open the dialog to select a file
	CFileDialog cFD(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "Compressed files (*.lz77)|*.lz77|");

	//	Select a file to be opened
	if (cFD.DoModal() == IDCANCEL)
	{
		ClearLog();
		Log("请选择一个可以解压的文件!");
		return;
	}
	m_SourceFile = cFD.GetPathName();

	Log("解压缩开始!");
	Log("压缩文件位置: " + m_SourceFile);

	FILE *fp;
	fp=fopen(m_SourceFile.GetBuffer(0),"rb");
	fread(&extlen,1,sizeof(BYTE),fp);
	fread(fileext,1,(int)extlen*sizeof(char),fp);
	fclose(fp);
	CString temp(fileext);

	m_TargetFile = m_SourceFile.Left(m_SourceFile.ReverseFind('.') + 1) + temp.Left((int)extlen);

	Log("解压文件位置: " + m_TargetFile);
	
	if (lz77(m_SourceFile.GetBuffer(0),m_TargetFile.GetBuffer(0),false))
	{
		ClearLog();
		Log("程序在解压缩过程中出错!");
		Log("程序解压缩失败!");
	}
	else
		Log("解压缩完成!");

	m_SourceFile.ReleaseBuffer();
	m_TargetFile.ReleaseBuffer();
}

⌨️ 快捷键说明

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