当前位置:编程学习 > C/C++ >>

c++builder ZIP文件解压与压缩(ZLIB DLL调用),目录复制与删除整合的自用类,可自行扩张!

头文件: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

补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,