亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > .NET > 正文

asp.net C#實現解壓縮文件的方法

2024-07-10 13:28:23
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了asp.net C#實現解壓縮文件的方法,分別講述了三種不同的實現方法,是非常實用的技巧,需要的朋友可以參考下
 
 

本文實例講述了asp.net C#實現解壓縮文件的方法。一共給大家介紹了三段代碼,一個是簡單的解壓縮單個zip文件,后一個可以解壓批量的大量的但需要調用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, "解壓文件時發生錯誤:");
            }
            finally
            {
                // Make sure we allways close all streams
                if ( sourceStream != null )
                    sourceStream.Close ( );
                if ( decompressedStream != null )
                    decompressedStream.Close ( );
                if ( destinationStream != null )
                    destinationStream.Close ( );
            }
}

 

批量解壓縮(這需要調用一個解壓縮類庫。。 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;
                //判斷是否需要創建目錄
                if (!Directory.Exists(strDir))
                    bUnzipDir = (Directory.CreateDirectory(strDir) != null);
                else
                    bUnzipDir = true;
                //如果解壓目錄存在
                if (bUnzipDir)
                {
                    //獲得ZIP數據流
                    ZipInputStream zipStream = new ZipInputStream(File.OpenRead(strZipFile));
                    if (zipStream != null)
                    {
                        ZipEntry zipEntry = null;
                        while ((zipEntry = zipStream.GetNextEntry()) != null)
                        {
                            string strUnzipFile = strDir + "//" + 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);
                                    //關閉Stream
                                    unzipFileStream.Flush();
                                    unzipFileStream.Close();
                                }
                            }
                        }
                        //關閉ZIP流
                        zipStream.Close();
                        //返回值
                        return true;
                    }
                }
            }
            return false;
        }
    }
}

上面兩個都是解壓縮文件,下面我們把壓縮與解壓縮放在一個實例中。
最近要做一個項目涉及到C#中壓縮與解壓縮的問題的解決方法,大家分享。
這里主要解決文件夾包含文件夾的解壓縮問題。

 

下載SharpZipLib.dll,在http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx中有最新免費版本,“Assemblies for .NET 1.1, .NET 2.0, .NET CF 1.0, .NET CF 2.0: Download [297 KB] ”點擊Download可以下載,解壓后里邊有好多文件夾,因為不同的版本,我用的FW2.0。
 

引用SharpZipLib.dll,在項目中點擊項目右鍵-->添加引用-->瀏覽,找到要添加的DLL-->確認

改寫了文件壓縮和解壓縮的兩個類,新建兩個類名字為ZipFloClass.cs,UnZipFloClass.cs
源碼如下

 

復制代碼代碼如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.IO;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;
/// <summary>
/// ZipFloClass 的摘要說明
/// </summary>
public class ZipFloClass
{
    public void ZipFile(string strFile, string strZip)
    {
        if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar)
            strFile += Path.DirectorySeparatorChar;
        ZipOutputStream s = new ZipOutputStream(File.Create(strZip));
        s.SetLevel(6); // 0 - store only to 9 - means best compression
        zip(strFile, s, strFile);
        s.Finish();
        s.Close();
    }

 

    private void zip(string strFile, ZipOutputStream s, string staticFile)
    {
        if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar) strFile += Path.DirectorySeparatorChar;
        Crc32 crc = new Crc32();
        string[] filenames = Directory.GetFileSystemEntries(strFile);
        foreach (string file in filenames)
        {
            if (Directory.Exists(file))
            {
                zip(file, s, staticFile);
            }
            else // 否則直接壓縮文件
            {
                //打開壓縮文件
                FileStream fs = File.OpenRead(file);
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                string tempfile = file.Substring(staticFile.LastIndexOf("/") + 1);
                ZipEntry entry = new ZipEntry(tempfile);
                entry.DateTime = DateTime.Now;
                entry.Size = fs.Length;
                fs.Close();
                crc.Reset();
                crc.Update(buffer);
                entry.Crc = crc.Value;
                s.PutNextEntry(entry);
                s.Write(buffer, 0, buffer.Length);
            }
        }
    }
}

 

 

復制代碼代碼如下:
using System;
using System.Data;
using System.Web;
using System.Text;
using System.Collections;
using System.IO;
using System.Diagnostics;
using System.Runtime.Serialization.Formatters.Binary;
using ICSharpCode.SharpZipLib.BZip2;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Checksums;

 

