Crear un archivo ZIP desde ASP clásico

Código ASP clásico

Este código en ASP clásico permite crear archivos ZIP a partir de un conjunto de archivos y directorios especificados. La función principal es CreateZipFile, que recibe como parámetros la ruta completa del archivo ZIP a crear (strZipPath) y un array de cadenas que contienen las rutas de los archivos y directorios a incluir en el archivo ZIP (arrFilesPath). Primero, el código verifica que la utilidad PKZIP25.EXE está presente en el mismo directorio que el archivo ASP. A continuación, se construye un comando de línea de comandos que utiliza PKZIP25.EXE para comprimir los archivos y directorios especificados en el archivo ZIP. Finalmente, el comando se ejecuta mediante la creación de un objeto de shell y se espera hasta que la operación se complete. Si ocurre algún error, se genera una excepción. Este código puede ser útil para reducir el tamaño de los archivos a transmitir en una aplicación web o para crear copias de seguridad de múltiples archivos.

Para crear un ZIP desde ASP clásico necesitamos cubrir los siguientes resquisitos:

1. Un servidor Windows que soporte WScript.Shell  (La mayoría lo soporta).
2. Una utilería llamada PKZIP25 que puede ser descargada en la siguiente dirección: http://www.filewatcher.com/m/PKZIP2...339456.0.0.html
3. El código fuente:

ZipFiles.asp


<%

'---------------------------------------------------------

'CreateZipFile: creating zip file in the given path.

'strZipPath - full path for the zip file, including the zip file name.

'arrFilesPath - array of the files to be zipped, e.g. Array("C:\*.exe", "C:\test\*.*")

'NOTE: this code requires PKZIP25.EXE utility in the same location

'	as this file.

'---------------------------------------------------------

Sub CreateZipFile(strZipPath, arrFilesPath)

	Const PKZIP_FILE_NAME="pkzip25.exe"

	Dim strCommand, objShell, objFSO

	Dim x

	

	'first verify pkzip exists:

	Set objFSO=Server.CreateObject("Scripting.FileSystemObject")

	If Not(objFSO.FileExists( Server.MapPath(PKZIP_FILE_NAME) )) Then

		Set objFSO=Nothing

		Err.Raise 20000, "Zip File Creator", "zip utility not found: "&Server.MapPath(PKZIP_FILE_NAME)

	End If

	'delete current file, if exists:

	If objFSO.FileExists(strZipPath) Then

		objFSO.DeleteFile(strZipPath)

	End If

	Set objFSO=Nothing

	

	'build batch command:

	strCommand=Server.MapPath(PKZIP_FILE_NAME)&" -add "&strZipPath&" "

	'para incluir subfolder strCommand=Server.MapPath(PKZIP_FILE_NAME)&" -add -rec "&strZipPath&" "

	For x=0 To UBound(arrFilesPath)

		strCommand=strCommand&arrFilesPath(x)

		If x
	Next

	

	'execute:

	Set objShell=Server.CreateObject("WScript.Shell")

	objShell.Run strCommand, 0, True 'wait!

	

	'done.

	Set objShell=Nothing

End Sub

%>


Ejemplo de su uso:

 

 

ZipTest.asp

 

 


<% Option Explicit %>



<%

Call TestZipFile()



Sub TestZipFile()

	'create zip and give link:

	Call CreateZipFile(Server.MapPath("myzip.zip"), Array(Server.MapPath("images")&"\*.*"))

	Response.Write("download zip")

End Sub

%>

tags: zip gratis, asp clasico, winrar gratis, como hacer paginas web gratis, programa zip, compresor zip gratis, programa zip, unzip, paginas para hacer, crear un zip, crear un archivo zip