converting files in to single zip using C#
I am using C# code for aspx pages. I need to convert multiple files in to single zip file that can be readable by windows default zip software.
Can any one have an idea about开发者_如何转开发 it...?
See this tutorial: Creating Zip archives in .NET (without an external library like SharpZipLib)
ZipFile zip= new ZipFile("MyNewZip.zip");
zip.AddDirectory("My Pictures", true); // AddDirectory recurses subdirectories
zip.Save();
Or you can use SharpZipLib.
Use an free library like
http://www.sharpdevelop.net/OpenSource/SharpZipLib/
DotNetZip is a good open source one, without any licensing issue.
//use this library SharpZipLib.
using this you can send multiple file for zipping which user selected and can save it to the physical path you specify either on client.
public string zipfile(string[] files)
{
string[] filenames = new string[files.Length];
for (int i = 0; i < files.Length; i++)
filenames[i] = HttpContext.Current.Request.PhysicalApplicationPath + files[i].Remove(0, 10// set it according to your filename).ToString();
else
for (int i = 0; i < files.Length; i++)
filenames[i] = HttpContext.Current.Request.PhysicalApplicationPath + files[i].Replace(HttpContext.Current.Request.UrlReferrer.ToString(), "");
string DirectoryName = filenames[0].Remove(filenames[0].LastIndexOf('/'));
DirectoryName = DirectoryName.Substring(DirectoryName.LastIndexOf('/') + 1).Replace("\\", "");
try
{
string newFile = HttpContext.Current.Request.PhysicalApplicationPath + "the physical path where you want to save it" + DirectoryName + ".zip";
if (File.Exists(newFile))
File.Delete(newFile);
using (ZipFile zip = new ZipFile())
{
foreach (string file in filenames)
{
string newfileName = file.Replace("\\'", "'");
zip.CompressionLevel = 0;
zip.AddFile(newfileName, "");
}
zip.Save(newFile);
}
}
精彩评论