/// <summary>
/// UnZipFloClass 的摘要說明
/// </summary>
public class UnZipFloClass
{
    public string unZipFile(string TargetFile, string fileDir)
    {
        string rootFile = " ";
        try
        {
            //讀取壓縮文件(zip文件),準備解壓縮
            ZipInputStream s = new ZipInputStream(File.OpenRead(TargetFile.Trim()));
            ZipEntry theEntry;
            string path = fileDir;                   
            //解壓出來的文件保存的路徑
            string rootDir = " ";                        
            //根目錄下的第一個子文件夾的名稱
            while ((theEntry = s.GetNextEntry()) != null)
            {
                rootDir = Path.GetDirectoryName(theEntry.Name);                          
                //得到根目錄下的第一級子文件夾的名稱
                if (rootDir.IndexOf("/") >= 0)
                {
                    rootDir = rootDir.Substring(0, rootDir.IndexOf("/") + 1);
                }
                string dir = Path.GetDirectoryName(theEntry.Name);                    
                //根目錄下的第一級子文件夾的下的文件夾的名稱
                string fileName = Path.GetFileName(theEntry.Name);                    
                //根目錄下的文件名稱
                if (dir != " " )                                                        
                    //創建根目錄下的子文件夾,不限制級別
                {
                    if (!Directory.Exists(fileDir + "/" + dir))
                    {
                        path = fileDir + "/" + dir;                                                
                        //在指定的路徑創建文件夾
                        Directory.CreateDirectory(path);
                    }
                }
                else if (dir == " " && fileName != "")                                             
                    //根目錄下的文件
                {
                    path = fileDir;
                    rootFile = fileName;
                }
                else if (dir != " " && fileName != "")                                             
                    //根目錄下的第一級子文件夾下的文件
                {
                    if (dir.IndexOf("/") > 0)                                                            
                        //指定文件保存的路徑
                    {
                        path = fileDir + "/" + dir;
                    }
                }
                if (dir == rootDir)                                                                                  
                    //判斷是不是需要保存在根目錄下的文件
                {
                    path = fileDir + "/" + rootDir;
                }
                //以下為解壓縮zip文件的基本步驟
                //基本思路就是遍歷壓縮文件里的所有文件,創建一個相同的文件。
                if (fileName != String.Empty)
                {
                    FileStream streamWriter = File.Create(path + "/" + fileName);
                    int size = 2048;
                    byte[] data = new byte[2048];
                    while (true)
                    {
                        size = s.Read(data, 0, data.Length);
                        if (size > 0)
                        {
                            streamWriter.Write(data, 0, size);
                        }
                        else
                        {
                            break;
                        }
                    }
                    streamWriter.Close();
                }
            }
            s.Close();
            return rootFile;
        }
        catch (Exception ex)
        {
            return "1; " + ex.Message;
        }
    }   
}


引用,新建一個頁面,添加兩個按鈕,為按鈕添加Click事件
源碼如下

 

 

復制代碼代碼如下:
protected void Button1_Click(object sender, EventArgs e)
{
        string[] FileProperties = new string[2];
        FileProperties[0] = "D:/unzipped/";//待壓縮文件目錄
        FileProperties[1] = "D:/zip/a.zip";  //壓縮后的目標文件
        ZipFloClass Zc = new ZipFloClass();
        Zc.ZipFile(FileProperties[0], FileProperties[1]);
}
protected void Button2_Click(object sender, EventArgs e)
{
        string[] FileProperties = new string[2];
        FileProperties[0] = "D:/zip/b.zip";//待解壓的文件
        FileProperties[1] = "D:/unzipped/";//解壓后放置的目標目錄
        UnZipFloClass UnZc = new UnZipFloClass();
        UnZc.unZipFile(FileProperties[0], FileProperties[1]);
}

 

