转载:http://blog.csdn.net/goodai007/article/details/7414512
头文件:ZipAndFile.h
1 //--------------------------------------------------------------------------- 2 3 #ifndef ZipAndFileH 4 #define ZipAndFileH 5 #include <Classes.hpp> 6 //--------------------------------------------------------------------------- 7 class ZipAndFile{ 8 private: 9 10 public: 11 ZipAndFile(); 12 ~ZipAndFile(); 13 //ZIP操作 14 bool DoZipfile(String DoZip,String ZipFilename,String SourceFile,bool Check); 15 //复制目录 16 bool MyCopyFiles(AnsiString FromFile,AnsiString ToFile); 17 //删除目录 18 bool deldir(char* dir_fullpath); 19 }; 20 #endif
源文件:ZipAndFile.cpp
1 //--------------------------------------------------------------------------- 2 #pragma hdrstop 3 #include "ZipAndFile.h" 4 #include "Tlhelp32.h" 5 #include <vcl.h> 6 7 //--------------------------------------------------------------------------- 8 ZipAndFile::ZipAndFile() 9 { 10 } 11 ZipAndFile::~ZipAndFile() 12 { 13 } 14 //ZIP压缩与解压 15 //--------------------------------------------------------------------------- 16 bool ZipAndFile::DoZipfile(String DoZip,String ZipFilename,String SourceFile,bool Check) 17 { 18 bool ZipReturn=false; 19 WideString w1;//必需要这样申请WideString变量,不然传值时会让两个变量使用同一样内存地址,搞了3个小时才发现这是BCB2006的BUG. 20 WideString w2; 21 LPCTSTR L1;//必需使用这个格式的变量,不然传过去到DLL时乱码。 22 LPCTSTR L2; 23 if(DoZip=="ZWZipCompress")//压缩 24 { 25 w1=SourceFile; 26 w2=ZipFilename; 27 L1=(const char*)w1.c_bstr(); 28 L2=(const char*)w2.c_bstr(); 29 bool __stdcall (*DllMethods)(LPCTSTR,LPCTSTR,bool); 30 HINSTANCE hInst=NULL; 31 hInst=LoadLibrary((ExtractFilePath(Application->ExeName)+"ZLibWrap.dll").c_str());//动态加载DLL //当前目录下的DLL文件 32 FARPROC P; 33 P = GetProcAddress(hInst,DoZip.c_str()); 34 DllMethods=(bool __stdcall (__cdecl *)(LPCTSTR,LPCTSTR,bool))P; 35 if(DllMethods){ 36 ZipReturn=DllMethods(L1,L2,Check); 37 } 38 FreeLibrary(hInst); 39 return ZipReturn; 40 }else if(DoZip=="ZWZipExtract")//解压 41 { 42 w1=ZipFilename; 43 w2=SourceFile; 44 L1=(const char*)w1.c_bstr(); 45 L2=(const char*)w2.c_bstr(); 46 bool __stdcall (*DllMethods)(LPCTSTR,LPCTSTR); 47 HINSTANCE hInst=NULL; 48 hInst=LoadLibrary((ExtractFilePath(Application->ExeName)+"ZLibWrap.dll").c_str());//动态加载DLL //当前目录下的DLL文件 49 FARPROC P; 50 P = GetProcAddress(hInst,DoZip.c_str()); 51 DllMethods=(bool __stdcall (__cdecl *)(LPCTSTR,LPCTSTR))P; 52 if(DllMethods){ 53 ZipReturn=DllMethods(L1,L2); 54 } 55 FreeLibrary(hInst); 56 return ZipReturn; 57 } 58 } 59 //复制目录文件 60 //--------------------------------------------------------------------------- 61 bool ZipAndFile::MyCopyFiles(AnsiString FromFile,AnsiString ToFile) 62 { 63 while(true){ 64 if (!DirectoryExists(ToFile)){ 65 CreateDir(ToFile);//文件夹不存在则创建 66 break; 67 }else{ 68 deldir(ToFile.c_str());//在就删除 69 } 70 } 71 SHFILEOPSTRUCT op; 72 String strFrom = FromFile+"\\*.*"; 73 String strTo = ToFile; 74 op.fAnyOperationsAborted = true; 75 op.hwnd = NULL; 76 op.wFunc = FO_COPY; 77 op.pFrom = strFrom.c_str(); 78 op.pTo = strTo.c_str(); 79 op.fFlags = FOF_NOCONFIRMATION |FOF_NOCONFIRMMKDIR; //FOF_NOCONFIRMATION 不出现确认对话框(当需要覆盖时) 80 bool b=false; 81 b=SHFileOperation(&op); 82 //int kkk= SHFileOperation(&op); 83 switch(GetLastError()) 84 { 85 //只要出错就弹出 86 return false; 87 } 88 89 return(b); 90 } 91 //删除目录文件 92 //--------------------------------------------------------------------------- 93 bool ZipAndFile::deldir(char* dir_fullpath) //删除指定的目录 94 { 95 char dir[260]={0}; 96 char filename[260]={0}; 97 int len = 0; 98 int ch = ‘\\‘; 99 strcpy(dir, dir_fullpath); 100 len = strlen(dir); 101 char *temp = strrchr(dir,ch);//查找\\ 102 if(len < 4 || temp == NULL) //根据后面的\\来判断,可能为磁盘根目录或者不是有效的目录路径 103 return false; 104 105 if(temp != NULL) 106 { 107 if((temp - dir + 1) != len) //在目录后添加 ‘\\‘ 108 strcat(dir,"\\"); 109 } 110 GetCurrentDirectory(260,filename);//得到当前目录 111 strcat(filename,"\\"); 112 if(strcmp(dir,filename)==0) //如果要删除的目录是当前目录 113 { 114 strcat(filename,".."); 115 SetCurrentDirectory(filename); //改变当前目录 116 } 117 118 WIN32_FIND_DATA finddata; 119 HANDLE fFile; 120 bool flag; 121 strcpy(filename,dir); 122 strcat(filename,"*.*"); 123 fFile=FindFirstFile(filename,&finddata); 124 flag=true; 125 if(fFile!=INVALID_HANDLE_VALUE) //此目录有没有效 126 { 127 BOOL bfind=true; 128 while(bfind) 129 { 130 if(finddata.cFileName[0] != ‘.‘) 131 { 132 strcpy(filename,dir); 133 strcat(filename,finddata.cFileName); 134 if(finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)//判断是不是文件夹 135 { //删除找到的子目录 136 strcat(filename,"\\");//如果文件夹就再加上\\后进行递归 137 flag = flag && deldir(filename); //递归 138 } 139 else 140 { //删除找到的文件 141 SetFileAttributes(filename,FILE_ATTRIBUTE_NORMAL);//文件属性设为普通 142 flag = flag && DeleteFile(filename); //删除 143 } 144 } 145 bfind = FindNextFile(fFile,&finddata); 146 } 147 FindClose(fFile); 148 } 149 if(flag) 150 { 151 SetFileAttributes(dir_fullpath,FILE_ATTRIBUTE_NORMAL);//去掉只读 152 if(RemoveDirectory(dir_fullpath))//删除空目录 153 return true; 154 } 155 return false; 156 }
使用方法:包了头文件后:
1 ZipAndFile *TZipAndFile=new ZipAndFile();//新建对像 2 //删除temp文件夹 3 dir_fullpath=(ExePath+"web_back").c_str();//删除web原文件 4 if(TZipAndFile->deldir(dir_fullpath)) {} 5 delete TZipAndFile;// 删除对像
时间: 2024-10-15 12:04:35