📄 compressdoc.cpp
字号:
// CompressDoc.cpp: implementation of the CCompressDoc class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Compress.h"
#include "CompressDoc.h"
#include "fileinfo.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CCompressDoc::CCompressDoc(CLibManager *lpLibmgr)
{
m_lpLibmgr=lpLibmgr;
m_LibIndex=-1;
m_hFile=0;
m_FnInfo.lpfnCreate=NULL;
m_FnInfo.lpfnRelease=NULL;
m_FnInfo.lpfnGetNoEntries=NULL;
m_FnInfo.lpfnGetFileInfo=NULL;
m_FnInfo.lpfnAddNewFile=NULL;
m_FnInfo.lpfnExtractFile=NULL;
m_FnInfo.lpfnDeleteFiles=NULL;
m_FnInfo.lpfnTestFile=NULL;
m_FnInfo.lpfnSetPassword=NULL;
m_FnInfo.lpfnGetComment=NULL;
m_FnInfo.lpfnSetComment=NULL;
}
CCompressDoc::~CCompressDoc()
{
Close();
}
BOOL CCompressDoc::OpenFile(LPCTSTR lpszFileName,int mode,int libIndex)
{
Close();
if (!m_lpLibmgr) return FALSE; //无任何库
if (libIndex>=0)
m_LibIndex=m_lpLibmgr->LoadLib(libIndex);
else
m_LibIndex=m_lpLibmgr->LoadLib((LPCTSTR)GetFileExt(lpszFileName));
if (m_LibIndex<0) {Close();return FALSE;} //无相应库
m_lpLibmgr->GetFuncInfo(m_LibIndex,&m_FnInfo);
if (!(m_hFile=m_FnInfo.lpfnCreate(lpszFileName,mode)))
{Close();return FALSE;} //不能打开该文件
m_lpszFileName=lpszFileName;
ReloadFileList();
return TRUE;
}
void CCompressDoc::ReloadFileList()
{
int n,i;
FILEINFO fi;
n=m_FnInfo.lpfnGetNoEntries(m_hFile);
m_FileList.Clear();
for (i=0;i<n;i++)
{
m_FnInfo.lpfnGetFileInfo(m_hFile,&fi,i);
m_FileList.Append(&fi);
}
}
void CCompressDoc::Close()
{
if (!m_lpLibmgr) return; //无任何库
if (m_hFile)
{
m_FnInfo.lpfnRelease(m_hFile);
m_hFile=0;
}
m_FileList.Clear();
m_FnInfo.lpfnCreate=NULL;
m_FnInfo.lpfnRelease=NULL;
m_FnInfo.lpfnGetNoEntries=NULL;
m_FnInfo.lpfnGetFileInfo=NULL;
m_FnInfo.lpfnAddNewFile=NULL;
m_FnInfo.lpfnExtractFile=NULL;
m_FnInfo.lpfnDeleteFiles=NULL;
m_FnInfo.lpfnTestFile=NULL;
m_FnInfo.lpfnSetPassword=NULL;
m_FnInfo.lpfnGetComment=NULL;
m_FnInfo.lpfnSetComment=NULL;
if (m_LibIndex>=0)
{
m_lpLibmgr->FreeLib(m_LibIndex);
m_LibIndex=-1;
}
m_lpszFileName.Empty();
}
void CCompressDoc::FindFile(LPCTSTR lpszName,CWordArray &ar)
{
FILEINDEXLISTNODE *p=m_FileList.root->next;
int len=lstrlen(lpszName);
CString szName=lpszName,szLeft;
szName.MakeLower();
ar.RemoveAll();
while (p)
{
szLeft=p->szFileName.Left(len);
szLeft.MakeLower();
if (szName==szLeft)
ar.Add(p->nIndex);
p=p->next;
}
}
/////////////////////////////
BOOL CCompressDoc::addFiles(int mode,const CStringArray &Files,const CString &DestPos,int iLevel)
{
char *pos;BOOL r=TRUE;
if (mode==nofolder) pos="";
else if (mode==absolute) pos=NULL;
else pos=(char*)(LPCTSTR)DestPos;
int num=Files.GetSize();
for (int i=0;i<num;i++)
r=r && m_FnInfo.lpfnAddNewFile(m_hFile,(LPCTSTR)Files[i],pos,iLevel);
return r;
}
BOOL CCompressDoc::AddFiles(int mode,const CStringArray &Files,const CString &DestPos,int iLevel)
{
BOOL r=addFiles(mode,Files,DestPos,iLevel);
if (r) ReloadFileList();
return r;
}
BOOL CCompressDoc::addFolders(int mode,const CStringArray &Folders,const CString &DestPos,int iLevel)
{
CStringArray allfile,allfolder;
CString pos,dname,fn;
CFileFind ff;
BOOL b;
int l,len; BOOL r=TRUE;
int num=Folders.GetSize();
for (int i=0;i<num;i++)
{
len=Folders[i].GetLength();
if (Folders[i].GetAt(len-1)=='\\')Folders[i].Delete(len-1);
fn=Folders[i]+"\\*.*";
l=len-Folders[i].ReverseFind('\\')-1;
if (l<len) dname=Folders[i].Right(l);
pos=DestPos+'\\'+dname;
//得到Folders[i]目录下所有文件: allfile
//得到Folders[i]目录下所有文件夹: allfolder
b=ff.FindFile(fn);
while (b)
{
b=ff.FindNextFile();
fn=ff.GetFileName();
if (!(fn=="."||fn==".."))
{
if (ff.IsDirectory())
allfolder.Add(ff.GetFilePath());
else
allfile.Add(ff.GetFilePath());
}
}
ff.Close();
r=r&& addFiles(mode,allfile,pos,iLevel);
allfile.RemoveAll();
r=r&& addFolders(mode,allfolder,pos,iLevel); //递归调用
allfolder.RemoveAll();
}
return r;
}
BOOL CCompressDoc::AddFolders(int mode,const CStringArray &Folders,const CString &DestPos,int iLevel)
{
BOOL r=addFolders(mode,Folders,DestPos,iLevel);
if (r) ReloadFileList();
return r;
}
/////////////////////////////
BOOL CCompressDoc::DeleteFiles(CWordArray &ar)
{
int i,*index,size=ar.GetSize();
index=new int[size+1];
for (i=0;i<size;i++) index[i]=ar.GetAt(i);
index[size]=-1;
BOOL r=m_FnInfo.lpfnDeleteFiles(m_hFile,index);
delete[] index;
if (r) ReloadFileList();
return r;
}
BOOL CCompressDoc::DeleteFolders(CStringArray &Folders)
{
CWordArray ar;
int num=Folders.GetSize();
for (int i=0;i<num;i++)
FindFile(LPCTSTR(Folders[i]),ar);
return DeleteFiles(ar);
}
////////////////////////////////////
BOOL CCompressDoc::ExtractFiles(CWordArray &ar,CString &lpszPath)
{
int i,n=ar.GetSize();
BOOL r=TRUE;
for (i=0;i<n;i++)
r=r && m_FnInfo.lpfnExtractFile(m_hFile,ar[i],(LPCTSTR)lpszPath);
return r;
}
BOOL CCompressDoc::ExtractFolders(CStringArray &Folders,CString &lpszPath)
{
CWordArray ar;
int num=Folders.GetSize();
for (int i=0;i<num;i++)
FindFile(LPCTSTR(Folders[i]),ar);
return ExtractFiles(ar,lpszPath);
}
CString CCompressDoc::GetComment(int nIndex)
{
char comment[128]="";
if (m_FnInfo.lpfnGetComment)
m_FnInfo.lpfnGetComment(m_hFile,nIndex,comment,127);
return CString(comment);
}
void CCompressDoc::SetComment(int nIndex,LPCTSTR lpszComment)
{
if (m_FnInfo.lpfnSetComment)
m_FnInfo.lpfnSetComment(m_hFile,nIndex,lpszComment);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -