Saturday, May 16, 2009

how to compress files using asp

how to compress files using asp

most of the time we require to compress file and save it to perticular localtion , or you can
use the for any purpose as u want .

for this we requires Server supporting WScript.Shell - most hosts won't allow this.

here u can get the download link , here u can specify either relative path or absolute path

before that please download this file, click here to download

-zipfile.asp
< % 

'---------------------------------------------------------
'CreateZipFile: function to create zip file at perticular location
'ZipPath - 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:\foldername\*.*")
'NOTE: this code requires ZIP.EXE utility in the same location
' as this file.
'---------------------------------------------------------
Sub CreateZipFile(ZipPath, arrFilesPath)
Const PKZIP_FILE_NAME="zip.exe"
Dim strCommand, objShell, objFSO
Dim x

'first check zip.exe file is exists:
Set objFSO=Server.CreateObject("Scripting.FileSystemObject")
If Not(objFSO.FileExists(Server.MapPath(ZIP_FILE_NAME) )) Then
Set objFSO=Nothing
Err.Raise 20000, "Zip File Creator", "zip utility not found: "&Server.MapPath(ZIP_FILE_NAME)
End If

'delete current file:
If objFSO.FileExists(ZipPath) Then
objFSO.DeleteFile(ZipPath)
End If
Set objFSO=Nothing

'batch command:

strCommand=Server.MapPath(ZIP_FILE_NAME)&" -add "&ZipPath&" "
For x=0 To UBound(arrFilesPath)
strCommand=strCommand&arrFilesPath(x)
If x < UBound(arrFilesPath) Then strCommand=strCommand&" "
Next

'execute:
Set objShell=Server.CreateObject("WScript.Shell")
objShell.Run strCommand, 0, True 'wait!

'done.
Set objShell=Nothing
End Sub
%>

now u can call your CreateZipFile() function
-checkZipFile.asp


< % 

Call checkZipFile()

Sub checkZipFile()
'create zip and give link:
Call CreateZipFile(Server.MapPath("Testzip.zip"), Array(Server.MapPath("images")&"\*.*"))
Response.Write("click here download zip")
End Sub
%>
here u can get the download link , here u can specify either relative path or absoutate path

No comments:

Post a Comment