asp.net C#實(shí)現(xiàn)解壓縮文件的方法

字號(hào):


    易賢網(wǎng)網(wǎng)校上線了!
    >>>點(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)考試的在線網(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;
    }
    }
    }