头文件:ZipAndFile.h
//---------------------------------------------------------------------------
#ifndef ZipAndFileH
#define ZipAndFileH
#include <Classes.hpp>
//---------------------------------------------------------------------------
class ZipAndFile{
private:
public:
ZipAndFile();
~ZipAndFile();
//ZIP操作
bool DoZipfile(String DoZip,String ZipFilename,String SourceFile,bool Check);
//复制目录
bool MyCopyFiles(AnsiString FromFile,AnsiString ToFile);
//删除目录
bool deldir(char* dir_fullpath);
};
#endif
源文件:ZipAndFile.cpp
//---------------------------------------------------------------------------
#pragma hdrstop
#include "ZipAndFile.h"
#include "Tlhelp32.h"
#include <vcl.h>
//---------------------------------------------------------------------------
ZipAndFile::ZipAndFile()
{
}
ZipAndFile::~ZipAndFile()
{
}
//ZIP压缩与解压
//---------------------------------------------------------------------------
bool ZipAndFile::DoZipfile(String DoZip,String ZipFilename,String SourceFile,bool Check)
{
bool ZipReturn=false;
WideString w1;//必需要这样申请WideString变量,不然传值时会让两个变量使用同一样内存地址,搞了3个小时才发现这是BCB2006的BUG.
WideString w2;
LPCTSTR L1;//必需使用这个格式的变量,不然传过去到DLL时乱码。
LPCTSTR L2;
if(DoZip=="ZWZipCompress")//压缩
{
w1=SourceFile;
w2=ZipFilename;
L1=(const char*)w1.c_bstr();
L2=(const char*)w2.c_bstr();
bool __stdcall (*DllMethods)(LPCTSTR,LPCTSTR,bool);
HINSTANCE hInst=NULL;
hInst=LoadLibrary((ExtractFilePath(Application->ExeName)+"ZLibWrap.dll").c_str());//动态加载DLL //当前目录下的DLL文件
FARPROC P;
P = GetProcAddress(hInst,DoZip.c_str());
DllMethods=(bool __stdcall (__cdecl *)(LPCTSTR,LPCTSTR,bool))P;
if(DllMethods){
ZipReturn=DllMethods(L1,L2,Check);
}
FreeLibrary(hInst);
return ZipReturn;
}else if(DoZip=="ZWZipExtract")//解压
{
w1=ZipFilename;
w2=SourceFile;
L1=(const char*)w1.c_bstr();
L2=(const char*)w2.c_bstr();
bool __stdcall (*DllMethods)(LPCTSTR,LPCTSTR);
HINSTANCE hInst=NULL;
hInst=LoadLibrary((ExtractFilePath(Application->ExeName)+"ZLibWrap.dll").c_str());//动态加载DLL //当前目录下的DLL文件
FARPROC P;
P = GetProcAddress(hInst,DoZip.c_str());
DllMethods=(bool __stdcall (__cdecl *)(LPCTSTR,LPCTSTR))P;
if(DllMethods){
ZipReturn=DllMethods(L1,L2);
}
FreeLibrary(hInst);
return ZipReturn;
}
}
//复制目录文件
//---------------------------------------------------------------------------
bool ZipAndFile::MyCopyFiles(AnsiString FromFile,AnsiString ToFile)
{
while(true){
if (!DirectoryExists(ToFile)){
CreateDir(ToFile);//文件夹不存在则创建
break;
}else{
deldir(ToFile.c_str());//在就删除
}
}
SHFILEOPSTRUCT op;
String strFrom = FromFile+"\\*.*";
String strTo = ToFile;
op.fAnyOperationsAborted = true;
op.hwnd = NULL;
op.wFunc = FO_COPY;
op.pFrom = strFrom.c_str();
op.pTo = strTo.c_str();
op.fFlags = FOF_NOCONFIRMATION |FOF_NOCONFIRMMKDIR; //FOF_NOCONFIRMATION 不出现确认对话框(当需要覆盖时)
bool b=false;
b=SHFileOperation(&op);
//int kkk= SHFileOperation(&op);
switch(GetLastError())
{
//只要出错就弹出
return false;
}
return(b);
}
//删除目录文件
//---------------------------------------------------------------------------
bool ZipAndFile::deldir(char* dir_fullpath) //删除指定的目录
{
char dir[260]={0};
char filename[260]={0};
int len = 0;
int ch = '\\';
strcpy(dir, dir_fullpath);
len = strlen(dir);
char *temp = strrchr(dir,ch);//查找\\
if(len < 4 || temp == NULL) //根据后面的\\来判断,可能为磁盘根目录或者不是有效的目录路径
return false;
if(temp != NULL)
{
if((temp - dir + 1) != len) //在目录后添加 '\\'
strcat(dir,"\\");
}
GetCurrentDirectory(260,filename);//得到当前目录
strcat(filename,"\\");
if(strcmp(dir,filename)==0) //如果要删除的目录是当前目录
{
strcat(filename,"..");
SetCurrentDirectory(filename); //改变当前目录
}
WIN32_FIND_DATA finddata;
HANDLE fFile;
bool flag;
strcpy(filename,dir);
strcat(filename,"*.*");
fFile=FindFirstFile(filename,&finddata);
flag=true;
if(fFile!=INVALID_HANDLE_VALUE) //此目录有没有效
{
BOOL bfind=true;
while(bfind)
{
if(finddata.cFileName[0] != '.')
{
strcpy(filename,dir);
strcat(filename,finddata.cFileName);
if(finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)//判断是不是文件夹
{ //删除找到的子目录
strcat(filename,"\\");//如果文件夹就再加上\\后进行递归
flag = flag && deldir(filename); //递归
}
else
{ //删除找到的文件
SetFileAttributes(filename,FILE_ATTRIBUTE_NORMAL);//文件属性设为普通
flag = flag && DeleteFile(filename); //删除
}
}
bfind = FindNextFile(fFile,&finddata);
}
FindClose(fFile);
}
if(flag)
{
SetFileAttributes(dir_fullpath,FILE_ATTRIBUTE_NORMAL);//去掉只读
if(RemoveDirectory(dir_fullpath))//删除空目录
return true;
}
return false;
}
使用方法:包了头文件后:
ZipAndFile *TZipAndFile=new ZipAndFile();//新建对像
//删除temp文件夹
dir_fullpath=(ExePath+"web_back").c_str();//删除web原文件
if(TZipAndFile->deldir(dir_fullpath)) {}
delete TZipAndFile;// 删除对像
DLL下载地址:
DLL文件下载
http://download.csdn.net/detail/goodai007/4182207
DLL两个接口:
//压缩,bUtf8为false将支持路径与文件内包含中文
BOOL ZWZipCompress(LPCTSTR lpszSourceFiles, LPCTSTR lpszDestFile, bool bUtf8 = false);
//解压
BOOL ZWZipExtract(LPCTSTR lpszSourceFile, LPCTSTR lpszDestFolder);