提取zip文件的Windows API?

5

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

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

4个回答

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

2

谢谢abmv。但是第一个链接需要一个外部的zip.exe文件。你知道那个zip.exe文件来自哪里吗? 显然SharpZipLib不能解压使用WinZip创建的zip文件,对吧? - Aximili
我看这是来自另一个项目。我想知道为什么不能将它合并成一个。 - Aximili

0
我在Excel 2010下尝试了上述函数Sub UnZipFile(...),但它没有起作用:在该行中出现了运行时错误'91'(对象变量或With块未设置)。
Set objArchive = objApp.Namespace(strArchive).Items() 

和这行代码

Set objDest = objApp.Namespace(strDest)

is silently also not working: After execution the objDest is still nothing!

Microsoft的.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/zh-cn/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);
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接