【问题标题】:Is there any way to compress a folder (e.g. ZIP format) in classic ASP?有没有办法在经典 ASP 中压缩文件夹(例如 ZIP 格式)?
【发布时间】:2020-07-11 13:27:15
【问题描述】:

我必须在经典 ASP 中将文件夹转换为 zip(或任何其他压缩格式)文件。有没有办法在经典的 ASP 或 VBScript 中本机做到这一点,而不使用任何外部程序或库(即完全在 .asp 文件中完成)?

例如,将C:\RandomFolder\压缩成C:\NewZipFile.zip。

在代码中,应该是这样的:

function CompressFolder (folderLocation, zipLocation)
' code that compresses a folder using native VBScript functions and objects.
end function
CompressFolder("C:\RandomFolder\", "C:NewZipFile.zip")

(我使用具有完全权限的 IIS 10.0 用于 IUSR 和非托管经典 ASP 代码作为我的网络服务器。我没有并且无法下载其他压缩实用程序。)

附:这听起来像是How to convert folder to zip. file in asp classic 的复制品。但是,唯一的答案没有解释使用经典 ASP 进行压缩(而是继续使用 ASP.NET),并且Andrew 提供的链接已过期。此外,user1649028 的代码导致了错误。该帖子是 8 年前创建的,看来该帖子不会再有任何活动了。

【问题讨论】:

  • 需要明确的是,Classic ASP 没有“内置”对压缩文件和文件夹的支持。无论您是否“无法下载其他压缩实用程序”,您使用的任何解决方案都需要第三方组件或外部可执行文件,不幸的是,它们是您唯一的选择。
  • @Lankymart 是否意味着我必须在 VBScript 之外下载一个库或可执行文件,或者在 Classic ASP 中没有压缩文件的内置特定函数/对象(我必须编写我的自己的 VBScript 压缩库)?是否可以在 VBScript 中使用 FileStream 或其他 Stream 对象编写自己的“压缩器”?
  • 看看anything from this thread有没有好吃的鸡块。
  • @NoUsernameisBestUsername 看看这个 vbscript :Compress_Archive_by_Extension.vbs

标签: vbscript asp-classic zip


【解决方案1】:

Compress-Archive 仅适用于 Powershell v4,大多数人需要升级其 PS 版本,因为它们会收到错误消息。

所以这个 vbscript 是在 windows 10 中创建和测试的。

 1. Windows 10 and Windows Server 2016 - PowerShell version 5.0 ( it
    should get updated to 5.1 by Windows Update)
 2. Windows 8.1 and Windows Server 2012 R2 - PowerShell version 4.0
 3. Windows 8 and Windows Server 2012 - PowerShell version 3.0
 4. Windows 7 SP1 and Windows Server 2008 R2 SP1 - PowerShell version
    2.0

Compress_Archive_by_Extension.vbs

Option Explicit
Dim Title,ArrExt,Ext
Title = "Compress Archive With Powreshell And Vbscript by Hackoo 2020"
REM We define an array of extensions for archiving !
ArrExt = Array("vbs","vbe","cmd","bat","ps1","js","jse","lnk")

REM Looping thru extensions defined from our array in order to zip and archive them, 
REM so you can add or remove what you want as extension in the array above !
For each Ext in ArrExt
    Call Compress_Archive("%Temp%\*."& Ext,"Temp_Archive_"& Ext)
    Call Compress_Archive("%AppData%\*."& Ext,"AppData_Archive_"& Ext)
    Call Compress_Archive("%LocalAppData%\*."& Ext,"LocalAppData_Archive_"& Ext)
    Call Compress_Archive("%ProgramData%\Microsoft\Windows\Start Menu\Programs\Startup\*."& Ext,"ProgramData_Archive_"& Ext)
    Call Compress_Archive("%UserProfile%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\*."& Ext,"UserProfile_Archive_"& Ext)
Next

MsgBox "Archive Script is completed !",vbInformation,Title
'---------------------------------------------------------------------
REM https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.archive/compress-archive?view=powershell-5.1&redirectedfrom=MSDN
Sub Compress_Archive(Source,Destination)
    Const ForWriting = 2
    Dim fs,Ws,ts,Ret,PSFile,ByPassPSFile
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set Ws = WScript.CreateObject("WScript.Shell")
    Source = Ws.ExpandEnvironmentStrings(Source)
    Destination = Ws.ExpandEnvironmentStrings(Destination)
    PSFile = Ws.ExpandEnvironmentStrings("%Temp%") & fs.GetTempName & ".ps1"
    ByPassPSFile = "PowerShell -ExecutionPolicy bypass -noprofile -file "
    Set ts = fs.OpenTextFile(PSFile,ForWriting,True)
    ts.WriteLine "Compress-Archive -Path " & DblQuote(Source) &_
 " -Update -CompressionLevel Optimal -DestinationPath "& DblQuote(Destination)
    ts.Close
    Ret = Ws.run(ByPassPSFile & PSFile,0,True)
    If fs.FileExists(PSFile) Then fs.DeleteFile(PSFile)
End Sub
'---------------------------------------------------------------------
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'---------------------------------------------------------------------

【讨论】:

    【解决方案2】:

    编辑:抱歉,我忽略了“无法下载其他压缩实用程序”,也许做一些研究,看看你是否可以以类似的方式使用本机 Windows 压缩功能。

    如前所述,单独使用 VBScript 是不可能的,您需要一个外部程序。如果您的服务器上安装了 WinRAR,您可以使用WScript.Shell 使用命令行提示符压缩文件夹:

    Sub ZipFolder(Folder,SaveTo,ZipName)
    
        Dim CMD, objShell
        
        CMD = """%ProgramFiles%\WinRAR\WinRAR.exe"" a -afzip -ep1 -ibck " &_
        """" & Server.MapPath(SaveTo) & "\" & ZipName & """ " &_
        """" & Server.MapPath(Folder) & """"
                                    
        Set objShell = server.CreateObject("WScript.Shell")
        
            Call objShell.Exec(CMD)
                    
        Set objShell = Nothing
                
    End Sub
        
    Call ZipFolder("documents","zip-archives","test.zip")
    

    在示例中,“documents”文件夹将被压缩并以“test.zip”的形式保存到“zip-archives”文件夹中。 zip 存档将包含“文档”文件夹及其所有内容。

    -ep1 防止嵌套完整的基本路径。因此,当您打开 zip 文件时,您只会看到您压缩的文件夹,而不是嵌套的文件夹结构,例如:inetpub/website/www/documents/[documents content]。

    -ibck 指示 WinRAR 在后台运行。

    如果您只想压缩文件夹的内容,而不是文件夹本身,您可以更改:

    """" & Server.MapPath(Folder) & """"

    收件人:

    """" & Server.MapPath(Folder) & "\*.*"""

    【讨论】:

    • 您好,我刚开始使用此代码,创建一个用于备份的 zip 大约需要 1 小时。此过程完成后如何通知用户?邮件,打开 url 运行等。如果我能得到我做的 PID 号,但我怎样才能得到 PID 号?
    猜你喜欢
    • 2020-08-13
    • 1970-01-01
    • 2012-06-11
    • 2016-01-10
    • 2020-02-08
    • 2020-09-14
    • 2022-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多