成人午夜激情影院,小视频免费在线观看,国产精品夜夜嗨,欧美日韩精品一区二区在线播放

基于ASP.NET中文件的壓縮與解壓

2010-09-10 14:15:46來源:作者:

  這里筆者為大家介紹在asp.net中使用文件的壓縮與解壓。在asp.net中使用壓縮給大家帶來的好處是顯而易見的,首先是減小了服務器端文件存儲的空間,其次下載時候下載的是壓縮文件想必也會有效果吧,特別是比較大的

  這里筆者為大家介紹在asp.net中使用文件的壓縮與解壓。在asp.net中使用壓縮給大家帶來的好處是顯而易見的,首先是減小了服務器端文件存儲的空間,其次下載時候下載的是壓縮文件想必也會有效果吧,特別是比較大的文件。有的客戶可能會很粗心上傳的是文件,那么可以通過判斷后綴名來判斷文件,不是壓縮文件,就可以壓縮文件,在存儲。

  這里筆者引用了一個DLL文件(ICSharpCode.SharpZipLib.dll)(包含在本文代碼中),調用其中的函數,就可以對文件進行壓縮及解壓了。其中壓縮筆者主要用到的函數是

C# Code復制內容到剪貼板
  1. // <summary>   
  2.          /// 壓縮文件   
  3.          /// </summary>   
  4.          /// <param name="fileName">要壓縮的所有文件(完全路徑)</param>   
  5.          /// <param name="name">壓縮后文件路徑</param>   
  6.         /// <param name="Level">壓縮級別</param>   
  7.           public void ZipFileMain(string[] filenames, string name, int Level)   
  8.          {   
  9.              ZipOutputStream s = new ZipOutputStream(File.Create(name));   
  10.              Crc32 crc = new Crc32();   
  11.              //壓縮級別   
  12.              s.SetLevel(Level); // 0 - store only to 9 - means best compression   
  13.              try  
  14.              {   
  15.                  foreach (string file in filenames)   
  16.                  {   
  17.                      //打開壓縮文件                     FileStream fs = File.OpenRead(file);   
  18.                      byte[] buffer = new byte[fs.Length];   
  19.                      fs.Read(buffer, 0, buffer.Length);   
  20.                                           //建立壓縮實體                     ZipEntry entry = new ZipEntry(System.IO.Path.GetFileName(file));   
  21.                      //時間                     entry.DateTime = DateTime.Now;   
  22.                     // set Size and the crc, because the information   
  23.                      // about the size and crc should be stored in the header   
  24.                      // if it is not set it is automatically written in the footer.   
  25.                      // (in this case size == crc == -1 in the header)                     // Some ZIP programs have problems with zip files that don't store   
  26.                      // the size and crc in the header.   
  27.                      //空間大小                     entry.Size = fs.Length;   
  28.                      fs.Close();   
  29.                      crc.Reset();   
  30.                      crc.Update(buffer);   
  31.                      entry.Crc = crc.Value;   
  32.                      s.Write(buffer, 0, buffer.Length);   
  33.                  }   
  34.              }   
  35.              catch  
  36.              {   
  37.                  throw;   
  38.              }   
  39.              finally  
  40.        {   
  41.                  s.Finish();   
  42.                  s.Close();   
  43.              }   
  44.          }  

  解壓縮的主要代碼

C# Code復制內容到剪貼板
  1. /// <summary>   
  2.          /// 解壓文件   
  3.          /// </summary>   
  4.         /// <param name="ZipPath">被解壓的文件路徑</param>   
  5.          /// <param name="Path">解壓后文件的路徑</param>   
  6.          public void UnZip(string ZipPath,string Path)   
  7.          {   
  8.              ZipInputStream s = new ZipInputStream(File.OpenRead(ZipPath));   
  9.             ZipEntry theEntry;   
  10.             try  
  11.                  while ((theEntry = s.GetNextEntry()) != null)   
  12.                  {   
  13.                      string fileName = System.IO.Path.GetFileName(theEntry.Name);   
  14.                      //生成解壓目錄                     Directory.CreateDirectory(Path);   
  15.                     if (fileName != String.Empty)   
  16.                      {   
  17.                          //解壓文件                         FileStream streamWriter = File.Create(Path + fileName);   
  18.                         int size = 2048;   
  19.                          byte[] data = new byte[2048];   
  20.                          while (true)                         {   
  21.                             size = s.Read(data, 0, data.Length);   
  22.                              if (size > 0)                             {   
  23.                                  streamWriter.Write(data, 0, size);   
  24.                              }                            else  
  25.                              {   
  26.                                 streamWriter.Close();   
  27.                                  streamWriter.Dispose();   
  28.                                  break;   
  29.                              }                         }   
  30.                          streamWriter.Close();   
  31.                          streamWriter.Dispose();   
  32.                      }   
  33.                  }   
  34.              }   
  35.              catch  
  36.              {   
  37.                  throw;   
  38.              }   
  39.              finally  
  40.              {   
  41.                  s.Close();   
  42.                  s.Dispose();   
  43.              }   
  44.          }   
  45.      }  

  我這里做了個簡單的測試程序(點擊下載)

1
 

  這里已知道要被壓縮文件,這里只需填入要被壓縮到的路徑("D:\text\")解壓路徑一樣。這里解壓級別越大,壓縮的就越厲害。

  可以看測試小程序,將解壓/壓縮引入到你的項目中。

關鍵詞:ASP.NET

贊助商鏈接:

主站蜘蛛池模板: 呼伦贝尔市| 柳江县| 新和县| 蕉岭县| 集安市| 大同县| 晋城| 宁城县| 舒兰市| 奈曼旗| 长岭县| 昆明市| 于都县| 昌邑市| 建瓯市| 梁河县| 滦南县| 浑源县| 丹巴县| 铜山县| 临泉县| 温州市| 固始县| 仙游县| 湟源县| 赞皇县| 桐梓县| 阿克| 咸阳市| 那坡县| 启东市| 错那县| 北宁市| 彭山县| 淳安县| 类乌齐县| 武功县| 福海县| 阿瓦提县| 临洮县| 汶川县|