21xrx.com
2024-12-22 21:34:37 Sunday
登录
文章检索 我的文章 写文章
如何在VC++6.0中解压文件?
2023-06-29 16:24:51 深夜i     --     --
VC++ 0 解压 文件

VC++6.0是一种经典的集成开发环境,它可用于开发各种C++软件应用程序。其中解压文件是其常见的应用之一,下面将介绍如何在VC++6.0中解压文件。

首先,打开VC++6.0,创建一个新的Win32控制台应用程序,命名为“unzip”。然后在“Solution Explorer”中右键单击该项目,选择“Add”->“New Item”,将创建一个新的源文件,命名为“unzip.cpp”。

接下来,我们需要包含必要的头文件,从而使程序能够正确使用解压缩功能。必备的头文件包括 , , , 等。

#include

#include

#include

#include

接着,我们定义一个解压文件的函数,名为“unzipFile”。此函数的参数包括待解压缩文件的路径名和目标路径名。函数代码如下所示:

BOOL unzipFile(LPCTSTR lpZipFileName, LPCTSTR lpDstPath)

{

  BOOL bResult = FALSE;

  int nLength = lstrlen(lpZipFileName);

  TCHAR* lpTempZipName = new TCHAR[nLength+1];

  lstrcpy(lpTempZipName, lpZipFileName);

  if(::PathFileExists(lpTempZipName))

  {

    TCHAR* lpZipFileTitle = ::PathFindFileName(lpTempZipName);

    TCHAR* lpZipPathName = new TCHAR[nLength+1];

    lstrcpyn(lpZipPathName, lpTempZipName, lpZipFileTitle-lpTempZipName+1);

    SHFILEINFO SHFileInfo= {0};

    LPITEMIDLIST pIDList=0;

    HRESULT hr = ::SHGetDesktopFolder(NULL, &pIDList);

    if(hr==NOERROR)

    {

      LPITEMIDLIST pSubIDList=NULL;

      hr = ::SHParseDisplayName(lpZipPathName, NULL, &pSubIDList, 0, NULL);

      if(hr==NOERROR)

      {

        IShellFolder* pSFDesktop=0;

        hr = ::SHBindToObject(pIDList, NULL, NULL, IID_IShellFolder, (LPVOID*)&pSFDesktop);

        if(hr==NOERROR)

        {

          ULONG chEaten=0;

          DWORD dwAttributes=0;

          hr = pSFDesktop->ParseDisplayName(NULL, NULL, lpZipFileTitle, &chEaten, &pSubIDList, &dwAttributes);

          if(hr==NOERROR)

          {

            IShellFolder* pSF=0;

            hr = pSFDesktop->BindToObject(pSubIDList, NULL, IID_IShellFolder, (LPVOID*)&pSF);

            if(hr==NOERROR)

            {

              LPITEMIDLIST pID=0;

              hr = pSF->ParseDisplayName(NULL, NULL, lpZipFileTitle, &chEaten, &pID, &dwAttributes);

              if(hr==NOERROR)

              {

                IExtractIcon* pExtractIcon=0;

                hr = pSF->GetUIObjectOf(NULL, 1, const_cast (&pID), IID_IExtractIcon, NULL, (LPVOID*)&pExtractIcon);

                if(hr==NOERROR)

                {

                  TCHAR lpIconPath[256]={0};

                  int nIndex=0;

                  UINT uFlags = GIL_SIMULATEDOC;

                  hr = pExtractIcon->GetIconLocation(GIL_FORSHELL, lpIconPath, sizeof(lpIconPath), &nIndex,&uFlags);

                  if(hr==NOERROR)

                  {

                    HICON hIcon=0;

                    hr = pExtractIcon->Extract(lpIconPath, nIndex, &hIcon, NULL, MAKELONG(64, 64));

                    if(hr==NOERROR)

                    {

                      DestroyIcon(hIcon);

                    }

                  }

                  pExtractIcon->Release();

                }

                pSF->Release();

              }

              ::CoTaskMemFree(pID);

            }

            pSFDesktop->Release();

          }

          ::CoTaskMemFree(pSubIDList);

        }

        ::CoTaskMemFree(pIDList);

      }

      delete[] lpZipPathName;

    }

    delete[] lpTempZipName;

  }

  return bResult;

}

至此,我们已经完成了VC++6.0中解压文件的代码,可将其编译运行。如果代码运行正常,将连接到待解压缩文件并提取所有文件到目标路径。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复