Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Zipping using System.IO.Compression

0.00/5 (No votes)
24 Jan 2012CPOL 22.2K  
Here's a .NET 3.5 version.Basic testing done./// /// http://www.codeproject.com/Tips/315115/Zip-using-System-IO-Compression /// /// mods by GNoter ~ 20120124_094448 /// - Made dotNET v3.5 compatible /// - dotNET v3.5 does not have the optional param capability that dotNET...
Here's a .NET 3.5 version.
Basic testing done.

C#
/// 
	///  http://www.codeproject.com/Tips/315115/Zip-using-System-IO-Compression
	///  
	/// mods by GNoter ~ 20120124_094448
	/// - Made dotNET v3.5 compatible
	///			- dotNET v3.5 does not have the optional param capability that dotNET v4.0 does
	///			- added extra function calls where needed
	/// - Added [limited] xml commenting
	/// - IEnumerable type changed to IList, as IEnumerable not available in dotNET v3.5.
	/// - Changed param and variable names to be more unique and more identifiable (param vs. variable vs. keyword)
	///			- case in point: "files" is too close to "System.IO.Files"; yeah, we know the diff, but not our managers...
	/// - Performance optimization:
	///    - Changed foreach to for(int z=0) loop where possible; foreach is slower than a for loop
	/// - Added "endOfCurlyBracket" markers. A personal preference, I don't care to try and guess what the end curly-bracket is for
	/// 
	public static class ZipHelper
	{

		/// 
		///
		/// 
		/// 
		/// 
		public static void ZipFiles(string pPath, IList pFiles)
		{
			CompressionOption tCompressionLevel = CompressionOption.Normal;
			ZipFiles(pPath, pFiles, tCompressionLevel);
		}//void ZipFiles(string pPath, IList pFiles)


		/// 
		/// 
		/// 
		/// 
		/// 
		/// 
		public static void ZipFiles(string pPath, IList pFiles, CompressionOption pCompressionLevel)
		{
			using (FileStream tFileStream = new FileStream(pPath, FileMode.Create))
			{ ZipHelper.ZipFilesToStream(tFileStream, pFiles, pCompressionLevel);}
		}//void ZipFiles(string pPath, IList pFiles, CompressionOption pCompressionLevel)


		/// 
		/// 
		/// 
		/// 
		/// 
		public static byte[] ZipFilesToByteArray(IList pFiles)
		{
			CompressionOption tCompressionLevel = CompressionOption.Normal;
			return ZipFilesToByteArray(pFiles, tCompressionLevel);
		}//byte[] ZipFilesToByteArray(IList pFiles)



		/// 
		/// 
		/// 
		/// 
		/// 
		/// 
		public static byte[] ZipFilesToByteArray(IList pFiles, CompressionOption pCompressionLevel)
		{
			byte[] tZipBytes = default(byte[]);

			using (MemoryStream tMemoryStream = new MemoryStream())
			{
				ZipHelper.ZipFilesToStream(tMemoryStream, pFiles, pCompressionLevel);
				tMemoryStream.Flush();
				tZipBytes = tMemoryStream.ToArray();
			}//using (MemoryStream tMemoryStream = new MemoryStream())

			return tZipBytes;
		}//byte[] ZipFilesToByteArray(IList pFiles, CompressionOption pCompressionLevel)


		/// 
		/// 
		/// 
		/// 
		/// 
		public static void Unzip(string pZipPath, string pBaseFolder)
		{
			using (FileStream tFileStream = new FileStream(pZipPath, FileMode.Open))
			{ ZipHelper.UnzipFilesFromStream(tFileStream, pZipPath); }
		}//void Unzip(string pZipPath, string pBaseFolder)


		/// 
		/// 
		/// 
		/// 
		/// 
		public static void UnzipFromByteArray(byte[] pZipData, string pBaseFolder)
		{
			using (MemoryStream tMemoryStream = new MemoryStream(pZipData))
			{ ZipHelper.UnzipFilesFromStream(tMemoryStream, pBaseFolder); 	}
		}//void UnzipFromByteArray(byte[] pZipData, string pBaseFolder)


		/// 
		/// 
		/// 
		/// 
		/// 
		/// 
		private static void ZipFilesToStream(Stream pDestination, IList pFiles, CompressionOption pCompressionLevel)
		{
			Uri tFileURI;
			string tContentType, tPath;

			using (Package package = Package.Open(pDestination, FileMode.Create))
			{
				for (int z = 0; z < pFiles.Count; z++)
				{
					tPath = pFiles[z];
					tFileURI = new Uri(@"/" + Path.GetFileName(tPath), UriKind.Relative);
					tContentType = @"data/" + ZipHelper.GetFileExtentionName(tPath);
					using (Stream tZipStream = package.CreatePart(tFileURI, tContentType, pCompressionLevel).GetStream())
					{
						using (FileStream tFileStream = new FileStream(tPath, FileMode.Open))
						{ ZipHelper.CopyStream(tFileStream, tZipStream); }
					}//using (Stream tZipStream = package.CreatePart(tFileURI, tContentType, pCompressionLevel).GetStream())
				}//for (int z = 0; z < pFiles.Count; z++)
			}//using (Package package = Package.Open(destination, FileMode.Create))
		}//void ZipFilesToStream(Stream pDestination, IList pFiles, CompressionOption pCompressionLevel)


		/// 
		/// 
		/// 
		/// 
		/// 
		private static void UnzipFilesFromStream(Stream pSource, string pBaseFolder)
		{
			string tPath = string.Empty;

			if (!Directory.Exists(pBaseFolder)) { Directory.CreateDirectory(pBaseFolder); }
			using (Package tPackage = Package.Open(pSource, FileMode.Open))
			{
				foreach (PackagePart zipPart in tPackage.GetParts())
				{
					tPath = Path.Combine(pBaseFolder, zipPart.Uri.ToString().Substring(1));
					using (Stream tZipStream = zipPart.GetStream())
					{
						using (FileStream tFileStream = new FileStream(tPath, FileMode.Create))
						{ ZipHelper.CopyStream(tZipStream, tFileStream); }
					}//using (Stream tZipStream = zipPart.GetStream())
				}
			}//using (Package tPackage = Package.Open(pSource, FileMode.Open))
		}//void UnzipFilesFromStream(Stream pSource, string pBaseFolder)


		/// 
		/// 
		/// 
		/// 
		/// 
		private static void CopyStream(Stream pSource, Stream pDestination)
		{
			const int tBufferSize = 0x1000;
			byte[] tBuffer = new byte[tBufferSize];
			int tCount = 0;

			while ((tCount = pSource.Read(tBuffer, 0, tBufferSize)) > 0)
			{ pDestination.Write(tBuffer, 0, tCount); }
		}//void CopyStream(Stream pSource, Stream pDestination)


		/// 
		/// 
		/// 
		/// 
		/// 
		private static string GetFileExtentionName(string pPath)
		{
			string tExtention = Path.GetExtension(pPath);
			if (!string.IsNullOrEmpty(tExtention) && tExtention.StartsWith("."))
			{ tExtention = tExtention.Substring(1); }

			return tExtention;
		}//string GetFileExtentionName(string pPath)
	}//static class ZipHelper

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)