話不多說,直接上代碼,最後有重要說明!!!哦對了,用的是ICSharpCode.SharpZipLib.Zip; 這個東西
壓縮:
先聲明個全局變量吧:
//附件打包的變量
ZipOutputStream zos = null;
下面就是壓縮的代碼了:
#region 壓縮附件的相關
protected void btnFile_Click(object sender, EventArgs e)
{
dlZipDir();
dla.Style.Remove("display");
}
protected void dlZipDir()
{
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);
//加密
zos.Password = encrypt("1qaz@WSX");
addZipEntry();
zos.Finish();
zos.Close();
Response.Clear();
Response.BinaryWrite(ms.ToArray());
Response.End();
}
protected void addZipEntry()
{
DataTable dt = new DataTable();
dt = getAccessory();
string folder = Server.MapPath("Data\\Temp");
//FileInfo[] files = new FileInfo[dt.Rows.Count];
List<FileInfo> list = new List<FileInfo>();
GZipResult r = new GZipResult();
int num = 0;
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
string path = dt.Rows[i]["Fjlj"].ToString();
//string xh = dt.Rows[i]["Xh"].ToString();
string servpath = ConfigurationManager.AppSettings["Fjlj"] + path;
if (File.Exists(servpath))
{
FileInfo file = new FileInfo(servpath);
FileStream fs = File.OpenRead(file.FullName);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
string _strFjlj = dt.Rows[i]["Fjlj"].ToString();
string strEntryName = _strFjlj.Substring(_strFjlj.Length - 40);
ZipEntry entry = new ZipEntry(strEntryName);
zos.PutNextEntry(entry);
zos.Write(buffer, 0, buffer.Length);
fs.Close();
}
}
}
}
/// <summary>
/// 獲取所有附件的DataTable1
/// </summary>
/// <returns></returns>
private DataTable getAccessory()
{
DataTable dt = new DataTable();
BLL.Sys_Xxfj xxfjBll = new BLL.Sys_Xxfj();
dt = xxfjBll.getAccessory();
return dt;
}
還有MD5加密的方法
#region "MD5加密"
/// <summary>
///32位 MD5加密
/// </summary>
/// <param name="str">加密字符</param>
/// <returns></returns>
public static string encrypt(string str)
{
string cl = str;
string pwd = "";
MD5 md5 = MD5.Create();
byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
for (int i = 0; i < s.Length; i++)
{
pwd = pwd + s[i].ToString("X");
}
return pwd;
}
#endregion
下面是解壓縮的“
/// <summary>
/// 解壓縮文件
/// </summary>
/// <param name="GzipFile">壓縮包文件名</param>
/// <param name="targetPath">解壓縮目標路徑</param>
public static void Decompress(string GzipFile, string targetPath)
{
//string directoryName = Path.GetDirectoryName(targetPath + "//") + "//";
string directoryName = targetPath;
if (!Directory.Exists(directoryName))
{
Directory.CreateDirectory(directoryName);//生成解壓目錄
}
string CurrentDirectory = directoryName;
byte[] data = new byte[2048];
int size = 2048;
ZipEntry theEntry = null;
using (ZipInputStream s = new ZipInputStream(File.OpenRead(GzipFile)))
{
//解密
s.Password = encrypt("1qaz@WSX");
while ((theEntry = s.GetNextEntry()) != null)
{
if (theEntry.IsDirectory)
{// 該結點是目錄
if (!Directory.Exists(CurrentDirectory + theEntry.Name)) Directory.CreateDirectory(CurrentDirectory + theEntry.Name);
}
else
{
if (theEntry.Name != String.Empty)
{
// 檢查多級目錄是否存在
if (theEntry.Name.Contains("//"))
{
string parentDirPath = theEntry.Name.Remove(theEntry.Name.LastIndexOf("//") + 1);
if (!Directory.Exists(parentDirPath))
{
Directory.CreateDirectory(CurrentDirectory + parentDirPath);
}
}
//解壓文件到指定的目錄
using (FileStream streamWriter = File.Create(CurrentDirectory + "\\" + theEntry.Name))
{
while (true)
{
size = s.Read(data, 0, data.Length);
if (size <= 0) break;
streamWriter.Write(data, 0, size);
}
streamWriter.Close();
}
}
}
}
s.Close();
}
}
最後的說明就是:這個壓縮方法是將文件流到內存中,如果文件過大 有可能造成程序崩掉,反正我是崩掉了。。。