Hi pujan45,
Using the below article i have created an example.
XML
<?xml version="1.0" encoding="utf-8" ?>
<Customers>
<Customer>
<Id>1</Id>
<Name>John Hammond</Name>
<Country>United States</Country>
</Customer>
<Customer>
<Id>2</Id>
<Name>Mudassar Khan</Name>
<Country>India</Country>
</Customer>
<Customer>
<Id>3</Id>
<Name>Suzanne Mathews</Name>
<Country>France</Country>
</Customer>
<Customer>
<Id>4</Id>
<Name>Robert Schidner</Name>
<Country>Russia</Country>
</Customer>
</Customers>
Namespaces
using System.Data;
using System.IO;
using Ionic.Zip;
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Download()
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("~/Customers.xml"));
using (ZipFile zip = new ZipFile())
{
zip.AlternateEncodingUsage = ZipOption.AsNecessary;
zip.AddDirectoryByName("Files");
if (!Directory.Exists(Server.MapPath("~/Files")))
{
Directory.CreateDirectory(Server.MapPath("~/Files"));
}
for (int i = 0; i < ds.Tables["Customer"].Rows.Count; i++)
{
string fileName = string.Format("{0}\\Customer_{1}.xml", Server.MapPath("~/Files"), i + 1);
DataTable dt = ds.Tables["Customer"].Clone();
DataRow dr = ds.Tables["Customer"].Rows[i];
dt.ImportRow(dr);
DataSet dsSplit = new DataSet("Customers");
dsSplit.Tables.Add(dt);
dsSplit.WriteXml(fileName);
zip.AddFile(fileName, "Files");
}
byte[] bytes = null;
string zipName = String.Format("Zip_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));
using (MemoryStream memoryStream = new MemoryStream())
{
zip.Save(memoryStream);
bytes = memoryStream.ToArray();
memoryStream.Close();
}
Directory.Delete(Server.MapPath("~/Files"), true);
return File(bytes, "application/zip", zipName);
}
}
}
View
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@using (Html.BeginForm("Download", "Home", FormMethod.Post))
{
<input type="submit" value="Download" />
}
</body>
</html>
Screenshots
View Page
Downloaded Zip File