頭文件:ZipAndFile.h
[cpp]
//---------------------------------------------------------------------------
#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
[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;
}
使用方法:包了頭文件後:
[cpp]
ZipAndFile *TZipAndFile=new ZipAndFile();//新建對像
//刪除temp文件夾
dir_fullpath=(ExePath+"web_back").c_str();//刪除web原文件
if(TZipAndFile->deldir(dir_fullpath)) {}
delete TZipAndFile;// 刪除對像
DLL下載地址:
DLL文件下載
http://www.BkJia.com/uploadfile/2012/0331/20120331103552848.rar
DLL兩個接口:
//壓縮,bUtf8為false將支持路徑與文件內包含中文
BOOL ZWZipCompress(LPCTSTR lpszSourceFiles, LPCTSTR lpszDestFile, bool bUtf8 = false);
//解壓
BOOL ZWZipExtract(LPCTSTR lpszSourceFile, LPCTSTR lpszDestFolder);