2009-05-18 108 views
5

在Windows资源管理器中,您可以提取压缩文件夹(zip文件)Windows API提取zip文件?

是否有API或命令行以编程方式使用相同方法提取zip文件?

回答

2
  1. 检查Compress Zip files with Windows Shell API and C#
  2. 你可以使用SharpZipLib这 是免费的,一个点网项目。
+0

谢谢abmv。但第一个链接需要一个外部zip.exe文件。你知道zip.exe的来源吗? 显然SharpZipLib无法解压缩使用WinZip创建的zip文件,可以吗? – Aximili 2009-05-18 23:25:18

+0

我看到它来自另一个项目。我想知道你为什么不能把它合并成一个 – Aximili 2009-05-18 23:59:32

5

您可以使用此的VBScript脚本:

'Adapted from http://www.robvanderwoude.com/vbstech_files_zip.html 

strFile = "c:\filename.zip" 
strDest = "c:\files" 

Set objFSO = CreateObject("Scripting.FileSystemObject") 

If Not objFSO.FolderExists(strDest) Then 
    objFSO.CreateFolder(strDest) 
End If 

UnZipFile strFile, strDest 

Sub UnZipFile(strArchive, strDest) 
    Set objApp = CreateObject("Shell.Application") 

    Set objArchive = objApp.NameSpace(strArchive).Items() 
    Set objDest = objApp.NameSpace(strDest) 

    objDest.CopyHere objArchive 
End Sub 
0

我的Excel 2010下试过上述功能Sub UnZipFile(...),这是工作:运行时错误“91”(对象变量或With块未设定)在管线

Set objArchive = objApp.Namespace(strArchive).Items() 

和线

Set objDest = objApp.Namespace(strDest) 

默默无闻:执行后objDest仍然没有!

微软的.Namespace()作为参数接受对象,字符串常量或字符串变量。随着字符串变量经常有可疑的问题,这是需要一个解决办法:

Set objArchive = objApp.Namespace(**CStr(** strArchive **)**).Items() 
Set objDest = objApp.Namespace(**CStr(** strDest **)**) 

或其他解决办法

Set objArchive = objApp.Namespace(**"" &** strArchive).Items() 
Set objDest = objApp.Namespace(**"" &** strDest) 

而且objDest.CopyHere objArchive也没有工作线:目标文件夹仍然是空的!

这里的一个版本,这是工作在Excel 2010中,最可能也是在其他环境中:

Sub UnZipFile(strZipArchive As String, strDestFolder As String) 
    Dim objApp As Object 
    Dim vItem As Variant 
    Dim objDest As Object 

    Set objApp = CreateObject("Shell.Application") 
    Set objDest = objApp.Namespace(CStr(strDestFolder)) 
    For Each vItem In objApp.Namespace(CStr(strZipArchive)).Items 
    objDest.CopyHere vItem 
    Next vItem 
End Sub 
0

对于C#或VB的用户,可以从MSDN检查答案: https://msdn.microsoft.com/en-us/library/ms404280(v=vs.100).aspx

对于.net 4.x,这里是MSDN示例代码

using System; 
using System.IO; 
using System.IO.Compression; 

namespace ConsoleApplication 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string startPath = @"c:\example\start"; 
      string zipPath = @"c:\example\result.zip"; 
      string extractPath = @"c:\example\extract"; 

      ZipFile.CreateFromDirectory(startPath, zipPath); 

      ZipFile.ExtractToDirectory(zipPath, extractPath); 
     } 
    } 
}