希望本文所述對大家的asp.net程序設計有所幫助。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
欧美另类暴力丝袜| 国产精品免费福利| 久久精品国产96久久久香蕉| 国产成人亚洲综合青青| 最近中文字幕2019免费| 亚洲精品综合精品自拍| 色综合五月天导航| 91高潮在线观看| 77777亚洲午夜久久多人| 久久人人爽人人| 热久久免费国产视频| 亚洲成人精品久久久| 亚洲视频视频在线| xxx欧美精品| 国产精品成人观看视频国产奇米| 久久免费视频网站| 国产日韩欧美视频| 精品久久久久久久久国产字幕| 国产成人短视频| 欧美视频第一页| 精品久久久久久国产91| 亚洲人成电影网站色xx| 亚洲最大中文字幕| 欧美剧在线观看| 午夜精品美女自拍福到在线| 亚洲女同精品视频| 国产精品极品在线| 成人午夜在线视频一区| 91亚洲精华国产精华| 上原亚衣av一区二区三区| 国产精品成人aaaaa网站| 久久亚洲综合国产精品99麻豆精品福利| 日韩激情av在线播放| 国产欧美精品在线播放| 成人在线观看视频网站| 色七七影院综合| 91精品在线国产| 久久久久国产精品免费网站| 亚洲图片欧美日产| 深夜成人在线观看| 欧美激情性做爰免费视频| 成人激情黄色网| 欧洲成人性视频| 久久精品最新地址| 亚洲第一级黄色片| 久久午夜a级毛片| 亚洲精品国产品国语在线| 中文字幕日韩精品在线| 国产一区二区视频在线观看| 精品亚洲一区二区| 亚洲成人久久电影| 久久久久国产视频| 亚洲一区二区久久久久久| 不卡av在线网站| 国产91精品视频在线观看| 国产精品视频公开费视频| 久久的精品视频| 久久国产精品99国产精| 久久亚洲影音av资源网| 国模视频一区二区三区| 97av在线视频| 国产精品扒开腿做爽爽爽男男| 亚洲另类图片色| 欧美日韩亚洲一区二区| 久久久久久久久久亚洲| 国产精品午夜视频| 91精品国产高清久久久久久久久| 久久免费视频网| 亚洲一区二区精品| 日韩国产在线播放| 日韩激情视频在线播放| 国产成人+综合亚洲+天堂| 尤物yw午夜国产精品视频| 亚洲一品av免费观看| 日韩激情av在线免费观看| 国产不卡av在线| 国产精品麻豆va在线播放| 欧美疯狂xxxx大交乱88av| 日韩美女免费观看| 欧美性生交xxxxx久久久| 九九热最新视频//这里只有精品| 亚洲成人精品在线| 国产91成人在在线播放| 美女av一区二区| 亚洲电影成人av99爱色| 国产亚洲欧洲黄色| 欧美放荡办公室videos4k| 国产一区二中文字幕在线看| 欧美专区在线播放| 亚洲 日韩 国产第一| 中文字幕在线亚洲| 国产精品一区二区三区久久久| 亚洲在线第一页| 国产亚洲精品美女| 精品性高朝久久久久久久| 在线观看国产精品91| 美女扒开尿口让男人操亚洲视频网站| 91亚洲永久免费精品| 日韩欧美在线视频| 国产日韩精品入口| 欧美亚洲激情在线| 神马国产精品影院av| 日韩中文字在线| 在线观看日韩www视频免费| 欧美日韩一区二区在线播放| 日韩在线视频导航| 欧美日韩国产va另类| 欧美日韩一二三四五区| 日韩美女在线观看| 成人在线精品视频| 国产一区二区三区免费视频| 91夜夜未满十八勿入爽爽影院| 最近中文字幕2019免费| 久久久久久亚洲| 在线精品播放av| 92国产精品久久久久首页| 国产精品久久久久影院日本| 国产精品视频导航| 国产精品揄拍500视频| 亚洲高清免费观看高清完整版| 中文字幕精品久久久久| 啊v视频在线一区二区三区| 欧美国产欧美亚洲国产日韩mv天天看完整| 清纯唯美亚洲综合| 国产欧美va欧美va香蕉在线| 日韩av在线播放资源| 亚洲第一福利网站| 久久人体大胆视频| 日本国产欧美一区二区三区| 日韩美女主播视频| 狠狠综合久久av一区二区小说| 在线亚洲男人天堂| 成人午夜激情免费视频| 国产狼人综合免费视频| 91国内在线视频| 精品久久久久久亚洲精品| 成人国产精品av| 欧美日韩在线一区| 中文字幕一区二区三区电影| 国产精品亚洲第一区| 成人午夜高潮视频| 成人欧美一区二区三区黑人| 欧美电影在线观看完整版| 亚洲天堂成人在线| 久久久爽爽爽美女图片| 精品久久久久久久久久久久| 欧美中文字幕在线观看| 久久久久久久999| 在线亚洲午夜片av大片| 久久亚洲精品一区二区| 久久91亚洲精品中文字幕奶水| 一区二区中文字幕| 91在线无精精品一区二区| 久久中文字幕国产| 懂色aⅴ精品一区二区三区蜜月| 91在线观看免费高清完整版在线观看| 一本大道香蕉久在线播放29| 亚洲欧美日韩精品久久亚洲区| 在线观看国产成人av片| 色悠久久久久综合先锋影音下载| 国产精品r级在线| 亚洲欧美日韩一区在线| 精品国产福利视频| 亚洲欧美日韩图片|