팡이네

* 특정 폴더에 있는 파일 압축
* ICSharpCode.SharpZipLib.dll 사용
* folderPath : 폴더 경로(서버 물리 경로)

	public void DownloadZipFile(string folderPath)
	{
		if (Directory.Exists(folderPath))
		{
			var zipFilePath = string.Format("{0}.zip", folderPath);

			//-----------------------------------
			//파일 압축
			//-----------------------------------
			try
			{
				var files = Directory.GetFiles(folderPath);

				// 'using' statements guarantee the stream is closed properly which is a big source
				// of problems otherwise.  Its exception safe as well which is great.
				using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath)))
				{
					s.SetLevel(9); // 0 - store only to 9 - means best compression

					byte[] buffer = new byte[4096];

					foreach (string file in files)
					{
						// Using GetFileName makes the result compatible with XP
						// as the resulting path is not absolute.
						var entry = new ZipEntry(Path.GetFileName(file));

						// Setup the entry data as required.

						// Crc and size are handled by the library for seakable streams
						// so no need to do them here.

						// Could also use the last write time or similar for the file.
						entry.DateTime = DateTime.Now;
						s.PutNextEntry(entry);

						using (FileStream fs = File.OpenRead(file))
						{
							// Using a fixed size buffer here makes no noticeable difference for output
							// but keeps a lid on memory usage.
							int sourceBytes;
							do
							{
								sourceBytes = fs.Read(buffer, 0, buffer.Length);
								s.Write(buffer, 0, sourceBytes);
							} while (sourceBytes > 0);
						}
					}

					// Finish/Close arent needed strictly as the using statement does this automatically

					// Finish is important to ensure trailing information for a Zip file is appended.  Without this
					// the created file would be invalid.
					s.Finish();

					// Close is important to wrap things up and unlock the file.
					s.Close();
				}
			}
			catch
			{
				throw new Exception("파일을 압축하는 도중 오류가 발생하였습니다.");
			}

			//-----------------------------------
			//폴더 삭제
			//-----------------------------------
			try
			{
				Directory.Delete(folderPath, true);
			}
			catch
			{
				throw new Exception("압축폴더를 다운로드 하는 도중 오류가 발생하였습니다.");
			}

			//-----------------------------------
			//압축파일 다운로드
			//-----------------------------------
			try
			{
				this.DownloadZipFile(zipFilePath, true);
			}
			catch
			{
				throw new Exception("압축파일을 다운로드 하는 도중 오류가 발생하였습니다.");
			}
		}
	}

* DownloadZipFile() 서버의 압축파일(ZIP)을 다운로드한다.

	/// 
	/// 서버의 압축파일(ZIP)을 다운로드한다.
	/// 
	/// 서버 파일 경로
	/// 다운로드 후 파일 삭제여부
	public void DownloadZipFile(string filePath, bool deleteAfterDown)
	{
		var fileName = System.IO.Path.GetFileName(filePath);

		//한글 깨어짐 방지
		var saveFileName = HttpUtility.UrlEncode(fileName).Replace("+", "%20");

		var response = System.Web.HttpContext.Current.Response;
		
		//아래 방식을 사용하면 다운로드 후 압축파일이 깨어졌다는 메시지 표시
		/*
		response.ClearContent();
		response.Clear();
		response.ContentType = "application/zip";
		response.Charset = "utf-8";
		response.AddHeader("Content-Disposition", "attachment; filename=" + saveFileName + ";");
		response.TransmitFile(filePath);
		response.Flush();

		if (deleteAfterDown)
		{
			//파일 삭제
			var deleteFile = new System.IO.FileInfo(filePath);
			if (deleteFile.Exists)
			{
				System.IO.File.Delete(filePath);
			}
		}

		response.End();
		*/

		//---------------------------------------------
		//제네릭 처리기 사용
		//---------------------------------------------
		//Response.TransmitFile() 메소드 사용시 압축파일이 깨졌다는 메시지 표시
		response.Redirect(string.Format("~/DownloadZipFile.ashx?fileName={0}", saveFileName));
	}

* DownloadZipFile.ashx 파일 내용

<%@ WebHandler Language="C#" Class="DownloadZipFile" %>

using System;
using System.IO;
using System.Web;

public class DownloadZipFile : IHttpHandler
{
	public void ProcessRequest(HttpContext context)
	{
		var request = context.Request;
		var response = context.Response;
		var fileName = request.QueryString["fileName"];

		if (!string.IsNullOrEmpty(fileName))
		{
			var saveFileName = HttpUtility.UrlEncode(fileName).Replace("+", "%20");
			var fullPath = string.Format("{0}{1}", context.Server.MapPath(@"~/Download/"), fileName);	//다운로드 파일 경로
			
			response.ClearContent();
			response.Clear();
			response.ContentType = "application/zip";
			response.Charset = "utf-8";
			response.AddHeader("Content-Disposition", string.Format(@"attachment; filename=""{0}"";", saveFileName));
			response.TransmitFile(fullPath);
			response.Flush();

			//엑셀 파일 삭제
			var deleteFile = new FileInfo(fullPath);
			if (deleteFile.Exists)
			{
				File.Delete(fullPath);
			}
			
			response.End();
		}
		else
		{
			var script = "<script type='text/javascript'>alert('압축 파일을 찾을 수 없습니다.');history.back();</script>";
			response.Write(script);
			response.End();
		}
	}

	public bool IsReusable
	{
		get
		{
			return false;
		}
	}
}