Is there any way to compress a folder (e.g. ZIP format) in classic ASP?

I must convert a folder into a zip (or any other compressed format) file in classic ASP. Is there any way to do this in classic ASP or VBScript natively without using any external program or library (i.e. do it in completely inside the .asp file)?

For example, compress C:\RandomFolder\ into C:\NewZipFile.zip.

In code, it should be something like:

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

(I am using IIS 10.0 with full permissions for IUSR and unmanaged classic ASP code as my webserver. I do not have and cannot download additional zipping utilities.)

P.S. This may sound like a duplicate of How to convert folder to zip. file in asp classic. However, the only answer does not explain zipping with classic ASP (instead proceeds to use ASP.NET) and the link provided by Andrew has expired. In addition, user1649028's code resulted in an error. The post was created 8 years ago, and it seems that there would be no further activity with the post.

Upvotes: 1

Views: 738

Answers (2)

Adam
Adam

Reputation: 926

EDIT: Sorry, I overlooked the "cannot download additional zipping utilities", maybe do some research to see if you can use the native windows compression feature in a similar fashion.

As already mentioned, it's not possible to do with VBScript alone, you will need an external program. If you have WinRAR installed on your server you can use WScript.Shell to zip folders using a command line prompt:

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")

In the example the "documents" folder will be zipped and saved to the "zip-archives" folder as "test.zip". The zip archive will contain the "documents" folder and all its content.

-ep1 prevents the full base path from being nested. So when you open the zip file you'll just see the folder you zipped, rather than a nested folder structure like: inetpub/website/www/documents/[documents content].

-ibck instructs WinRAR to run in the background.

If you want to only zip the contents of a folder, and not the folder itself, you can change:

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

To:

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

Upvotes: 0

Hackoo
Hackoo

Reputation: 18837

Compress-Archive is only available with Powershell v4 and most will need to upgrade their PS version because they will get an error.

So this vbscript is created and tested in 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://learn.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
'---------------------------------------------------------------------

Upvotes: 1

Related Questions