using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO.Compression;
using System.IO;
using System.Diagnostics;
using System.Xml;
using ICSharpCode.SharpZipLib.BZip2;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Checksums;
namespace Sp.DBUtility
{
public class ZipHelper
{
public void ZipFile(string fileToZip, string zippedFile)
{
if (string.IsNullOrEmpty(fileToZip))
throw new ArgumentNullException("fileToZip");
using (ZipOutputStream stream = new ZipOutputStream(File.Create(zippedFile)))
{
using (FileStream fs = File.OpenRead(fileToZip))
{
ZipEntry entry = new ZipEntry(Path.GetFileName(fileToZip));
entry.DateTime = DateTime.UtcNow;
entry.Size = fs.Length;
stream.PutNextEntry(entry);
int readLen = 0;
int maxlength = 10000;
byte[] buffer = new byte[maxlength];
readLen = fs.Read(buffer, 0, buffer.Length);
for (int i = 0; ; i++)
{
if (readLen > 0)
{
stream.Write(buffer, 0, readLen);
readLen = fs.Read(buffer, 0, buffer.Length);
}
else
{
break;
}
}
fs.Close();
stream.Close();
}
}
}
public void ZipDir(string zippedDir, string zippedFile, bool recurse, string fileFilter)
{
ICSharpCode.SharpZipLib.Zip.FastZip fastZip = new ICSharpCode.SharpZipLib.Zip.FastZip();
fastZip.CreateZip(zippedFile, zippedDir, recurse, fileFilter);
}
public byte[] Zip(string text, string zipEntryName)
{
byte[] inputByteArray = Encoding.UTF8.GetBytes(text);
return this.ZipBytes(inputByteArray, zipEntryName);
}
public byte[] ZipBytes(byte[] bytes, string zipEntryName)
{
byte[] inputByteArray = bytes;
using (MemoryStream ms = new MemoryStream())
{
// Check the #ziplib docs for more information
using (ZipOutputStream zipOut = new ZipOutputStream(ms))
{
ZipEntry ZipEntry = new ZipEntry(zipEntryName);
zipOut.PutNextEntry(ZipEntry);
zipOut.SetLevel(9);
zipOut.Write(inputByteArray, 0, inputByteArray.Length);
zipOut.Finish();
zipOut.Close();
// Return the zipped contents
return ms.ToArray();
}
}
}
}
}