Wrapper I wrote for
SharpZipLib:
using System;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using System.Collections.Generic;
using System.Linq;
using ICSharpCode.SharpZipLib.Core;
public static class SharpZipLibHelper
{
#region Functions
private static bool _ignore(List<string> excludes, string fileName)
{
foreach (var exclude in excludes)
{
if (exclude.IndexOf("*") > -1)
{
var fileExtension = Path.GetExtension(fileName);
var excludeExtension = Path.GetExtension(exclude);
if (string.Equals(excludeExtension, fileExtension, StringComparison.InvariantCultureIgnoreCase))
return true;
}
else
{
if (string.Equals(fileName, exclude, StringComparison.InvariantCultureIgnoreCase))
return true;
}
}
return false;
}
private static void _compressFolder(string path, ZipOutputStream zipStream, int folderOffset, List<string> excludes = null)
{
var files = Directory.GetFiles(path);
foreach (string filename in files)
{
FileInfo fi = new FileInfo(filename);
if (excludes == null) excludes = new List<string>();
if (_ignore(excludes, fi.Name)) continue;
string entryName = filename.Substring(folderOffset);
entryName = ZipEntry.CleanName(entryName);
ZipEntry newEntry = new ZipEntry(entryName);
newEntry.DateTime = fi.LastWriteTime;
newEntry.Size = fi.Length;
zipStream.PutNextEntry(newEntry);
byte[] buffer = new byte[4096];
using (FileStream streamReader = File.OpenRead(filename))
{
StreamUtils.Copy(streamReader, zipStream, buffer);
}
zipStream.CloseEntry();
}
string[] folders = Directory.GetDirectories(path);
foreach (string folder in folders)
{
_compressFolder(folder, zipStream, folderOffset, excludes);
}
}
#endregion
#region Methods
public static void CreateZipFromFolder(string folderToZipPath, string zipFullFilePath, List<string> excludes = null)
{
FileStream fsOut = File.Create(zipFullFilePath);
ZipOutputStream zipStream = new ZipOutputStream(fsOut);
zipStream.SetLevel(9);
int folderOffset = folderToZipPath.Length + (folderToZipPath.EndsWith("\\") ? 0 : 1);
_compressFolder(folderToZipPath, zipStream, folderOffset, excludes);
zipStream.IsStreamOwner = true;
zipStream.Close();
}
public static void ExtractZipFile(string zipFile, string destinationFolder, List<string> excludes = null, string password = null)
{
ZipFile zf = null;
try
{
FileStream fs = File.OpenRead(zipFile);
zf = new ZipFile(fs);
if (!String.IsNullOrEmpty(password))
{
zf.Password = password;
}
foreach (ZipEntry zipEntry in zf)
{
if (!zipEntry.IsFile)
continue;
if (excludes != null && zipEntry.IsFile)
{
if (_ignore(excludes, zipEntry.Name)) continue;
}
String entryFileName = zipEntry.Name;
byte[] buffer = new byte[4096];
Stream zipStream = zf.GetInputStream(zipEntry);
String fullZipToPath = Path.Combine(destinationFolder, entryFileName);
string directoryName = Path.GetDirectoryName(fullZipToPath);
if (directoryName.Length > 0)
Directory.CreateDirectory(directoryName);
using (FileStream streamWriter = File.Create(fullZipToPath))
{
StreamUtils.Copy(zipStream, streamWriter, buffer);
}
}
}
finally
{
if (zf != null)
{
zf.IsStreamOwner = true;
zf.Close();
}
}
}
#endregion
}