using System;
using System.Linq;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Core;
using System.Collections;
using System.Xml;
namespace Softex.Utilities
{
public class ZipFile
{
private string MySqlConnectionString;
private ZipOutputStream oZipFileStream;
public ZipFile(string mySqlConnectionString)
{
this.MySqlConnectionString = mySqlConnectionString;
}
#region " Method to create Zip file"
public void MakeZipFile(string zipConfigXml, ref MemoryStream zipStream)
{
if (zipConfigXml == null) throw new ArgumentNullException("zipConfigXml");
ZipOutputStream oZipStream = new ZipOutputStream(zipStream);
if (Functions.IsValidXmlData(zipConfigXml) == false)
{
zipStream = null;
}
else
{
XmlDocument zipXMLDoc = new XmlDocument();
zipXMLDoc.LoadXml(zipConfigXml);
XmlNodeList nodeListDownloadParts = zipXMLDoc.GetElementsByTagName("downloadParts");
foreach (XmlNode nodeDownloadParts in nodeListDownloadParts)
{
CreateDownloadPartsFileList(nodeDownloadParts, null, ref oZipStream);
}
}
oZipStream.Finish();
}
public void MakeZipFile(string zipConfigXml, string zipFilePath)
{
if (zipConfigXml == null) throw new ArgumentNullException("zipConfigXml");
try
{
if (Functions.IsValidXmlData(zipConfigXml))
{
if (!Directory.Exists(Path.GetDirectoryName(zipFilePath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(zipFilePath));
}
FileStream fsOutZipFile = File.Create(zipFilePath);
oZipFileStream = new ZipOutputStream(fsOutZipFile);
XmlDocument zipXMLDoc = new XmlDocument();
zipXMLDoc.LoadXml(zipConfigXml);
XmlNodeList nodeListDownloadParts = zipXMLDoc.GetElementsByTagName("downloadParts");
foreach (XmlNode nodeDownloadParts in nodeListDownloadParts)
{
AddFilesToZip(nodeDownloadParts, null);
}
oZipFileStream.IsStreamOwner = true;
oZipFileStream.Close();
}
}
catch (Exception ex)
{
throw (ex);
}
finally
{
}
}
public void CreateDownloadPartsFileList(XmlNode nodeDownloadParts, ArrayList aryElementPath, ref ZipOutputStream oZipStream)
{
XmlNode xnodWorking;
ArrayList aryWorkingElementPath = new ArrayList();
bool isSkipTheChildNode = false;
if (aryElementPath != null)
{
foreach (string paths in aryElementPath)
{
aryWorkingElementPath.Add(paths);
}
}
if (nodeDownloadParts.NodeType == XmlNodeType.Element)
{
XmlNamedNodeMap xmlAttributes = nodeDownloadParts.Attributes;
string fileRelativePath, fileAbsolutePath = null, zipFilePath;
switch (nodeDownloadParts.Name)
{
case "folder":
if (xmlAttributes != null)
{
if (xmlAttributes.GetNamedItem("isData") != null)
{
switch (xmlAttributes.GetNamedItem("isData").Value)
{
case "0":
aryWorkingElementPath.Add(xmlAttributes.GetNamedItem("id").Value);
break;
case "1":
string folderPath = xmlAttributes.GetNamedItem("path").Value;
if (folderPath != null)
{
if (xmlAttributes.GetNamedItem("isCopyRootFolder") != null)
{
if (xmlAttributes.GetNamedItem("isCopyRootFolder").Value == "1")
{
aryWorkingElementPath.Add(Path.GetDirectoryName(folderPath));
}
}
ArrayList fileList = GenerateFileList(folderPath);
int trimLength = folderPath.Length;
fileRelativePath = string.Join("\\",
(string[])
aryWorkingElementPath.ToArray(typeof(string)));
foreach (string fileFullPath in fileList)
{
fileAbsolutePath = fileFullPath;
zipFilePath = fileRelativePath + fileFullPath.Remove(0, trimLength);
WriteToZip(fileAbsolutePath, zipFilePath, ref oZipStream);
}
}
break;
default:
break;
}
}
}
break;
case "file":
if (xmlAttributes != null) fileAbsolutePath = xmlAttributes.GetNamedItem("path").Value;
fileRelativePath = string.Join("\\", (string[])aryWorkingElementPath.ToArray(typeof(string)));
zipFilePath = fileRelativePath + "\\" + Path.GetFileName(fileAbsolutePath);
if (nodeDownloadParts.HasChildNodes)
{
isSkipTheChildNode = true;
MemoryStream zipMemStream = new MemoryStream();
StreamWriter sw = new StreamWriter(zipMemStream) { AutoFlush = true };
long count = 0;
XmlNodeList fileEditLineNodeList = nodeDownloadParts.SelectNodes("editLine");
using (StreamReader r = new StreamReader(fileAbsolutePath))
{
string line;
while ((line = r.ReadLine()) != null)
{
if (fileEditLineNodeList != null)
{
foreach (XmlNode fileEditLineNode in fileEditLineNodeList)
{
if (fileEditLineNode.Attributes != null)
{
if (line.Contains(fileEditLineNode.Attributes.GetNamedItem("from").Value))
{
line =
line.Replace(
fileEditLineNode.Attributes.GetNamedItem("from").Value,
fileEditLineNode.Attributes.GetNamedItem("to").Value);
break;
}
}
}
}
count++;
sw.WriteLine(line);
}
}
XmlNodeList fileAddLineNodeList = nodeDownloadParts.SelectNodes("addLine");
if (fileAddLineNodeList != null)
{
foreach (XmlNode fileAddLineNode in fileAddLineNodeList)
{
string newLine = null;
if (fileAddLineNode.Attributes != null)
{
switch (fileAddLineNode.Attributes.GetNamedItem("type").Value)
{
case "lstFile":
newLine = ("Reg" + count + fileAddLineNode.InnerText);
break;
default:
newLine = fileAddLineNode.InnerText;
break;
}
}
sw.WriteLine(newLine);
count++;
}
}
byte[] fileBuffer = zipMemStream.ToArray();
sw.Close();
WriteToZip(fileBuffer, zipFilePath, ref oZipStream);
}
else
{
WriteToZip(fileAbsolutePath, zipFilePath, ref oZipStream);
}
break;
case "filefromdatabase":
if (xmlAttributes != null)
{
string tableName = xmlAttributes.GetNamedItem("tablename").Value;
string fileName = string.Empty;
int fileId = Convert.ToInt32(xmlAttributes.GetNamedItem("fileid").Value);
byte[] fileBuffer = Common.Functions.GetFileFromDatabase(this.MySqlConnectionString, tableName, fileId, ref fileName);
fileRelativePath = string.Join("\\", (string[])aryWorkingElementPath.ToArray(typeof(string)));
zipFilePath = fileRelativePath + "\\" + fileName;
WriteToZip(fileBuffer, zipFilePath, ref oZipStream);
}
break;
default:
break;
}
}
if ((nodeDownloadParts.HasChildNodes) && !isSkipTheChildNode)
{
xnodWorking = nodeDownloadParts.FirstChild;
while (xnodWorking != null)
{
CreateDownloadPartsFileList(xnodWorking, aryWorkingElementPath, ref oZipStream);
xnodWorking = xnodWorking.NextSibling;
}
}
}
public void AddFilesToZip(XmlNode nodeDownloadParts, ArrayList aryElementPath)
{
XmlNode xnodWorking;
ArrayList aryWorkingElementPath = new ArrayList();
bool isSkipTheChildNode = false;
if (aryElementPath != null)
{
foreach (string paths in aryElementPath)
{
aryWorkingElementPath.Add(paths);
}
}
if (nodeDownloadParts.NodeType == XmlNodeType.Element)
{
XmlNamedNodeMap xmlAttributes = nodeDownloadParts.Attributes;
string fileRelativePath, fileAbsolutePath = null, zipFilePath;
switch (nodeDownloadParts.Name)
{
case "folder":
if (xmlAttributes != null)
{
if (xmlAttributes.GetNamedItem("isData") != null)
{
switch (xmlAttributes.GetNamedItem("isData").Value)
{
case "0":
aryWorkingElementPath.Add(xmlAttributes.GetNamedItem("id").Value);
break;
case "1":
string folderPath = xmlAttributes.GetNamedItem("path").Value;
if (folderPath != null)
{
if (xmlAttributes.GetNamedItem("isCopyRootFolder") != null)
{
if (xmlAttributes.GetNamedItem("isCopyRootFolder").Value == "1")
{
aryWorkingElementPath.Add(Path.GetDirectoryName(folderPath));
}
}
ArrayList fileList = GenerateFileList(folderPath);
int trimLength = folderPath.Length;
fileRelativePath = string.Join("\\",
(string[])
aryWorkingElementPath.ToArray(typeof(string)));
foreach (string fileFullPath in fileList)
{
fileAbsolutePath = fileFullPath;
zipFilePath = fileRelativePath + fileFullPath.Remove(0, trimLength);
WriteToZip(fileAbsolutePath, zipFilePath);
}
}
break;
default:
break;
}
}
}
break;
case "file":
if (xmlAttributes != null) fileAbsolutePath = xmlAttributes.GetNamedItem("path").Value;
fileRelativePath = string.Join("\\", (string[])aryWorkingElementPath.ToArray(typeof(string)));
zipFilePath = fileRelativePath + "\\" + Path.GetFileName(fileAbsolutePath);
if (nodeDownloadParts.HasChildNodes)
{
isSkipTheChildNode = true;
MemoryStream zipMemStream = new MemoryStream();
StreamWriter sw = new StreamWriter(zipMemStream) { AutoFlush = true };
long count = 0;
XmlNodeList fileEditLineNodeList = nodeDownloadParts.SelectNodes("editLine");
using (StreamReader r = new StreamReader(fileAbsolutePath))
{
string line;
while ((line = r.ReadLine()) != null)
{
if (fileEditLineNodeList != null)
{
foreach (XmlNode fileEditLineNode in fileEditLineNodeList)
{
if (fileEditLineNode.Attributes != null)
{
if (line.Contains(fileEditLineNode.Attributes.GetNamedItem("from").Value))
{
line =
line.Replace(
fileEditLineNode.Attributes.GetNamedItem("from").Value,
fileEditLineNode.Attributes.GetNamedItem("to").Value);
break;
}
}
}
}
count++;
sw.WriteLine(line);
}
}
XmlNodeList fileAddLineNodeList = nodeDownloadParts.SelectNodes("addLine");
if (fileAddLineNodeList != null)
{
foreach (XmlNode fileAddLineNode in fileAddLineNodeList)
{
string newLine = null;
if (fileAddLineNode.Attributes != null)
{
switch (fileAddLineNode.Attributes.GetNamedItem("type").Value)
{
case "lstFile":
newLine = ("Reg" + count + fileAddLineNode.InnerText);
break;
default:
newLine = fileAddLineNode.InnerText;
break;
}
}
sw.WriteLine(newLine);
count++;
}
}
byte[] fileBuffer = zipMemStream.ToArray();
sw.Close();
WriteToZip(fileBuffer, zipFilePath);
}
else
{
WriteToZip(fileAbsolutePath, zipFilePath);
}
break;
case "filefromdatabase":
if (xmlAttributes != null)
{
string tableName = xmlAttributes.GetNamedItem("tablename").Value;
string fileName = string.Empty;
int fileId = Convert.ToInt32(xmlAttributes.GetNamedItem("fileid").Value);
byte[] fileBuffer = Common.Functions.GetFileFromDatabase(this.MySqlConnectionString, tableName, fileId, ref fileName);
fileRelativePath = string.Join("\\", (string[])aryWorkingElementPath.ToArray(typeof(string)));
zipFilePath = fileRelativePath + "\\" + fileName;
WriteToZip(fileBuffer, zipFilePath);
}
break;
default:
break;
}
}
if ((nodeDownloadParts.HasChildNodes) && !isSkipTheChildNode)
{
xnodWorking = nodeDownloadParts.FirstChild;
while (xnodWorking != null)
{
AddFilesToZip(xnodWorking, aryWorkingElementPath);
xnodWorking = xnodWorking.NextSibling;
}
}
}
public void WriteToZip(string filePath, string fileRelativePath, ref ZipOutputStream oZipStream)
{
FileStream oStream;
byte[] oBuffer = new byte[1024 * 1024];
ZipEntry oZipEntry = new ZipEntry(fileRelativePath);
oZipStream.SetLevel(9);
oZipStream.PutNextEntry(oZipEntry);
if (!filePath.EndsWith(@"/"))
{
oStream = File.OpenRead(filePath);
ICSharpCode.SharpZipLib.Core.StreamUtils.Copy(oStream, oZipStream, oBuffer);
}
}
public void WriteToZip(string filePath, string fileRelativePath)
{
ZipEntry oZipEntry = new ZipEntry(fileRelativePath);
oZipFileStream.SetLevel(9);
oZipFileStream.PutNextEntry(oZipEntry);
if (!filePath.EndsWith(@"/"))
{
byte[] buffer = new byte[4096];
using (FileStream oStream = File.OpenRead(filePath))
{
StreamUtils.Copy(oStream, oZipFileStream, buffer);
}
oZipFileStream.CloseEntry();
}
}
public void WriteToZip(byte[] fileBuffer, string fileRelativePath, ref ZipOutputStream oZipStream)
{
ZipEntry oZipEntry = new ZipEntry(fileRelativePath);
oZipStream.SetLevel(9);
oZipStream.PutNextEntry(oZipEntry);
oZipStream.Write(fileBuffer, 0, fileBuffer.Length);
}
public void WriteToZip(byte[] fileBuffer, string fileRelativePath)
{
ZipEntry oZipEntry = new ZipEntry(fileRelativePath);
oZipFileStream.SetLevel(9);
oZipFileStream.PutNextEntry(oZipEntry);
oZipFileStream.Write(fileBuffer, 0, fileBuffer.Length);
oZipFileStream.CloseEntry();
}
public ArrayList GenerateFileList(string Dir)
{
ArrayList fils = new ArrayList();
bool Empty = true;
if (Directory.Exists(Dir))
{
foreach (string file in Directory.GetFiles(Dir))
{
fils.Add(file);
Empty = false;
}
if (Empty)
{
if (Directory.GetDirectories(Dir).Length == 0)
{
fils.Add(Dir + @"/");
}
}
foreach (string dirs in Directory.GetDirectories(Dir))
{
foreach (object obj in GenerateFileList(dirs))
{
fils.Add(obj);
}
}
}
return fils;
}
#endregion
}
}
Its a utility which compress the file . I want to use this utility . For that i have to create a XML file from it and then use it when download occurs . Will anyone suggest me what is the XML file output through this code.
[edit]Code block added - so at least we can collapse it... - OriginalGriff[/edit]