asp.net C#實(shí)現(xiàn)解壓縮文件的方法
來(lái)源:易賢網(wǎng) 閱讀:855 次 日期:2014-11-02 10:07:23
溫馨提示:易賢網(wǎng)小編為您整理了“asp.net C#實(shí)現(xiàn)解壓縮文件的方法”,方便廣大網(wǎng)友查閱!

易賢網(wǎng)網(wǎng)校上線(xiàn)了!

>>>點(diǎn)擊進(jìn)入<<<

網(wǎng)校開(kāi)發(fā)及擁有的課件范圍涉及公務(wù)員、財(cái)會(huì)類(lèi)、外語(yǔ)類(lèi)、外貿(mào)類(lèi)、學(xué)歷類(lèi)、

職業(yè)資格類(lèi)、計(jì)算機(jī)類(lèi)、建筑工程類(lèi)、等9大類(lèi)考試的在線(xiàn)網(wǎng)絡(luò)培訓(xùn)輔導(dǎo)。

本文實(shí)例講述了asp.net C#實(shí)現(xiàn)解壓縮文件的方法。一共給大家介紹了三段代碼,一個(gè)是簡(jiǎn)單的解壓縮單個(gè)zip文件,后一個(gè)可以解壓批量的大量的但需要調(diào)用ICSharpCode.SharpZipLib.dll類(lèi)了,最后一個(gè)比較實(shí)例可壓縮也可以解壓縮了分享給大家供大家參考。具體如下:

解壓縮單個(gè)文件:

代碼如下:

using System.IO;

using System.IO.Compression;

string sourceFile=@"D:2.zip";

string destinationFile=@"D:1.txt";

private const long BUFFER_SIZE = 20480;

// make sure the source file is there

if (File.Exists ( sourceFile ))

{

FileStream sourceStream = null;

FileStream destinationStream = null;

GZipStream decompressedStream = null;

byte[] quartetBuffer = null;

try

{

// Read in the compressed source stream

sourceStream = new FileStream ( sourceFile, FileMode.Open );

// Create a compression stream pointing to the destiantion stream

decompressedStream = new DeflateStream ( sourceStream, CompressionMode.Decompress, true );

// Read the footer to determine the length of the destiantion file

quartetBuffer = new byte[4];

int position = (int)sourceStream.Length - 4;

sourceStream.Position = position;

sourceStream.Read ( quartetBuffer, 0, 4 );

sourceStream.Position = 0;

int checkLength = BitConverter.ToInt32 ( quartetBuffer, 0 );

byte[] buffer = new byte[checkLength + 100];

int offset = 0;

int total = 0;

// Read the compressed data into the buffer

while ( true )

{

int bytesRead = decompressedStream.Read ( buffer, offset, 100 );

if ( bytesRead == 0 )

break;

offset += bytesRead;

total += bytesRead;

}

// Now write everything to the destination file

destinationStream = new FileStream ( destinationFile, FileMode.Create );

destinationStream.Write ( buffer, 0, total );

// and flush everyhting to clean out the buffer

destinationStream.Flush ( );

}

catch ( ApplicationException ex )

{

Console.WriteLine(ex.Message, "解壓文件時(shí)發(fā)生錯(cuò)誤:");

}

finally

{

// Make sure we allways close all streams

if ( sourceStream != null )

sourceStream.Close ( );

if ( decompressedStream != null )

decompressedStream.Close ( );

if ( destinationStream != null )

destinationStream.Close ( );

}

}

批量解壓縮(這需要調(diào)用一個(gè)解壓縮類(lèi)庫(kù)。。 ICSharpCode.SharpZipLib.dll)

代碼如下:

using System;

using System.IO;

using System.Collections.Generic;

using System.Text;

using ICSharpCode.SharpZipLib.Zip;

namespace ZipLib

{

/// <summary>

/// 解壓縮類(lèi)

/// </summary>

public static class ZIP

{

/// <summary>

/// 解壓ZIP文件包

/// </summary>

/// <param name="strZipFile">ZIP文件路徑</param>

/// <param name="strDir">解壓后的文件目錄路徑</param>

/// <returns>是否解壓成功</returns>

public static bool unzipFiles(string strZipFile, string strDir)

{

//判斷ZIP文件是否存在

if (File.Exists(strZipFile))

{

//判斷目錄是否存在

bool bUnzipDir = false;

//判斷是否需要?jiǎng)?chuàng)建目錄

if (!Directory.Exists(strDir))

bUnzipDir = (Directory.CreateDirectory(strDir) != null);

else

bUnzipDir = true;

//如果解壓目錄存在

if (bUnzipDir)

{

//獲得ZIP數(shù)據(jù)流

ZipInputStream zipStream = new ZipInputStream(File.OpenRead(strZipFile));

if (zipStream != null)

{

ZipEntry zipEntry = null;

while ((zipEntry = zipStream.GetNextEntry()) != null)

{

string strUnzipFile = strDir + "http://" + zipEntry.Name;

string strFileName = Path.GetFileName(strUnzipFile);

string strDirName = Path.GetDirectoryName(strUnzipFile);

//是否為解壓目錄

if (!string.IsNullOrEmpty(strDirName))

Directory.CreateDirectory(strDirName);

//是否為解壓文件

if (!string.IsNullOrEmpty(strFileName))

{

//解壓文件

FileStream unzipFileStream = new FileStream(strUnzipFile, FileMode.Create);

if (unzipFileStream != null)

{

byte[] buf = new byte[2048];

int size = 0;

while ((size = zipStream.Read(buf, 0, 2048)) > 0)

unzipFileStream.Write(buf, 0, size);

//關(guān)閉Stream

unzipFileStream.Flush();

unzipFileStream.Close();

}

}

}

//關(guān)閉ZIP流

zipStream.Close();

//返回值

return true;

}

}

}

return false;

}

}

}

更多信息請(qǐng)查看IT技術(shù)專(zhuān)欄

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:asp.net C#實(shí)現(xiàn)解壓縮文件的方法
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢(xún)回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門(mén)公布的正式信息和咨詢(xún)?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽(tīng)報(bào)名

  • 報(bào)班類(lèi)型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢(xún) | 簡(jiǎn)要咨詢(xún)須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專(zhuān)用圖標(biāo)
聯(lián)系電話(huà):0871-65099533/13759567129 獲取招聘考試信息及咨詢(xún)關(guān)注公眾號(hào):hfpxwx
咨詢(xún)QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專(zhuān)用圖標(biāo)