Hi landomarossi,
You are initializing the ZipFile class inside the ForEach loop. So the last subdirectory files are created as zip file.
You need to initialize the ZipFile class instance outside the loop.
Then inside the loop you need only to add the files using the AddFile method.
Then call the Save method after all the files added to the zip.
Refer the modified code.
string monthName = DateTime.Now.AddMonths(-1).ToString("MMM");
string directoryName = DateTime.Now.AddMonths(-1).ToString("MMM_yyyy");
string[] states = { "ALABAMA", "FLORIDA", "CALIFORNIA" };
using (ZipFile zip = new ZipFile())
{
zip.AlternateEncodingUsage = ZipOption.AsNecessary;
zip.AddDirectoryByName(directoryName);
foreach (string state in states)
{
string filename = @"C:\inetpub\wwwroot\public\" +
state.ToString() + "\\" +
monthName.ToString() + "_" + DateTime.Now.Year;
if (Directory.Exists(filename))
{
string files = Directory.GetFiles(filename, "*.pdf", SearchOption.AllDirectories);
foreach (string filePath in files)
{
zip.AddFile(filePath, directoryName);
}
}
}
zip.Save(Server.MapPath("~/public/files/" + directoryName + ".zip"));
}