asp.net SharpZipLib的壓縮與解壓?jiǎn)栴}
具體壓縮、解壓代碼實(shí)現(xiàn)參照網(wǎng)絡(luò)上的代碼,貼出概要代碼:
/// <summary>
/// 壓縮文件
/// </summary>
/// <param name="sourceFilePath">源文件路徑</param>
/// <param name="destinationPath">壓縮文件后的保存路徑</param>
/// <returns>壓縮是否成功</returns>
public bool Compress(string sourceFilePath, string destinationPath)
{
try
{
string[] filenames = Directory.GetFiles(sourceFilePath);
using (ZipOutputStream zs = new ZipOutputStream(File.Create(destinationPath)))
{
zs.SetLevel(9);
byte[] buffer = new byte[4096];
foreach (string file in filenames)
{
ZipEntry entry = new ZipEntry(Path.GetFileName(file));
entry.DateTime = DateTime.Now;
zs.PutNextEntry(entry);
using (FileStream fs = File.OpenRead(file))
{
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, 0, buffer.Length);
zs.Write(buffer, 0, sourceBytes);
}
while (sourceBytes > 0);
}
}
zs.Finish();
zs.Flush();
zs.Close();
}
}
catch (Exception)
{
return false;
}
return true;
} public bool DeCompress(string sourceFilePath, string destinationPath)
{
try
{
using (ZipInputStream zs = new ZipInputStream(File.OpenRead(sourceFilePath)))
{
ZipEntry entry = null;
//解壓縮*.rar文件運(yùn)行至此處出錯(cuò):Wrong Local header signature: 0x21726152,解壓*.zip文件不出錯(cuò)
while ((entry = zs.GetNextEntry()) != null)
{
string directoryName = Path.GetDirectoryName(entry.Name);
string fileName = Path.GetFileName(entry.Name);
if (!string.IsNullOrEmpty(fileName))
{
using (FileStream streamWriter = File.Create(destinationPath + entry.Name))
{
int size = 2048;
byte[] data = new byte[size];
while (true)
{
size = zs.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
}
}
}
}
}
catch (System.Exception)
{
return false;
}
return true;
}
如果需解壓*.rar的壓縮文件在網(wǎng)絡(luò)也可以找到相關(guān)的實(shí)現(xiàn)代碼,概要代碼:
public bool DeCompressRAR(string sourceFilePath, string destinationPath)
{
try
{
string SeverDir = @"D:\Program Files\WinRAR";//rar.exe的要目錄
Process ProcessDecompression = new Process();
ProcessDecompression.StartInfo.FileName = SeverDir + "\\rar.exe";
Directory.CreateDirectory(sourceFilePath);
ProcessDecompression.StartInfo.Arguments = " X " + sourceFilePath + " " + destinationPath;
ProcessDecompression.Start();
while (!ProcessDecompression.HasExited)
{
//nothing to do here.
}
return true;
}
catch (System.Exception)
{
return false;
}
}
我本想利用FileUpload控件將上傳的壓縮文件解壓后保存至相對(duì)應(yīng)的目錄并更新數(shù)據(jù)庫(kù)文件目錄,后發(fā)現(xiàn)一些較好的用于上傳的開(kāi)源軟件:如NeatUpload,SWFUpload可以較方便的實(shí)現(xiàn)我的需求,遂沒(méi)有過(guò)多糾纏于SharpZipLib,可能關(guān)于SharpZipLib的壓縮與解壓有其它用法,不能被我誤導(dǎo),以上代碼是從網(wǎng)絡(luò)上整合出來(lái)的,因?yàn)樗^(guò)于重復(fù)和散亂。
相關(guān)文章
asp.net 關(guān)于==?:和if()else()條件判斷等效例子
關(guān)于==?:和if()else() 等效例子2010-03-03
在?.NET?中使用?FixedTimeEquals?應(yīng)對(duì)計(jì)時(shí)攻擊的例子
在計(jì)算機(jī)安全中,計(jì)時(shí)攻擊(Timing attack)是旁道攻擊 (Side-channel attack) 的一種,而旁道攻擊是根據(jù)計(jì)算機(jī)處理過(guò)程發(fā)出的信息進(jìn)行分析,這篇文章主要介紹了在?.NET?中使用?FixedTimeEquals?應(yīng)對(duì)計(jì)時(shí)攻擊,需要的朋友可以參考下2022-06-06
ASP.NET 防止按鈕多次提交核心實(shí)現(xiàn)代碼
防止按鈕多次提交通常都是在注冊(cè)表單中提示時(shí)的一個(gè)小功能,具體實(shí)現(xiàn)如下,由此需求的朋友可以參考下2013-08-08
asp.net實(shí)現(xiàn)將Excel中多個(gè)sheet數(shù)據(jù)導(dǎo)入到SQLSERVER中的方法
這篇文章主要介紹了asp.net實(shí)現(xiàn)將Excel中多個(gè)sheet數(shù)據(jù)導(dǎo)入到SQLSERVER中的方法,涉及asp.net針對(duì)Excel的讀取與數(shù)據(jù)庫(kù)操作相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-12-12
asp.net后臺(tái)如何輸出js腳本使用什么方法可以實(shí)現(xiàn)
asp.net后臺(tái)如何輸出js腳本,用page.ClientScript.RegisterStartupScript方式實(shí)現(xiàn),實(shí)現(xiàn)示例如下,感興趣的朋友不要錯(cuò)過(guò)2014-01-01
把ASP.NET MVC項(xiàng)目部署到本地IIS上的完整步驟
最近會(huì)經(jīng)常修改一些網(wǎng)站前端的內(nèi)容,為了方便跟UI和產(chǎn)品交流,需要將自己修改過(guò)的頁(yè)面及時(shí)發(fā)布到測(cè)試機(jī)或者是本地的IIS上。下面這篇文章主要給大家介紹了關(guān)于如何把ASP.NET MVC項(xiàng)目部署到本地IIS上的相關(guān)資料,需要的朋友可以參考下2018-06-06
asp.net下將Excel轉(zhuǎn)成XML檔的實(shí)現(xiàn)代碼
通過(guò)Asp.net(C#)應(yīng)用程序讀取本地上傳的Excle文件,存放到DataSet中,通過(guò)DataSet中的方法直接生成XML文件.2009-11-11
asp.net Oracle數(shù)據(jù)庫(kù)訪問(wèn)操作類(lèi)
asp.net Oracle數(shù)據(jù)庫(kù)訪問(wèn)操作類(lèi),需要的朋友可以參考一下2013-03-03
阿里云上從ASP.NET線程角度對(duì)“黑色30秒”問(wèn)題的全新分析
在這篇博文中,我們拋開(kāi)對(duì)阿里云的懷疑,完全從ASP.NET的角度進(jìn)行分析,看能不能找到針對(duì)問(wèn)題現(xiàn)象的更合理的解釋2015-09-09

