using Ionic.Zip;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
namespace PepLib
{
public static class ZipHelper
{
///
/// Returns the files that match the specified pattern.
///
/// Input zip file.
/// Pattern to match.
/// Names of the files that match the pattern.
/// Data of the files that match the pattern.
///
public static int ExtractByPattern(string file, string pattern, out string[] names, out Stream[] streams)
{
var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
var zipStream = new ZipInputStream(fileStream);
var nameList = new List();
var streamList = new List();
ZipEntry theEntry;
while ((theEntry = zipStream.GetNextEntry()) != null)
{
if (!Regex.IsMatch(theEntry.FileName, pattern))
continue;
nameList.Add(theEntry.FileName);
var memstream = new MemoryStream();
var size = 2048;
var data = new byte[size];
while (true)
{
size = zipStream.Read(data, 0, data.Length);
if (size > 0)
{
memstream.Write(data, 0, size);
memstream.Flush();
}
else break;
}
memstream.Seek(0, SeekOrigin.Begin);
streamList.Add(memstream);
}
zipStream.Close();
names = nameList.ToArray();
streams = streamList.ToArray();
return streams.Length;
}
///
/// Returns the first file found that matches the specified file extension.
///
/// Input zip file.
/// Extension to match.
/// The name of the file that matches the file extension.
/// The data of the file that matches the file extension.
///
public static bool ExtractByExtension(string file, string extension, out string name, out Stream stream)
{
var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
var zipStream = new ZipInputStream(fileStream);
var memstream = new MemoryStream();
ZipEntry theEntry;
while ((theEntry = zipStream.GetNextEntry()) != null)
{
if (Path.GetExtension(theEntry.FileName) != extension)
continue;
int size = 2048;
var data = new byte[size];
while (true)
{
size = zipStream.Read(data, 0, data.Length);
if (size > 0)
{
memstream.Write(data, 0, size);
memstream.Flush();
}
else break;
}
zipStream.Close();
memstream.Seek(0, SeekOrigin.Begin);
stream = memstream;
name = theEntry.FileName;
return true;
}
zipStream.Close();
memstream.Close();
stream = null;
name = null;
return false;
}
}
}