Feb
8
Written by:
Oleg Zhukov
2/8/2010 10:10 PM
The other day I needed to unzip a bunch of zip-archives, modify the unzipped content and zip them back again. Since I'm lazy (as every developer should be) I decided to write a tool that would do all the work for me.
After googling around I found several ways of handling zip archives. First of them I tried was the System.IO.Packaging namespace classes available in .NET 3.0. But disappointment came soon when it simply failed to unzip my archives probably due to some zip format incompatibility.
Well-known #ziplib turned out to be under GPL, and I also heard it's less usable than its main competitor DotNetZip.
With DotNetZip i did my job in several minutes:
private void ProcessMMCSECOfile(string mmcsecoFileName)
{
using (ZipFile mmcseco = ZipFile.Read(mmcsecoFileName))
{
mmcseco.CompressionLevel = CompressionLevel.None;
string newCodeModelContents = ChangeEncoding(
GetContents(mmcseco, "codemodel.xml"));
string newDiagramsContents = ChangeEncoding(
GetContents(mmcseco, "diagrams.xml"));
mmcseco.RemoveEntry("diagrams.xml");
mmcseco.RemoveEntry("codemodel.xml");
mmcseco.AddEntry("diagrams.xml", newDiagramsContents);
mmcseco.AddEntry("codemodel.xml", newCodeModelContents);
mmcseco.Save();
}
processedFilesCount++;
}
string GetContents(ZipFile zipFile, string archivedFileName)
{
using (Stream st = zipFile[archivedFileName].OpenReader())
{
using (StreamReader reader = new StreamReader(st,
Encoding.GetEncoding(correctEncodingTxtBox.Text)))
{
return reader.ReadToEnd();
}
}
}
Tags: