引用:
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;
cs:
ZipOutputStream zos = null;
String strBaseDir = "";
protected void Button1_Click(object sender, EventArgs e)
{
//SELECT FITEMID,FCUSTOM,FFIXID,FMACHINENAME,FDATEID FROM tblfileupdown WHERE FITEMID = '003' GROUP BY FITEMID,FCUSTOM,FFIXID,FMACHINENAME,FDATEID
if (txtSearch.Text.Trim() != "")
{
com = con.CreateCommand();
string strSql = "SELECT FCUSTOM,FDATEID,FMACHINENAME,FFIXID,FITEMID FROM TBLFILEUPDOWN ";
string strLimt = "";
if (txtSearch.Text != "")
{
if (strLimt == "")
{
strLimt = " WHERE FITEMID =:item";
}
}
strSql = strSql + strLimt + " GROUP BY FCUSTOM,FDATEID,FMACHINENAME,FFIXID,FITEMID ";
con.Open();
OracleDataAdapter da = new OracleDataAdapter(strSql, con);
da.SelectCommand.Parameters.AddWithValue("item", txtSearch.Text.Trim());
DataSet ds = new DataSet();
da.Fill(ds);
//方法一(但放在服務器不行)
//WebClient wc = new WebClient();
//string strFlage = "0";
//for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
//{
// //string uriString = this.Context.Server.MapPath("mafile").ToString() + "//" + ds.Tables[0].Rows[i][0].ToString() + " ICT PROGRAM//" + ds.Tables[0].Rows[i][1].ToString() + "//" + ds.Tables[0].Rows[i][2].ToString() + "//" + ds.Tables[0].Rows[i][3].ToString() + "//" + ds.Tables[0].Rows[i][4].ToString() + "//" + ds.Tables[0].Rows[i][5].ToString();
// string uriString = "http://172.24.84.10/down/mafile" + "/" + ds.Tables[0].Rows[i][0].ToString() + " ICT PROGRAM/" + ds.Tables[0].Rows[i][1].ToString() + "/" + ds.Tables[0].Rows[i][2].ToString() + "/" + ds.Tables[0].Rows[i][3].ToString() + "/" + ds.Tables[0].Rows[i][4].ToString() + "/" + ds.Tables[0].Rows[i][5].ToString();
// if (File.Exists(uriString))
// {
// Uri address = new Uri(uriString);
// wc.DownloadFile(uriString, TextBox1.Text + ds.Tables[0].Rows[i][5].ToString());
// //wc.DownloadFileAsync(uriString, TextBox1.Text + ds.Tables[0].Rows[i][5].ToString());
// if (wc != null)
// wc.Dispose();
// }
// else
// {
// strFlage = "1";
// break;
// }
//}
//TextBox1.Text = "";
//if (strFlage == "1")
// Response.Write("<script>alert('文件不存在!');</script>");
//else
// Response.Write("<script>alert('下載成功!');</script>");
//方法二
if(ds.Tables[0].Rows.Count >0)
{
string strPath = this.Context.Server.MapPath("mafile").ToString() + "//" + ds.Tables[0].Rows[0][0].ToString() + " ICT PROGRAM//" + ds.Tables[0].Rows[0][1].ToString() + "//" + ds.Tables[0].Rows[0][2].ToString() + "//" + ds.Tables[0].Rows[0][3].ToString() + "//" + ds.Tables[0].Rows[0][4].ToString();
DirectoryInfo dir = new DirectoryInfo(strPath);
if (dir.Exists)
{
dlZipDir(strPath, ds.Tables[0].Rows[0][4].ToString().Trim());
}
else
{
Response.Write("<script>alert('文件不存在!');</script>");
}
}
else
{
Response.Write("<script>alert('無相關文件!');</script>");
}
//END
con.Close();
}
else
{
Response.Write("<script>alert('請輸入條件下載!');</script>");
}
}
public void dlZipDir(string strPath, string strFileName)
{
MemoryStream ms = null;
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);
strBaseDir = strPath + "//";
addZipEntry(strBaseDir);
zos.Finish();
zos.Close();
Response.Clear();
Response.BinaryWrite(ms.ToArray());
Response.End();
}
public void addZipEntry(string PathStr)
{
DirectoryInfo di = new DirectoryInfo(PathStr);
foreach (DirectoryInfo item in di.GetDirectories())
{
addZipEntry(item.FullName);
}
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.Replace(strBaseDir, "");
ZipEntry entry = new ZipEntry(strEntryName);
zos.PutNextEntry(entry);
zos.Write(buffer, 0, buffer.Length);
fs.Close();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
System.Windows.Forms.FolderBrowserDialog oFBD = new System.Windows.Forms.FolderBrowserDialog();
if (oFBD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
txtSearch.Text = oFBD.SelectedPath;
}
}