最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

ASP.NET 文件壓縮解壓類(C#)

 更新時(shí)間:2016年07月11日 10:19:22   作者:孫凱旋  
這篇文章主要為大家詳細(xì)介紹了ASP.NET 文件壓縮解壓類,感興趣的小伙伴們可以參考一下

本文實(shí)例講述了asp.net C#實(shí)現(xiàn)解壓縮文件的方法,需要引用一個(gè)ICSharpCode.SharpZipLib.dll,供大家參考,具體如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;
using ICSharpCode.SharpZipLib.Checksums;
using System.Web;
namespace Mvc51Hiring.Common.Tool
{
  /// <summary> <br>  /// 作者:來自網(wǎng)格<br>  /// 修改人:sunkaixaun
  /// 壓縮和解壓文件 
  /// </summary> 
  public class ZipClass
  {
    /// <summary> 
    /// 所有文件緩存 
    /// </summary> 
    List<string> files = new List<string>();
 
    /// <summary> 
    /// 所有空目錄緩存 
    /// </summary> 
    List<string> paths = new List<string>();

    /// <summary> 
    /// 壓縮單個(gè)文件根據(jù)文件地址
    /// </summary> 
    /// <param name="fileToZip">要壓縮的文件</param> 
    /// <param name="zipedFile">壓縮后的文件全名</param> 
    /// <param name="compressionLevel">壓縮程度,范圍0-9,數(shù)值越大,壓縮程序越高</param> 
    /// <param name="blockSize">分塊大小</param> 
    public void ZipFile(string fileToZip, string zipedFile, int compressionLevel, int blockSize)
    {
      if (!System.IO.File.Exists(fileToZip))//如果文件沒有找到,則報(bào)錯(cuò) 
      {
        throw new FileNotFoundException("The specified file " + fileToZip + " could not be found. Zipping aborderd");
      }

      FileStream streamToZip = new FileStream(fileToZip, FileMode.Open, FileAccess.Read);
      FileStream zipFile = File.Create(zipedFile);
      ZipOutputStream zipStream = new ZipOutputStream(zipFile);
      ZipEntry zipEntry = new ZipEntry(fileToZip);
      zipStream.PutNextEntry(zipEntry);
      zipStream.SetLevel(compressionLevel);
      byte[] buffer = new byte[blockSize];
      int size = streamToZip.Read(buffer, 0, buffer.Length);
      zipStream.Write(buffer, 0, size);
      try
      {
        while (size < streamToZip.Length)

        {
          int sizeRead = streamToZip.Read(buffer, 0, buffer.Length);
          zipStream.Write(buffer, 0, sizeRead);
          size += sizeRead;
        }
      }

      catch (Exception ex)

      {

        GC.Collect();

        throw ex;

      }
      zipStream.Finish();

      zipStream.Close();

      streamToZip.Close();

      GC.Collect();

    }

    /// <summary> 
    /// 壓縮目錄(包括子目錄及所有文件) 
    /// </summary> 
    /// <param name="rootPath">要壓縮的根目錄</param> 
    /// <param name="destinationPath">保存路徑</param> 
    /// <param name="compressLevel">壓縮程度,范圍0-9,數(shù)值越大,壓縮程序越高</param> 
    public void ZipFileFromDirectory(string rootPath, string destinationPath, int compressLevel)

    {

      GetAllDirectories(rootPath);
      /* while (rootPath.LastIndexOf("\\") + 1 == rootPath.Length)//檢查路徑是否以"\"結(jié)尾 

      { 

       rootPath = rootPath.Substring(0, rootPath.Length - 1);//如果是則去掉末尾的"\" 

      } 
      */

      //string rootMark = rootPath.Substring(0, rootPath.LastIndexOf("\\") + 1);//得到當(dāng)前路徑的位置,以備壓縮時(shí)將所壓縮內(nèi)容轉(zhuǎn)變成相對(duì)路徑。 
      string rootMark = rootPath + "\\";//得到當(dāng)前路徑的位置,以備壓縮時(shí)將所壓縮內(nèi)容轉(zhuǎn)變成相對(duì)路徑。 
      Crc32 crc = new Crc32();
      ZipOutputStream outPutStream = new ZipOutputStream(File.Create(destinationPath));
      outPutStream.SetLevel(compressLevel); // 0 - store only to 9 - means best compression 
      foreach (string file in files)
      {
        FileStream fileStream = File.OpenRead(file);//打開壓縮文件 
        byte[] buffer = new byte[fileStream.Length];
        fileStream.Read(buffer, 0, buffer.Length);
        ZipEntry entry = new ZipEntry(file.Replace(rootMark, string.Empty));
        entry.DateTime = DateTime.Now;
        // set Size and the crc, because the information 
        // about the size and crc should be stored in the header 
        // if it is not set it is automatically written in the footer. 
        // (in this case size == crc == -1 in the header) 
        // Some ZIP programs have problems with zip files that don't store 
        // the size and crc in the header. 
        entry.Size = fileStream.Length;
        fileStream.Close();
        crc.Reset();
        crc.Update(buffer);
        entry.Crc = crc.Value;
        outPutStream.PutNextEntry(entry);
        outPutStream.Write(buffer, 0, buffer.Length);

      }
   this.files.Clear();

    foreach (string emptyPath in paths)
      {

        ZipEntry entry = new ZipEntry(emptyPath.Replace(rootMark, string.Empty) + "/");

        outPutStream.PutNextEntry(entry);

      }

      this.paths.Clear();
      outPutStream.Finish();
      outPutStream.Close();
      GC.Collect();

    }
    /// <summary> 
    /// 多文件打包下載
    /// </summary> 
    public void DwonloadZip(string[] filePathList, string zipName)

    {
      MemoryStream ms = new MemoryStream();
      byte[] buffer = null;
      var context = HttpContext.Current;
      using (ICSharpCode.SharpZipLib.Zip.ZipFile file = ICSharpCode.SharpZipLib.Zip.ZipFile.Create(ms))

      {
        file.BeginUpdate();

        file.NameTransform = new MyNameTransfom();//通過這個(gè)名稱格式化器,可以將里面的文件名進(jìn)行一些處理。默認(rèn)情況下,會(huì)自動(dòng)根據(jù)文件的路徑在zip中創(chuàng)建有關(guān)的文件夾。

        foreach (var it in filePathList)

        {

          file.Add(context.Server.MapPath(it));

        }
        file.CommitUpdate();
        buffer = new byte[ms.Length];
        ms.Position = 0;
        ms.Read(buffer, 0, buffer.Length);
      }

      context.Response.AddHeader("content-disposition", "attachment;filename=" + zipName);
      context.Response.BinaryWrite(buffer);
      context.Response.Flush();
      context.Response.End();

    }
    /// <summary> 
    /// 取得目錄下所有文件及文件夾,分別存入files及paths 
    /// </summary> 
    /// <param name="rootPath">根目錄</param> 
    private void GetAllDirectories(string rootPath)

    {

      string[] subPaths = Directory.GetDirectories(rootPath);//得到所有子目錄 

      foreach (string path in subPaths)

      {

        GetAllDirectories(path);//對(duì)每一個(gè)字目錄做與根目錄相同的操作:即找到子目錄并將當(dāng)前目錄的文件名存入List 

      }

      string[] files = Directory.GetFiles(rootPath);

      foreach (string file in files)

      {
        this.files.Add(file);//將當(dāng)前目錄中的所有文件全名存入文件List 
      }
      if (subPaths.Length == files.Length && files.Length == 0)//如果是空目錄 
      {
        this.paths.Add(rootPath);//記錄空目錄 

      }

    }
    /// <summary> 
    /// 解壓縮文件(壓縮文件中含有子目錄) 
    /// </summary> 
    /// <param name="zipfilepath">待解壓縮的文件路徑</param> 
    /// <param name="unzippath">解壓縮到指定目錄</param> 
    /// <returns>解壓后的文件列表</returns> 
    public List<string> UnZip(string zipfilepath, string unzippath)

    {
      //解壓出來的文件列表 

      List<string> unzipFiles = new List<string>();
      //檢查輸出目錄是否以“\\”結(jié)尾 

      if (unzippath.EndsWith("\\") == false || unzippath.EndsWith(":\\") == false)

      {

        unzippath += "\\";

      }
      ZipInputStream s = new ZipInputStream(File.OpenRead(zipfilepath));
      ZipEntry theEntry;
      while ((theEntry = s.GetNextEntry()) != null)

      {

        string directoryName = Path.GetDirectoryName(unzippath);

        string fileName = Path.GetFileName(theEntry.Name);

 

        //生成解壓目錄【用戶解壓到硬盤根目錄時(shí),不需要?jiǎng)?chuàng)建】 

        if (!string.IsNullOrEmpty(directoryName))

        {

          Directory.CreateDirectory(directoryName);
        }
        if (fileName != String.Empty)

        {
          //如果文件的壓縮后大小為0那么說明這個(gè)文件是空的,因此不需要進(jìn)行讀出寫入 

          if (theEntry.CompressedSize == 0)

            break;

          //解壓文件到指定的目錄 

          directoryName = Path.GetDirectoryName(unzippath + theEntry.Name);

          //建立下面的目錄和子目錄 

          Directory.CreateDirectory(directoryName);
         //記錄導(dǎo)出的文件 

          unzipFiles.Add(unzippath + theEntry.Name);
         FileStream streamWriter = File.Create(unzippath + theEntry.Name);
          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();

      GC.Collect();

      return unzipFiles;

    }
  }
  public class MyNameTransfom : ICSharpCode.SharpZipLib.Core.INameTransform
  {
    #region INameTransform 成員

    public string TransformDirectory(string name)
    {
      return null;
    }
    public string TransformFile(string name)
    {
      return Path.GetFileName(name);
    }
    #endregion
  }
} 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。 

相關(guān)文章

  • ASP.NET Core對(duì)不同類型的用戶進(jìn)行區(qū)別限流詳解

    ASP.NET Core對(duì)不同類型的用戶進(jìn)行區(qū)別限流詳解

    這篇文章主要介紹了ASP.NET Core對(duì)不同類型的用戶進(jìn)行區(qū)別限流的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • ASP.net無法加載oci.dll解決新法

    ASP.net無法加載oci.dll解決新法

    最近突然發(fā)現(xiàn)運(yùn)行程序時(shí)會(huì)出現(xiàn):無法加載oci.dll 的錯(cuò)誤,上網(wǎng)找了好久,總算解決了.下面把方法分享給大家。
    2015-03-03
  • Queryable.Union 方法實(shí)現(xiàn)json格式的字符串合并的具體實(shí)例

    Queryable.Union 方法實(shí)現(xiàn)json格式的字符串合并的具體實(shí)例

    這篇文章介紹了Queryable.Union 方法實(shí)現(xiàn)json格式的字符串合并的具體實(shí)例,有需要的朋友可以參考一下
    2013-10-10
  • HTTP錯(cuò)誤500.19解決方法(定義了重復(fù)的節(jié)點(diǎn))

    HTTP錯(cuò)誤500.19解決方法(定義了重復(fù)的節(jié)點(diǎn))

    HTTP 錯(cuò)誤 500.19 - Internal Server Error 無法訪問請(qǐng)求的頁(yè)面,因?yàn)樵擁?yè)的相關(guān)配置數(shù)據(jù)無效
    2013-06-06
  • ASP.NET Core中如何利用多種方式給Action傳參

    ASP.NET Core中如何利用多種方式給Action傳參

    這篇文章主要給大家介紹了關(guān)于ASP.NET Core中如何利用多種方式給Action傳參的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • asp.net 計(jì)劃任務(wù)管理程序?qū)崿F(xiàn),多線程任務(wù)加載

    asp.net 計(jì)劃任務(wù)管理程序?qū)崿F(xiàn),多線程任務(wù)加載

    b/s模式下用程序?qū)崿F(xiàn)計(jì)劃任務(wù),一直是個(gè)不太好解決和管理的問題,當(dāng)然可以采用ajax 計(jì)時(shí)器的方法模擬form端的timer事件。
    2009-11-11
  • .NET8中g(shù)RPC的使用方法詳解

    .NET8中g(shù)RPC的使用方法詳解

    gRPC是一種高性能、開源的遠(yuǎn)程過程調(diào)用(RPC)框架,基于 HTTP/2 協(xié)議,支持雙向流、頭部壓縮等特性,下面我們就來看看.NET8下gRPC的具體使用吧
    2025-03-03
  • .NET的Ajax請(qǐng)求數(shù)據(jù)提交實(shí)例

    .NET的Ajax請(qǐng)求數(shù)據(jù)提交實(shí)例

    這篇文章主要介紹了.NET的Ajax請(qǐng)求數(shù)據(jù)提交實(shí)例,較為詳細(xì)的分析了Ajax請(qǐng)求、數(shù)據(jù)的提交以及參數(shù)的傳遞技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-01-01
  • ASP.NET如何自定義項(xiàng)目模板詳解

    ASP.NET如何自定義項(xiàng)目模板詳解

    這篇文章主要給大家介紹了關(guān)于ASP.NET如何自定義項(xiàng)目模板的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用ASP.NET具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • .NET?Core企業(yè)微信開發(fā)接口回調(diào)配置

    .NET?Core企業(yè)微信開發(fā)接口回調(diào)配置

    這篇文章介紹了.NET?Core企業(yè)微信回調(diào)配置的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06

最新評(píng)論

临沭县| 儋州市| 伊金霍洛旗| 洞头县| 阜宁县| 新安县| 洪江市| 湘潭市| 福安市| 德格县| 虞城县| 黄浦区| 日土县| 隆昌县| 灌南县| 抚远县| 青海省| 邯郸市| 鄂伦春自治旗| 广丰县| 双牌县| 墨竹工卡县| 曲松县| 长白| 许昌县| 长汀县| 九江县| 高碑店市| 蓝田县| 宁陕县| 图片| 长岛县| 张家界市| 建湖县| 阜新市| 岳西县| 洪湖市| 富裕县| 怀来县| 陵水| 班戈县|