麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > C# > 正文

C#中關于zip壓縮解壓幫助類的封裝 附源碼下載

2020-01-24 03:35:18
字體:
來源:轉載
供稿:網友
c#下壓縮解壓,主要是用第三方類庫進行封裝的。ICSharpCode.SharpZipLib.dll類庫,鏈接地址為你官方下載鏈接。壓縮主要是用流的方式進行壓縮的。

壓縮文件及文件夾。文件壓縮很簡單,把待壓縮的文件用流的方式讀到內存中,然后放到壓縮流中。就可以了。文件夾就稍微麻煩下了。因為要把待壓縮的文件夾解壓后保留文件夾文件的層次結構。所以我的實現方式就是 遞歸遍歷文件夾中的文件。計算其相對位置放到壓縮流中。

代碼如下

復制代碼 代碼如下:

/// <summary>
        /// 壓縮文件或者文件夾
        /// </summary>
        /// <param name="_depositPath">壓縮后文件的存放路徑   如C://windows/abc.zip</param>
        /// <returns></returns>
        public bool CompressionZip(string _depositPath)
        {
            bool result = true;
            FileStream fs = null;
            try
            {
                ZipOutputStream ComStream = new ZipOutputStream(File.Create(_depositPath));
                ComStream.SetLevel(9);      //壓縮等級
                foreach (string path in AbsolutePaths)
                {
                    //如果是目錄
                    if (Directory.Exists(path))
                    {
                        ZipFloder(path, ComStream, path);
                    }
                    else if (File.Exists(path))//如果是文件
                    {
                         fs = File.OpenRead(path);
                        byte[] bts = new byte[fs.Length];
                        fs.Read(bts, 0, bts.Length);
                        ZipEntry ze = new ZipEntry(new FileInfo(path).Name);
                        ComStream.PutNextEntry(ze);             //為壓縮文件流提供一個容器
                        ComStream.Write(bts, 0, bts.Length);  //寫入字節
                    }
                }
                ComStream.Finish(); // 結束壓縮
                ComStream.Close();
            }
            catch (Exception ex)
            {
                if (fs != null)
                {
                    fs.Close();
                }
                errorMsg = ex.Message;
                result = false;
            }
            return result;
        }
        //壓縮文件夾
        private void ZipFloder(string _OfloderPath, ZipOutputStream zos, string _floderPath)
        {
            foreach (FileSystemInfo item in new DirectoryInfo(_floderPath).GetFileSystemInfos())
            {
                if (Directory.Exists(item.FullName))
                {
                    ZipFloder(_OfloderPath, zos, item.FullName);
                }
                else if (File.Exists(item.FullName))//如果是文件
                {
                    DirectoryInfo ODir = new DirectoryInfo(_OfloderPath);
                    string fullName2 = new FileInfo(item.FullName).FullName;
                    string path = ODir.Name + fullName2.Substring(ODir.FullName.Length, fullName2.Length - ODir.FullName.Length);//獲取相對目錄
                    FileStream fs = File.OpenRead(fullName2);
                    byte[] bts = new byte[fs.Length];
                    fs.Read(bts, 0, bts.Length);
                    ZipEntry ze = new ZipEntry(path);
                    zos.PutNextEntry(ze);             //為壓縮文件流提供一個容器
                    zos.Write(bts, 0, bts.Length);  //寫入字節
                }
            }
        }

關于解壓  解壓就簡單多了。有文件解壓文件,有文件夾 遍歷,解壓其中的文件。解壓的文件中已經包含了其與文件夾的層次關系。

復制代碼 代碼如下:

/// <summary>
        /// 解壓
        /// </summary>
        /// <param name="_depositPath">壓縮文件路徑</param>
        /// <param name="_floderPath">解壓的路徑</param>
        /// <returns></returns>
        public bool DeCompressionZip(string _depositPath, string _floderPath)
        {
            bool result = true;
            FileStream fs=null;
            try
            {
                ZipInputStream InpStream = new ZipInputStream(File.OpenRead(_depositPath));
                ZipEntry ze = InpStream.GetNextEntry();//獲取壓縮文件中的每一個文件
                Directory.CreateDirectory(_floderPath);//創建解壓文件夾
                while (ze != null)//如果解壓完ze則是null
                {
                    if (ze.IsFile)//壓縮zipINputStream里面存的都是文件。帶文件夾的文件名字是文件夾//文件名
                    {
                        string[] strs=ze.Name.Split('//');//如果文件名中包含'//‘則表明有文件夾
                        if (strs.Length > 1)
                        {
                            //兩層循環用于一層一層創建文件夾
                            for (int i = 0; i < strs.Length-1; i++)
                            {
                                string floderPath=_floderPath;
                                for (int j = 0; j < i; j++)
                                {
                                    floderPath = floderPath + "http://" + strs[j];
                                }
                                floderPath=floderPath+"http://"+strs[i];
                                Directory.CreateDirectory(floderPath);
                            }
                        }
                         fs = new FileStream(_floderPath+"http://"+ze.Name, FileMode.OpenOrCreate, FileAccess.Write);//創建文件
                        //循環讀取文件到文件流中
                        while (true)
                        {
                            byte[] bts = new byte[1024];
                           int i= InpStream.Read(bts, 0, bts.Length);
                           if (i > 0)
                           {
                               fs.Write(bts, 0, i);
                           }
                           else
                           {
                               fs.Flush();
                               fs.Close();
                               break;
                           }
                        }
                    }
                    ze = InpStream.GetNextEntry();
                }
            }
            catch (Exception ex)
            {
                if (fs != null)
                {
                    fs.Close();
                }
                errorMsg = ex.Message;
                result = false;
            }
            return result;
        }

最后做個總結。C#作為高級語言,其強大的類庫和第三方提供的類庫。可以做很多事情。但也有弊端,用第三方類庫性能不是很高。我壓縮幾百M的東西。cpu瞬間跑到50%多。比360壓縮和zip壓縮性能差遠了。所以此類也就適用壓縮比較小的東西。

完整例子下載地址

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 精品久久中文网址 | 欧美黄成人免费网站大全 | 免费永久在线观看黄网 | 精品国产91久久久久久浪潮蜜月 | 污片视频网站 | 国产精品久久久久无码av | 成年人黄色免费电影 | 青青国产在线视频 | 青草伊人网 | 在线播放视频一区二区 | 色七七亚洲 | 美女视频黄视大全视频免费网址 | 啪啪毛片 | 欧美成人免费电影 | chengrenzaixian | 久国产 | 视频一区 在线 | 国产免费让你躁在线视频 | 一级黄色av电影 | 麻豆一二区 | 极品xxxx欧美一区二区 | 一级性生活视频 | 精国品产一区二区三区有限公司 | 91热久久免费频精品黑人99 | 99视频有精品 | av色哟哟 | 美女喷水网站 | 在线亚洲播放 | 久久精品在线免费观看 | 9999免费视频 | 精品一区二区久久久久久按摩 | 久久国产精品影视 | 国产精品免费视频观看 | 人禽l交免费视频 | 九九热免费精品视频 | av在线影片| 越南一级黄色片 | 一级毛片播放 | 国产免费视频一区二区裸体 | 毛毛片在线看 | 黄视频网站免费在线观看 |