windows - Windows 的内置 ZIP 压缩可以编写脚本吗?

标签 windows vbscript batch-file zip scripting

Windows XP/Vista/2003/2008 中内置的 ZIP 压缩是否完全可以编写脚本?我必须从 BAT/CMD 文件调用什么可执行文件?还是可以用 VBScript 实现?

我意识到使用 WinZip 是可能的, 7-Zip和其他外部应用程序,但我正在寻找不需要安装外部应用程序的东西。

最佳答案

zip 有 VBA 方法和 unzip还使用内置压缩的窗口,这应该可以让您了解系统的运行方式。您可以将这些方法构建到您选择的脚本语言中。

基本原理是,在 Windows 中,您可以将 zip 文件视为一个目录,并可以将其复制到其中或从中复制出来。因此,要创建一个新的 zip 文件,您只需创建一个扩展名为 .zip 的文件,该文件具有空 zip 文件的正确标题。然后关闭它,并告诉 Windows 您想将文件复制到其中,就好像它是另一个目录一样。

解压缩更容易 - 只需将其视为目录即可。

以防网页再次丢失,这里有一些相关的代码片段:

zip

Sub NewZip(sPath)
'Create empty Zip File
'Changed by keepITcool Dec-12-2005
    If Len(Dir(sPath)) > 0 Then Kill sPath
    Open sPath For Output As #1
    Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0)
    Close #1
End Sub


Function bIsBookOpen(ByRef szBookName As String) As Boolean
' Rob Bovey
    On Error Resume Next
    bIsBookOpen = Not (Application.Workbooks(szBookName) Is Nothing)
End Function


Function Split97(sStr As Variant, sdelim As String) As Variant
'Tom Ogilvy
    Split97 = Evaluate("{""" & _
                       Application.Substitute(sStr, sdelim, """,""") & """}")
End Function

Sub Zip_File_Or_Files()
    Dim strDate As String, DefPath As String, sFName As String
    Dim oApp As Object, iCtr As Long, I As Integer
    Dim FName, vArr, FileNameZip

    DefPath = Application.DefaultFilePath
    If Right(DefPath, 1) <> "\" Then
        DefPath = DefPath & "\"
    End If

    strDate = Format(Now, " dd-mmm-yy h-mm-ss")
    FileNameZip = DefPath & "MyFilesZip " & strDate & ".zip"

    'Browse to the file(s), use the Ctrl key to select more files
    FName = Application.GetOpenFilename(filefilter:="Excel Files (*.xl*), *.xl*", _
                    MultiSelect:=True, Title:="Select the files you want to zip")
    If IsArray(FName) = False Then
        'do nothing
    Else
        'Create empty Zip File
        NewZip (FileNameZip)
        Set oApp = CreateObject("Shell.Application")
        I = 0
        For iCtr = LBound(FName) To UBound(FName)
            vArr = Split97(FName(iCtr), "\")
            sFName = vArr(UBound(vArr))
            If bIsBookOpen(sFName) Then
                MsgBox "You can't zip a file that is open!" & vbLf & _
                       "Please close it and try again: " & FName(iCtr)
            Else
                'Copy the file to the compressed folder
                I = I + 1
                oApp.Namespace(FileNameZip).CopyHere FName(iCtr)

                'Keep script waiting until Compressing is done
                On Error Resume Next
                Do Until oApp.Namespace(FileNameZip).items.Count = I
                    Application.Wait (Now + TimeValue("0:00:01"))
                Loop
                On Error GoTo 0
            End If
        Next iCtr

        MsgBox "You find the zipfile here: " & FileNameZip
    End If
End Sub

解压

Sub Unzip1()
    Dim FSO As Object
    Dim oApp As Object
    Dim Fname As Variant
    Dim FileNameFolder As Variant
    Dim DefPath As String
    Dim strDate As String

    Fname = Application.GetOpenFilename(filefilter:="Zip Files (*.zip), *.zip", _
                                        MultiSelect:=False)
    If Fname = False Then
        'Do nothing
    Else
        'Root folder for the new folder.
        'You can also use DefPath = "C:\Users\Ron\test\"
        DefPath = Application.DefaultFilePath
        If Right(DefPath, 1) <> "\" Then
            DefPath = DefPath & "\"
        End If

        'Create the folder name
        strDate = Format(Now, " dd-mm-yy h-mm-ss")
        FileNameFolder = DefPath & "MyUnzipFolder " & strDate & "\"

        'Make the normal folder in DefPath
        MkDir FileNameFolder

        'Extract the files into the newly created folder
        Set oApp = CreateObject("Shell.Application")

        oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).items

        'If you want to extract only one file you can use this:
        'oApp.Namespace(FileNameFolder).CopyHere _
         'oApp.Namespace(Fname).items.Item("test.txt")

        MsgBox "You find the files here: " & FileNameFolder

        On Error Resume Next
        Set FSO = CreateObject("scripting.filesystemobject")
        FSO.deletefolder Environ("Temp") & "\Temporary Directory*", True
    End If
End Sub

关于windows - Windows 的内置 ZIP 压缩可以编写脚本吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30211/

相关文章:

c++ - Windows WriteFileGather 函数将最后一个错误设置为 ERROR_INVALID_PARAMETER

service - 如何使用vbscript检查服务状态?

windows - 重命名文件名中包含空格的文件

windows - %* 在批处理文件中是什么意思?

c++ - 如何在 Windows 上编译 Clang

windows - 将命令的输出设置为变量

mysql - 如何允许用户更轻松地在用户计算机上安装 MySQL,以便他们可以通过 Java 应用程序连接到它?

javascript - 如何在 VBScript 中引用 Photoshop 文件夹对象

batch-file - 如何将两个或多个命令一起放入批处理文件

windows - 将文本文件中的字符串提取到另一个文本文件中?在.bat中