有什么方法可以压缩经典ASP中的文件夹(例如ZIP格式)?

时间:2020-07-11 13:27:15

标签: vbscript asp-classic zip

我必须经典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代码用作我的网络服务器。我没有,并且无法下载其他压缩实用程序。)< / p>

P.S。这听起来像How to convert folder to zip. file in asp classic的副本。但是,唯一的答案不能解释使用经典ASP进行压缩(而是继续使用ASP.NET),并且Andrew提供的链接已过期。另外,user1649028的代码导致错误。该帖子是8年前创建的,似乎该帖子不再有进一步的活动。

2 个答案:

答案 0 :(得分: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
'---------------------------------------------------------------------

答案 1 :(得分:0)

编辑:对不起,我忽略了“无法下载其他压缩实用程序”,也许做一些研究,看看是否可以以类似的方式使用本机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(Folder) & """"

收件人:

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

相关问题