Here's a .NET 3.5 version.
Basic testing done.
public static class ZipHelper
{
public static void ZipFiles(string pPath, IList pFiles)
{
CompressionOption tCompressionLevel = CompressionOption.Normal;
ZipFiles(pPath, pFiles, tCompressionLevel);
}
public static void ZipFiles(string pPath, IList pFiles, CompressionOption pCompressionLevel)
{
using (FileStream tFileStream = new FileStream(pPath, FileMode.Create))
{ ZipHelper.ZipFilesToStream(tFileStream, pFiles, pCompressionLevel);}
}
public static byte[] ZipFilesToByteArray(IList pFiles)
{
CompressionOption tCompressionLevel = CompressionOption.Normal;
return ZipFilesToByteArray(pFiles, tCompressionLevel);
}
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();
}
return tZipBytes;
}
public static void Unzip(string pZipPath, string pBaseFolder)
{
using (FileStream tFileStream = new FileStream(pZipPath, FileMode.Open))
{ ZipHelper.UnzipFilesFromStream(tFileStream, pZipPath); }
}
public static void UnzipFromByteArray(byte[] pZipData, string pBaseFolder)
{
using (MemoryStream tMemoryStream = new MemoryStream(pZipData))
{ ZipHelper.UnzipFilesFromStream(tMemoryStream, 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); }
}
}
}
}
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); }
}
}
}
}
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); }
}
private static string GetFileExtentionName(string pPath)
{
string tExtention = Path.GetExtension(pPath);
if (!string.IsNullOrEmpty(tExtention) && tExtention.StartsWith("."))
{ tExtention = tExtention.Substring(1); }
return tExtention;
}
}