Vbscript 有没有办法在经典ASP中压缩文件夹(例如ZIP格式)?

Vbscript 有没有办法在经典ASP中压缩文件夹(例如ZIP格式)?,vbscript,asp-classic,zip,Vbscript,Asp Classic,Zip,我必须将文件夹转换为经典ASP中的zip文件(或任何其他压缩格式)。在传统ASP或VBScript中是否有任何方法可以在不使用任何外部程序或库的情况下以本机方式执行此操作(即完全在.ASP文件中执行) 例如,将C:\RandomFolder\压缩到C:\NewZipFile.zip 在代码中,它应该类似于: function CompressFolder (folderLocation, zipLocation) ' code that compresses a folder using nat

我必须将文件夹转换为经典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")
(我正在使用具有IUSR和非托管经典ASP代码的完全权限的IIS 10.0作为我的Web服务器。我没有并且无法下载其他压缩实用程序


顺便说一句,这听起来像是一个复制品。但是,唯一的答案并没有解释使用经典ASP进行压缩(而是继续使用ASP.NET),并且提供的链接已过期。此外,的代码导致了一个错误。这篇文章是8年前创建的,似乎没有进一步的活动了。

Compress Archive
仅适用于Powershell v4,大多数人都需要升级他们的PS版本,因为他们会遇到错误

因此,这个vbscript是在Windows10中创建和测试的

通过扩展名.vbs压缩归档文件


编辑:对不起,我忽略了“无法下载额外的压缩实用程序”,也许可以做一些研究,看看您是否可以以类似的方式使用本机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存档将包含“documents”文件夹及其所有内容

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

-ibck
指示WinRAR在后台运行

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

“”&Server.MapPath(文件夹)和“”

致:


“”&Server.MapPath(Folder)和“\*.*”

很明显,经典ASP对压缩文件和文件夹没有“内置”支持。无论您使用何种解决方案,都需要第三方组件或外部可执行文件,无论您是否“无法下载其他压缩实用程序”,不幸的是,它们是您唯一的选择。@Lankymart是否意味着我必须下载VBScript之外的库或可执行文件,或者,没有内置的特定函数/对象在经典ASP中压缩文件(我必须用VBScript编写自己的压缩库)?是否可以使用VBScript中的FileStream或其他流对象编写您自己的“压缩器”?查看是否提供了任何美味的金块。@NoUsernameisBestUsername查看此VBScript:
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
'---------------------------------------------------------------------
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")