PDA

View Full Version : ی کد با DLL برای Zip



mjsoft
چهارشنبه 11 اسفند 1389, 11:25 صبح
ی کد با DLL برای Zip کردن براتون میذارم
ZipOutputStream zos = null;
protected void Button1_Click(object sender, EventArgs e)
{
string[] pathCollection = new string[2];
PathCellction[0] = "c:\\folder1";
PathCellction[1] = "c:\\folder2";
StartZip(pathCollection, "filename");
}

protected void StartZip(string[] pathCollection, string strFileName)
{
MemoryStream ms;
Response.ContentType = "application/octet-stream";
strFileName = HttpUtility.UrlEncode(strFileName).Replace('+', ' ');
Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName + ".zip");
ms = new MemoryStream();
zos = new ZipOutputStream(ms);
addZipEntry(pathCollection);
ms.Close();
zos.Finish();
zos.Close();
Response.Clear();
Response.BinaryWrite(ms.ToArray());
Response.End();
}

protected void addZipEntry(string[] pathCollection)
{
for (int i = 0; i < pathCollection.Length; i++)
{
string strPath = pathCollection[i];
addZipEntry(strPath, strPath.LastIndexOf("\\") + 1);
}
}

protected void addZipEntry(string strPath, int baseIndex)
{
DirectoryInfo di = new DirectoryInfo(strPath);
foreach (DirectoryInfo item in di.GetDirectories())
{
addZipEntry(item.FullName, baseIndex);
}
foreach (FileInfo item in di.GetFiles())
{
FileStream fs = File.OpenRead(item.FullName);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
string strEntryName = item.FullName.Remove(0, baseIndex);
ZipEntry entry = new ZipEntry(strEntryName);
zos.PutNextEntry(entry);
zos.Write(buffer, 0, buffer.Length);
fs.Close();
}
}