午夜国产狂喷潮在线观看|国产AⅤ精品一区二区久久|中文字幕AV中文字幕|国产看片高清在线

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

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

    >>>點擊進入<<<

    網(wǎng)校開發(fā)及擁有的課件范圍涉及公務(wù)員、財會類、外語類、外貿(mào)類、學歷類、

    職業(yè)資格類、計算機類、建筑工程類、等9大類考試的在線網(wǎng)絡(luò)培訓輔導。

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

    解壓縮單個文件:

    代碼如下:

    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, "解壓文件時發(fā)生錯誤:");

    }

    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)用一個解壓縮類庫。。 ICSharpCode.SharpZipLib.dll)

    代碼如下:

    using System;

    using System.IO;

    using System.Collections.Generic;

    using System.Text;

    using ICSharpCode.SharpZipLib.Zip;

    namespace ZipLib

    {

    /// <summary>

    /// 解壓縮類

    /// </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;

    //判斷是否需要創(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;

    }

    }

    }

    更多信息請查看IT技術(shù)專欄

    更多信息請查看網(wǎng)絡(luò)編程
    易賢網(wǎng)手機網(wǎng)站地址:asp.net C#實現(xiàn)解壓縮文件的方法

    2025國考·省考課程試聽報名

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