Zip Component, Email Component, Encryption Component ActiveX Control for Zip Compression .NET Components for ASP.NET
ActiveX and .NET Components for Zip Compression, Encryption, Email, XML, S/MIME, HTML Email, Character Encoding, Digital Certificates, FTP, and more ASP Email ActiveX Component


Index of Chilkat Blog Posts

August 23, 2007

Stream Binary .ZIP to ASP Response

This ASP example demonstrates how to write a .zip to a temp directory, stream it to the ASP Response, and then delete the temporary .zip. From the end-user’s perspective, it looks like a simple .zip download.

It uses a freeware "Chilkat FileAccess" ActiveX which can be downloaded here: Freeware ASP FileAccess ActiveX Component.

<%
	
' Return a Zip file created from in-memory data.
set zip = Server.CreateObject("Chilkat.Zip2")
	
' This example will use the Chilkat FileAccess ActiveX, which is a free
' utility component available at:
' http://www.chilkatsoft.com/download/ChilkatFileAccess.zip
set fac = Server.CreateObject("Chilkat.FileAccess")
	
' Any value passed to UnlockComponent begins the 30-day trial.
unlocked = zip.UnlockComponent("30-day trial")
if unlocked then
	
	' Generate a temporary filename for the .zip to be created:
	dirPath = Server.MapPath("/data")
	prefix = "xyz"
	zipFilename = fac.GetTempFilename(dirPath,prefix)
	zip.NewZip zipFilename
	
	' If the files are large, the Zip component may use temporary files.
	' make sure your zip.TempDir property is set to a directory w/ read and write
	' permissions
	zip.TempDir = Server.MapPath("/data")
	
	' Add some directory trees to the zip object.
	recurse = 1
	baseDir = Server.MapPath("/temp/abc123/")
	' The MapPath returns a directory w/out the trailing slash
	baseDir = baseDir & "/*"
	count = zip.AppendFiles(baseDir,recurse)
	if count < 0 then
		Response.Write zip.LastErrorHtml
		Response.End
	end if
	
	baseDir = Server.MapPath("/temp/test111/")
	baseDir = baseDir & "/*"
	count = zip.AppendFiles(baseDir,recurse)
	if count < 0 then
		Response.Write zip.LastErrorHtml
		Response.End
	end if
	
	' Write the .zip file (to the filename used in the NewZip method )
	success = zip.WriteZipAndClose()
	if (success = 0) then
		Response.Write zip.LastErrorHtml
		Response.End
	end if
	
	' Send the .zip to the Response...
	' Use the Chilkat FileAccess ActiveX to stream it to the response output:
	
	' Open the file:
	' The integer arguments to FileOpen are identical to the arguments used
	' by the Microsoft CreateFile Platform SDK function
	' (http://msdn2.microsoft.com/en-us/library/aa363858.aspx)
	
	' Access Modes:
	' GENERIC_READ	(0x80000000)
	' GENERIC_WRITE (0x40000000)
	accessMode = &H80000000
	
	' Share Modes:
	' FILE_SHARE_READ(0x00000001)
	' FILE_SHARE_WRITE(0x00000002)
	shareMode = 1
	
	' Create Dispositions
	' CREATE_NEW          1
	' CREATE_ALWAYS       2
	' OPEN_EXISTING       3
	' OPEN_ALWAYS         4
	' TRUNCATE_EXISTING   5
	createDisp = 3
	
	' Attributes:
	' FILE_ATTRIBUTE_READONLY         0x00000001
	' FILE_ATTRIBUTE_HIDDEN           0x00000002
	' FILE_ATTRIBUTE_SYSTEM           0x00000004
	' FILE_ATTRIBUTE_DIRECTORY        0x00000010
	' FILE_ATTRIBUTE_ARCHIVE          0x00000020
	' FILE_ATTRIBUTE_NORMAL           0x00000080
	' FILE_ATTRIBUTE_TEMPORARY	   0x00000100
	fileAttr = 0
	
	success = fac.FileOpen(zipFilename,accessMode,shareMode,createDisp,fileAttr)
	if (success = 0) then
		Response.Write fac.LastErrorHtml
		Response.End
	end if
	
	' Send the response header.
	' The filename that the end-user will see is specified in the Content-Disposition header.
	Response.Buffer = True
	Response.Expires = 0
	Response.ContentType = "application/zip"
	Response.AddHeader "Content-transfer-encoding", "binary"
	Response.AddHeader "Content-Disposition", "attachment;filename=test.zip"
	
	' Stream the file to the output:
	Dim dataChunk
	do while (fac.EndOfFile = 0)
		' Read chunks of 4K at a time...
		dataChunk = fac.FileRead(4096)
		if (not (isNull(dataChunk))) then
			Response.BinaryWrite(dataChunk)
			Response.Flush()
		end if
	loop
	
	fac.FileClose
	
	Response.Flush()
	
	' Delete the .zip we just created..
	success = fac.FileDelete(zipFilename)
end if
	
%>


Privacy Statement. Copyright 2000-2011 Chilkat Software, Inc. All rights reserved.
Send feedback to support@chilkatsoft.com

Components for Microsoft Windows XP, 2000, 2003 Server, Vista, Windows 7, and Windows 95/98/NT4.