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)絡上整合出來的,因為它太過于重復和散亂。
相關文章
asp.net 關于==?:和if()else()條件判斷等效例子
關于==?:和if()else() 等效例子2010-03-03
在?.NET?中使用?FixedTimeEquals?應對計時攻擊的例子
在計算機安全中,計時攻擊(Timing attack)是旁道攻擊 (Side-channel attack) 的一種,而旁道攻擊是根據(jù)計算機處理過程發(fā)出的信息進行分析,這篇文章主要介紹了在?.NET?中使用?FixedTimeEquals?應對計時攻擊,需要的朋友可以參考下2022-06-06
asp.net實現(xiàn)將Excel中多個sheet數(shù)據(jù)導入到SQLSERVER中的方法
這篇文章主要介紹了asp.net實現(xiàn)將Excel中多個sheet數(shù)據(jù)導入到SQLSERVER中的方法,涉及asp.net針對Excel的讀取與數(shù)據(jù)庫操作相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-12-12
asp.net后臺如何輸出js腳本使用什么方法可以實現(xiàn)
asp.net后臺如何輸出js腳本,用page.ClientScript.RegisterStartupScript方式實現(xiàn),實現(xiàn)示例如下,感興趣的朋友不要錯過2014-01-01
asp.net下將Excel轉(zhuǎn)成XML檔的實現(xiàn)代碼
通過Asp.net(C#)應用程序讀取本地上傳的Excle文件,存放到DataSet中,通過DataSet中的方法直接生成XML文件.2009-11-11
asp.net Oracle數(shù)據(jù)庫訪問操作類
asp.net Oracle數(shù)據(jù)庫訪問操作類,需要的朋友可以參考一下2013-03-03
阿里云上從ASP.NET線程角度對“黑色30秒”問題的全新分析
在這篇博文中,我們拋開對阿里云的懷疑,完全從ASP.NET的角度進行分析,看能不能找到針對問題現(xiàn)象的更合理的解釋2015-09-09

