好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > ASP.NET 文件壓縮解壓類(C#)

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

熱門標簽:400電話辦理怎么樣 聯通官網400電話辦理 臨沂智能電話機器人加盟 網絡電話外呼系統上海 蘇州如何辦理400電話 西寧呼叫中心外呼系統線路商 外呼電話機器人成本 百應電話機器人外呼系統 地圖標注軟件免費下載

本文實例講述了asp.net C#實現解壓縮文件的方法,需要引用一個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>  /// 作者:來自網格br>  /// 修改人:sunkaixaun
  /// 壓縮和解壓文件 
  /// /summary> 
  public class ZipClass
  {
    /// summary> 
    /// 所有文件緩存 
    /// /summary> 
    Liststring> files = new Liststring>();
 
    /// summary> 
    /// 所有空目錄緩存 
    /// /summary> 
    Liststring> paths = new Liststring>();

    /// summary> 
    /// 壓縮單個文件根據文件地址
    /// /summary> 
    /// param name="fileToZip">要壓縮的文件/param> 
    /// param name="zipedFile">壓縮后的文件全名/param> 
    /// param name="compressionLevel">壓縮程度,范圍0-9,數值越大,壓縮程序越高/param> 
    /// param name="blockSize">分塊大小/param> 
    public void ZipFile(string fileToZip, string zipedFile, int compressionLevel, int blockSize)
    {
      if (!System.IO.File.Exists(fileToZip))//如果文件沒有找到,則報錯 
      {
        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,數值越大,壓縮程序越高/param> 
    public void ZipFileFromDirectory(string rootPath, string destinationPath, int compressLevel)

    {

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

      { 

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

      } 
      */

      //string rootMark = rootPath.Substring(0, rootPath.LastIndexOf("\\") + 1);//得到當前路徑的位置,以備壓縮時將所壓縮內容轉變成相對路徑。 
      string rootMark = rootPath + "\\";//得到當前路徑的位置,以備壓縮時將所壓縮內容轉變成相對路徑。 
      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();//通過這個名稱格式化器,可以將里面的文件名進行一些處理。默認情況下,會自動根據文件的路徑在zip中創建有關的文件夾。

        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);//對每一個字目錄做與根目錄相同的操作:即找到子目錄并將當前目錄的文件名存入List 

      }

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

      foreach (string file in files)

      {
        this.files.Add(file);//將當前目錄中的所有文件全名存入文件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 Liststring> UnZip(string zipfilepath, string unzippath)

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

      Liststring> unzipFiles = new Liststring>();
      //檢查輸出目錄是否以“\\”結尾 

      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);

 

        //生成解壓目錄【用戶解壓到硬盤根目錄時,不需要創建】 

        if (!string.IsNullOrEmpty(directoryName))

        {

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

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

          if (theEntry.CompressedSize == 0)

            break;

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

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

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

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

          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
  }
} 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。 

您可能感興趣的文章:
  • C#調用7z實現文件的壓縮與解壓
  • 詳解C#壓縮、解壓文件夾/文件(帶密碼)
  • C#使用GZipStream實現文件的壓縮與解壓
  • c# 文件壓縮zip或將zip文件解壓的方法
  • C#使用ICSharpCode.SharpZipLib.dll進行文件的壓縮與解壓功能
  • c#打包文件解壓縮的實例
  • C#實現壓縮和解壓縮的方法示例【Gzip和Zip方式】
  • C#實現文件壓縮與解壓的方法示例【ZIP格式】
  • C#使用WinRar命令進行壓縮和解壓縮操作的實現方法
  • 如何在c#中使用Zlib壓縮與解壓

標簽:清遠 平涼 臨夏 聊城 中衛 慶陽 甘肅 海西

巨人網絡通訊聲明:本文標題《ASP.NET 文件壓縮解壓類(C#)》,本文關鍵詞  ASP.NET,文件,壓縮,解壓,類,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《ASP.NET 文件壓縮解壓類(C#)》相關的同類信息!
  • 本頁收集關于ASP.NET 文件壓縮解壓類(C#)的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 特级毛片免费观看视频| 美女如云坐厕丝袜偷拍| 欧美男男gay1069大长吊| 蜜臀久久99精品久久久久久酒店| 性放荡一级小说| 丁香激情综合色伊人久久| 成人窝窝午夜看片| 性饥渴寡妇肉乱免费视频 | 97人妻精品一区二区三区动漫 | 韩国三级bd高清中字木鱼天在线观看| 九九久久精品视频| 一本无码人妻一区二区| vr成人做爰A片| 日本????69少妇学校| 清冷受高H被强cao哭高H漫画| 国产一级a毛一级a做免费图片| 九九视频麻豆精品| 五月婷婷综合激情| 九九资源站| 高校教师在线观看| 久污tv| 亚裔大战黑人老外AV3p| 美人诱受多汁水肉np| 足球尤物电影完整版在线观看| 337P粉嫩大胆色噜噜噜噜| 白雪公主裸体被7个小矮人强| 国产91??对白在线播放| 杨幂ai换脸??视频在线观看| 国模娜娜大胆扒开自慰| 久久久久久久久免费影院| 性欧美巨大极品videos| 免费看男人吃奶摸捏奶头视频| 美女???免费视频观看| 亲子乱子伦xxxxxinin| mm131美女大爽爽| 年轻漂亮的女邻居hd| 女人两个奶头被吃高潮| 男女激烈XXOO无遮挡| 强烈按揉你的胸漫画无遮挡| 岳好紧好紧我要进去了视频| 夫目前|