asp.net SharpZipLib的壓縮與解壓問題
更新時間:2009年11月11日 00:48:10 作者:
關于SharpZipLib的壓縮與解壓縮的實現(xiàn)代碼,網(wǎng)絡上有一堆,千遍一律,連注釋也一模一樣,一模一樣的文章拷來拷去??
我使用SharpZipLib.dll中遇到的問題是:利用SharpZipLib壓縮后生成的*.rar文件,利用其可以正常解壓,但如果使用文件右擊壓縮生成的*.RAR文件,在解壓過程中出錯,具體報錯信息:Wrong Local header signature: 0x21726152 ;但*.zip文件可正常解壓。
具體壓縮、解壓代碼實現(xiàn)參照網(wǎng)絡上的代碼,貼出概要代碼:
/// <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文件運行至此處出錯:Wrong Local header signature: 0x21726152,解壓*.zip文件不出錯
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)絡也可以找到相關的實現(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控件將上傳的壓縮文件解壓后保存至相對應的目錄并更新數(shù)據(jù)庫文件目錄,后發(fā)現(xiàn)一些較好的用于上傳的開源軟件:如NeatUpload,SWFUpload可以較方便的實現(xiàn)我的需求,遂沒有過多糾纏于SharpZipLib,可能關于SharpZipLib的壓縮與解壓有其它用法,不能被我誤導,以上代碼是從網(wǎng)絡上整合出來的,因為它太過于重復和散亂。
具體壓縮、解壓代碼實現(xiàn)參照網(wǎng)絡上的代碼,貼出概要代碼:
復制代碼 代碼如下:
/// <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文件運行至此處出錯:Wrong Local header signature: 0x21726152,解壓*.zip文件不出錯
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)絡也可以找到相關的實現(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控件將上傳的壓縮文件解壓后保存至相對應的目錄并更新數(shù)據(jù)庫文件目錄,后發(fā)現(xiàn)一些較好的用于上傳的開源軟件:如NeatUpload,SWFUpload可以較方便的實現(xiàn)我的需求,遂沒有過多糾纏于SharpZipLib,可能關于SharpZipLib的壓縮與解壓有其它用法,不能被我誤導,以上代碼是從網(wǎng)絡上整合出來的,因為它太過于重復和散亂。
相關文章
使用 Visual Studio 的“代碼度量值”來改進代碼質(zhì)量
代碼度量是一組軟件度量值,使開發(fā)人員可以更好地了解他們正在開發(fā)的代碼.這篇文章主要介紹了通過 Visual Studio 的“代碼度量值”來改進代碼質(zhì)量,需要的朋友可以參考下2017-11-11.net+FusionChart實現(xiàn)動態(tài)顯示的柱狀圖和餅狀圖
這篇文章介紹了.net+FusionChart實現(xiàn)動態(tài)顯示柱狀圖和餅狀圖的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07ASP.NET Core模仿中間件方式實現(xiàn)列表過濾功能
這篇文章介紹了ASP.NET Core模仿中間件方式實現(xiàn)列表過濾功能的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07在Global.asax文件里實現(xiàn)通用防SQL注入漏洞程序(適應于post/get請求)
可使用Global.asax中的Application_BeginRequest(object sender, EventArgs e)事件來實現(xiàn)表單或者URL提交數(shù)據(jù)的獲取,獲取后傳給SQLInjectionHelper類ValidUrlData方法來完成檢查2013-01-01.net開發(fā)中幾個重要的認識誤區(qū)小結(jié)
.net如今已經(jīng)很流行,成為趕時髦的程序員的首選。但是,大量剛剛接觸.net的程序員的確存在一定的認識誤區(qū),這里先介紹一部分。2010-04-04asp.net 從POST的數(shù)據(jù)流中提取參數(shù)和文件
按理,F(xiàn)orm提交的數(shù)據(jù),無論是application/x-www-form-urlencoded還是multipart/form-data(有附件時),都可在服務端通過Request.Form["name"]和Request.Files["name"]獲取到參數(shù)和上傳的文件。2010-02-02關于前臺調(diào)用后臺事件__doPostBack函數(shù)
關于前臺調(diào)用后臺事件__doPostBack函數(shù)...2007-04-04