1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時(shí)間:8:30-17:00
      你可能遇到了下面的問(wèn)題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
      .NET多類型文件解壓縮組件SharpZipLib是怎樣的-創(chuàng)新互聯(lián)

      本篇文章給大家分享的是有關(guān).NET多類型文件解壓縮組件SharpZipLib是怎樣的,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。

      專注于為中小企業(yè)提供做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)咸豐免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了千余家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

      解壓縮組件SharpZipLib。

      SharpZipLib是一個(gè)完全在C#中為.NET平臺(tái)編寫的Zip,GZip,Tar和BZip2庫(kù)。

      一.SharpZipLib組件概述:

      ziplib(SharpZipLib,以前的NZipLib)是一個(gè)完全在C#為.NET平臺(tái)編寫的Zip,GZip,Tar和BZip2庫(kù)。它實(shí)現(xiàn)為一個(gè)程序集(可安裝在GAC中),因此可以輕松地集成到其他項(xiàng)目(任何.NET語(yǔ)言)中。 #ziplib的創(chuàng)建者這樣說(shuō):“我已經(jīng)將zip庫(kù)移植到C#,因?yàn)槲倚枰猤zip / zip壓縮,我不想使用libzip.dll或類似的東西我想要的所有在純C#“。

      SharpZipLib官網(wǎng)提供的下載操作:.NET 1.1,.NET 2.0(3.5,4.0),.NET CF 1.0,.NET CF 2.0的裝配:下載237 KB,源代碼和示例下載708 KB;源代碼和示例下載708 KB;幫助文件下載1208 KB;

      SharpZipLib是在GPL下發(fā)布,遵守開源協(xié)議。

      二.SharpZipLib核心類和方法介紹:

      以上簡(jiǎn)單的介紹了SharpZipLib組件的相關(guān)背景,現(xiàn)在具體看一下該組件的相關(guān)核心類和方法:

        .NET多類型文件解壓縮組件SharpZipLib是怎樣的

      .NET多類型文件解壓縮組件SharpZipLib是怎樣的

      1.ZipOutputStream類PutNextEntry():

      public void PutNextEntry(ZipEntry entry)
      {
        bool hasCrc;
        if (entry == null)
        {
          throw new ArgumentNullException("entry");
        }
        if (this.entries == null)
        {
          throw new InvalidOperationException("ZipOutputStream was finished");
        }
        if (this.curEntry != null)
        {
          this.CloseEntry();
        }
        if (this.entries.Count == 0x7fffffff)
        {
          throw new ZipException("Too many entries for Zip file");
        }
        CompressionMethod compressionMethod = entry.CompressionMethod;
        int defaultCompressionLevel = this.defaultCompressionLevel;
        entry.Flags &= 0x800;
        this.patchEntryHeader = false;
        if (entry.Size == 0L)
        {
          entry.CompressedSize = entry.Size;
          entry.Crc = 0L;
          compressionMethod = CompressionMethod.Stored;
          hasCrc = true;
        }
        else
        {
          hasCrc = (entry.Size >= 0L) && entry.HasCrc;
          if (compressionMethod == CompressionMethod.Stored)
          {
            if (!hasCrc)
            {
              if (!base.CanPatchEntries)
              {
                compressionMethod = CompressionMethod.Deflated;
                defaultCompressionLevel = 0;
              }
            }
            else
            {
              entry.CompressedSize = entry.Size;
              hasCrc = entry.HasCrc;
            }
          }
        }
        if (!hasCrc)
        {
          if (!base.CanPatchEntries)
          {
            entry.Flags |= 8;
          }
          else
          {
            this.patchEntryHeader = true;
          }
        }
        if (base.Password != null)
        {
          entry.IsCrypted = true;
          if (entry.Crc < 0L)
          {
            entry.Flags |= 8;
          }
        }
        entry.Offset = this.offset;
        entry.CompressionMethod = compressionMethod;
        this.curMethod = compressionMethod;
        this.sizePatchPos = -1L;
        if ((this.useZip64_ == UseZip64.On) || ((entry.Size < 0L) && (this.useZip64_ == UseZip64.Dynamic)))
        {
          entry.ForceZip64();
        }
        this.WriteLeInt(0x4034b50);
        this.WriteLeShort(entry.Version);
        this.WriteLeShort(entry.Flags);
        this.WriteLeShort((byte) entry.CompressionMethodForHeader);
        this.WriteLeInt((int) entry.DosTime);
        if (hasCrc)
        {
          this.WriteLeInt((int) entry.Crc);
          if (entry.LocalHeaderRequiresZip64)
          {
            this.WriteLeInt(-1);
            this.WriteLeInt(-1);
          }
          else
          {
            this.WriteLeInt(entry.IsCrypted ? (((int) entry.CompressedSize) + 12) : ((int) entry.CompressedSize));
            this.WriteLeInt((int) entry.Size);
          }
        }
        else
        {
          if (this.patchEntryHeader)
          {
            this.crcPatchPos = base.baseOutputStream_.Position;
          }
          this.WriteLeInt(0);
          if (this.patchEntryHeader)
          {
            this.sizePatchPos = base.baseOutputStream_.Position;
          }
          if (entry.LocalHeaderRequiresZip64 || this.patchEntryHeader)
          {
            this.WriteLeInt(-1);
            this.WriteLeInt(-1);
          }
          else
          {
            this.WriteLeInt(0);
            this.WriteLeInt(0);
          }
        }
        byte[] buffer = ZipConstants.ConvertToArray(entry.Flags, entry.Name);
        if (buffer.Length > 0xffff)
        {
          throw new ZipException("Entry name too long.");
        }
        ZipExtraData extraData = new ZipExtraData(entry.ExtraData);
        if (entry.LocalHeaderRequiresZip64)
        {
          extraData.StartNewEntry();
          if (hasCrc)
          {
            extraData.AddLeLong(entry.Size);
            extraData.AddLeLong(entry.CompressedSize);
          }
          else
          {
            extraData.AddLeLong(-1L);
            extraData.AddLeLong(-1L);
          }
          extraData.AddNewEntry(1);
          if (!extraData.Find(1))
          {
            throw new ZipException("Internal error cant find extra data");
          }
          if (this.patchEntryHeader)
          {
            this.sizePatchPos = extraData.CurrentReadIndex;
          }
        }
        else
        {
          extraData.Delete(1);
        }
        if (entry.AESKeySize > 0)
        {
          AddExtraDataAES(entry, extraData);
        }
        byte[] entryData = extraData.GetEntryData();
        this.WriteLeShort(buffer.Length);
        this.WriteLeShort(entryData.Length);
        if (buffer.Length > 0)
        {
          base.baseOutputStream_.Write(buffer, 0, buffer.Length);
        }
        if (entry.LocalHeaderRequiresZip64 && this.patchEntryHeader)
        {
          this.sizePatchPos += base.baseOutputStream_.Position;
        }
        if (entryData.Length > 0)
        {
          base.baseOutputStream_.Write(entryData, 0, entryData.Length);
        }
        this.offset += (30 + buffer.Length) + entryData.Length;
        if (entry.AESKeySize > 0)
        {
          this.offset += entry.AESOverheadSize;
        }
        this.curEntry = entry;
        this.crc.Reset();
        if (compressionMethod == CompressionMethod.Deflated)
        {
          base.deflater_.Reset();
          base.deflater_.SetLevel(defaultCompressionLevel);
        }
        this.size = 0L;
        if (entry.IsCrypted)
        {
          if (entry.AESKeySize > 0)
          {
            this.WriteAESHeader(entry);
          }
          else if (entry.Crc < 0L)
          {
            this.WriteEncryptionHeader(entry.DosTime << 0x10);
          }
          else
          {
            this.WriteEncryptionHeader(entry.Crc);
          }
        }
      }

      2.ZipOutputStream類Finish():

      public override void Finish()
      {
        if (this.entries != null)
        {
          if (this.curEntry != null)
          {
            this.CloseEntry();
          }
          long count = this.entries.Count;
          long sizeEntries = 0L;
          foreach (ZipEntry entry in this.entries)
          {
            this.WriteLeInt(0x2014b50);
            this.WriteLeShort(0x33);
            this.WriteLeShort(entry.Version);
            this.WriteLeShort(entry.Flags);
            this.WriteLeShort((short) entry.CompressionMethodForHeader);
            this.WriteLeInt((int) entry.DosTime);
            this.WriteLeInt((int) entry.Crc);
            if (entry.IsZip64Forced() || (entry.CompressedSize >= 0xffffffffL))
            {
              this.WriteLeInt(-1);
            }
            else
            {
              this.WriteLeInt((int) entry.CompressedSize);
            }
            if (entry.IsZip64Forced() || (entry.Size >= 0xffffffffL))
            {
              this.WriteLeInt(-1);
            }
            else
            {
              this.WriteLeInt((int) entry.Size);
            }
            byte[] buffer = ZipConstants.ConvertToArray(entry.Flags, entry.Name);
            if (buffer.Length > 0xffff)
            {
              throw new ZipException("Name too long.");
            }
            ZipExtraData extraData = new ZipExtraData(entry.ExtraData);
            if (entry.CentralHeaderRequiresZip64)
            {
              extraData.StartNewEntry();
              if (entry.IsZip64Forced() || (entry.Size >= 0xffffffffL))
              {
                extraData.AddLeLong(entry.Size);
              }
              if (entry.IsZip64Forced() || (entry.CompressedSize >= 0xffffffffL))
              {
                extraData.AddLeLong(entry.CompressedSize);
              }
              if (entry.Offset >= 0xffffffffL)
              {
                extraData.AddLeLong(entry.Offset);
              }
              extraData.AddNewEntry(1);
            }
            else
            {
              extraData.Delete(1);
            }
            if (entry.AESKeySize > 0)
            {
              AddExtraDataAES(entry, extraData);
            }
            byte[] entryData = extraData.GetEntryData();
            byte[] buffer3 = (entry.Comment != null) ? ZipConstants.ConvertToArray(entry.Flags, entry.Comment) : new byte[0];
            if (buffer3.Length > 0xffff)
            {
              throw new ZipException("Comment too long.");
            }
            this.WriteLeShort(buffer.Length);
            this.WriteLeShort(entryData.Length);
            this.WriteLeShort(buffer3.Length);
            this.WriteLeShort(0);
            this.WriteLeShort(0);
            if (entry.ExternalFileAttributes != -1)
            {
              this.WriteLeInt(entry.ExternalFileAttributes);
            }
            else if (entry.IsDirectory)
            {
              this.WriteLeInt(0x10);
            }
            else
            {
              this.WriteLeInt(0);
            }
            if (entry.Offset >= 0xffffffffL)
            {
              this.WriteLeInt(-1);
            }
            else
            {
              this.WriteLeInt((int) entry.Offset);
            }
            if (buffer.Length > 0)
            {
              base.baseOutputStream_.Write(buffer, 0, buffer.Length);
            }
            if (entryData.Length > 0)
            {
              base.baseOutputStream_.Write(entryData, 0, entryData.Length);
            }
            if (buffer3.Length > 0)
            {
              base.baseOutputStream_.Write(buffer3, 0, buffer3.Length);
            }
            sizeEntries += ((0x2e + buffer.Length) + entryData.Length) + buffer3.Length;
          }
          using (ZipHelperStream stream = new ZipHelperStream(base.baseOutputStream_))
          {
            stream.WriteEndOfCentralDirectory(count, sizeEntries, this.offset, this.zipComment);
          }
          this.entries = null;
        }
      }

       3.ZipEntry類Clone():

      public object Clone()
      {
        ZipEntry entry = (ZipEntry) base.MemberwiseClone();
        if (this.extra != null)
        {
          entry.extra = new byte[this.extra.Length];
          Array.Copy(this.extra, 0, entry.extra, 0, this.extra.Length);
        }
        return entry;
      }

       4.ZipOutputStream類Write():

      public override void Write(byte[] buffer, int offset, int count)
      {
        if (this.curEntry == null)
        {
          throw new InvalidOperationException("No open entry.");
        }
        if (buffer == null)
        {
          throw new ArgumentNullException("buffer");
        }
        if (offset < 0)
        {
          throw new ArgumentOutOfRangeException("offset", "Cannot be negative");
        }
        if (count < 0)
        {
          throw new ArgumentOutOfRangeException("count", "Cannot be negative");
        }
        if ((buffer.Length - offset) < count)
        {
          throw new ArgumentException("Invalid offset/count combination");
        }
        this.crc.Update(buffer, offset, count);
        this.size += count;
        switch (this.curMethod)
        {
          case CompressionMethod.Stored:
            if (base.Password != null)
            {
              this.CopyAndEncrypt(buffer, offset, count);
            }
            else
            {
              base.baseOutputStream_.Write(buffer, offset, count);
            }
            break;
      
          case CompressionMethod.Deflated:
            base.Write(buffer, offset, count);
            break;
        }
      }

      三.SharpZipLib實(shí)例:

        1.壓縮單個(gè)文件:

         /// 
          /// 壓縮單個(gè)文件
          /// 
          /// 要壓縮的文件
          /// 壓縮后的文件
          /// 壓縮等級(jí)
          /// 每次寫入大小
          public static void ZipFile(string fileToZip, string zipedFile, int compressionLevel, int blockSize)
          {
            if (string.IsNullOrEmpty(fileToZip))
            {
              throw new ArgumentNullException(fileToZip);
            }
            if (string.IsNullOrEmpty(zipedFile))
            {
              throw new ArgumentNullException(zipedFile);
            }
            if (!File.Exists(fileToZip))
            {
              throw new FileNotFoundException("指定要壓縮的文件: " + fileToZip + " 不存在!");
            }
            try
            {
              using (var zipFile = File.Create(zipedFile))
              {
                using (var zipStream = new ZipOutputStream(zipFile))
                {
                  using (var streamToZip = new FileStream(fileToZip, FileMode.Open, FileAccess.Read))
                  {
                    var fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\", StringComparison.Ordinal) + 1);
                    var zipEntry = new ZipEntry(fileName);
                    zipStream.PutNextEntry(zipEntry);
                    zipStream.SetLevel(compressionLevel);
                    var buffer = new byte[blockSize];
                    try
                    {
                      int sizeRead;
                      do
                      {
                        sizeRead = streamToZip.Read(buffer, 0, buffer.Length);
                        zipStream.Write(buffer, 0, sizeRead);
                      }
                      while (sizeRead > 0);
                    }
                    catch (Exception ex)
                    {
                      throw new Exception(ex.Message);
                    }
                    streamToZip.Close();
                  }
                  zipStream.Finish();
                  zipStream.Close();
                }
                zipFile.Close();
              }
            }
            catch (IOException ioex)
            {
              throw new IOException(ioex.Message);
            }
            catch (Exception ex)
            {
              throw new Exception(ex.Message);
            }
      
          }

       2. 壓縮單個(gè)文件:

      /// 
          /// 壓縮單個(gè)文件
          /// 
          /// 要進(jìn)行壓縮的文件名
          /// 壓縮后生成的壓縮文件名
          public static void ZipFile(string fileToZip, string zipedFile)
          {
            if (string.IsNullOrEmpty(fileToZip))
            {
              throw new ArgumentException(fileToZip);
            }
            if (string.IsNullOrEmpty(zipedFile))
            {
              throw new ArgumentException(zipedFile);
            }
            if (!File.Exists(fileToZip))
            {
              throw new FileNotFoundException("指定要壓縮的文件: " + fileToZip + " 不存在!");
            }
            try
            {
              using (var fs = File.OpenRead(fileToZip))
              {
                var buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                fs.Close();
                using (var zipFile = File.Create(zipedFile))
                {
                  using (var zipStream = new ZipOutputStream(zipFile))
                  {
                    var fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\", StringComparison.Ordinal) + 1);
                    var zipEntry = new ZipEntry(fileName);
                    zipStream.PutNextEntry(zipEntry);
                    zipStream.SetLevel(5);
                    zipStream.Write(buffer, 0, buffer.Length);
                    zipStream.Finish();
                    zipStream.Close();
                  }
                }
              }
            }
            catch (IOException ioex)
            {
              throw new IOException(ioex.Message);
            }
            catch (Exception ex)
            {
              throw new Exception(ex.Message);
            }
      
          }

      3.壓縮多層目錄:

         /// 
          /// 壓縮多層目錄
          /// 
          /// 目錄
          /// 壓縮文件
          public static void ZipFileDirectory(string strDirectory, string zipedFile)
          {
            if (string.IsNullOrEmpty(strDirectory))
            {
              throw new ArgumentException(strDirectory);
            }
            if (string.IsNullOrEmpty(zipedFile))
            {
              throw new ArgumentException(zipedFile);
            }
            using (var zipFile = File.Create(zipedFile))
            {
              using (var s = new ZipOutputStream(zipFile))
              {
                ZipSetp(strDirectory, s, "");
              }
            }
          }

      4.遞歸遍歷目錄:

      /// 
          /// 遞歸遍歷目錄
          /// 
          /// 目錄
          /// ZipOutputStream對(duì)象
          /// 父路徑
          private static void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath)
          {
            if (strDirectory[strDirectory.Length - 1] != Path.DirectorySeparatorChar)
            {
              strDirectory += Path.DirectorySeparatorChar;
            }
            var crc = new Crc32();
      
            var filenames = Directory.GetFileSystemEntries(strDirectory);
            try
            {
              // 遍歷所有的文件和目錄
              foreach (var file in filenames)
              {
                // 先當(dāng)作目錄處理如果存在這個(gè)目錄就遞歸Copy該目錄下面的文件
                if (Directory.Exists(file))
                {
                  var pPath = parentPath;
                  pPath += file.Substring(file.LastIndexOf("\\", StringComparison.Ordinal) + 1);
                  pPath += "\\";
                  ZipSetp(file, s, pPath);
                }
                // 否則直接壓縮文件
                else
                {
                  //打開壓縮文件
                  using (var fs = File.OpenRead(file))
                  {
                    var buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);
                    var fileName = parentPath + file.Substring(file.LastIndexOf("\\", StringComparison.Ordinal) + 1);
                    var entry = new ZipEntry(fileName)
                    {
                      DateTime = DateTime.Now,
                      Size = fs.Length
                    };
                    fs.Close();
                    crc.Reset();
                    crc.Update(buffer);
                    entry.Crc = crc.Value;
                    s.PutNextEntry(entry);
                    s.Write(buffer, 0, buffer.Length);
                  }
                }
              }
            }
            catch (IOException ioex)
            {
              throw new IOException(ioex.Message);
            }
            catch (Exception ex)
            {
              throw new Exception(ex.Message);
            }
      
          }

      5.解壓縮一個(gè) zip 文件:

      /// 
          /// 解壓縮一個(gè) zip 文件。
          /// 
          /// The ziped file.
          /// The STR directory.
          /// zip 文件的密碼。
          /// 是否覆蓋已存在的文件。
          public void UnZip(string zipedFile, string strDirectory, string password, bool overWrite)
          {
            if (string.IsNullOrEmpty(zipedFile))
            {
              throw new ArgumentException(zipedFile);
            }
            if (string.IsNullOrEmpty(strDirectory))
            {
              throw new ArgumentException(strDirectory);
            }
            if (string.IsNullOrEmpty(password))
            {
              throw new ArgumentException(password);
            }
            if (strDirectory == "")
            {
              strDirectory = Directory.GetCurrentDirectory();
            }
            if (!strDirectory.EndsWith("\\"))
            {
              strDirectory = strDirectory + "\\";
            }
            try
            {
              using (var s = new ZipInputStream(File.OpenRead(zipedFile)))
              {
                s.Password = password;
                ZipEntry theEntry;
                while ((theEntry = s.GetNextEntry()) != null)
                {
                  var directoryName = string.Empty;
                  var pathToZip = theEntry.Name;
                  if (pathToZip != "")
                  {
                    directoryName = Path.GetDirectoryName(pathToZip) + "\\";
                  }
                  var fileName = Path.GetFileName(pathToZip);
                  Directory.CreateDirectory(strDirectory + directoryName);
                  if (fileName == "") continue;
                  if ((!File.Exists(strDirectory + directoryName + fileName) || !overWrite) &&
                    (File.Exists(strDirectory + directoryName + fileName))) continue;
                  using (var streamWriter = File.Create(strDirectory + directoryName + fileName))
                  {
                    var data = new byte[2048];
                    while (true)
                    {
                      var size = s.Read(data, 0, data.Length);
      
                      if (size > 0)
                        streamWriter.Write(data, 0, size);
                      else
                        break;
                    }
                    streamWriter.Close();
                  }
                }
      
                s.Close();
              }
            }
            catch (IOException ioex)
            {
              throw new IOException(ioex.Message);
            }
            catch (Exception ex)
            {
              throw new Exception(ex.Message);
            }
      
          }

      以上是對(duì)SharpZipLib組件的相關(guān)介紹,本文的講解上比較的淺顯,如果需要深入的學(xué)習(xí)可以進(jìn)入官網(wǎng)進(jìn)行詳細(xì)的學(xué)習(xí)。組件的功能是很強(qiáng)大的,如何在項(xiàng)目中使用組件,完成我們?cè)陧?xiàng)目中需要實(shí)現(xiàn)的功能,這就是對(duì)每個(gè)開發(fā)者提出了要求,需要我們仔細(xì)的去考慮。

      以上就是.NET多類型文件解壓縮組件SharpZipLib是怎樣的,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。


      分享標(biāo)題:.NET多類型文件解壓縮組件SharpZipLib是怎樣的-創(chuàng)新互聯(lián)
      當(dāng)前路徑:http://www.ef60e0e.cn/article/spggp.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        石泉县| 商洛市| 舞阳县| 南昌市| 清苑县| 纳雍县| 布拖县| 二手房| 苏州市| 赣州市| 隆德县| 台湾省| 龙胜| 普安县| 敦化市| 子长县| 金坛市| 关岭| 莎车县| 东兴市| 五峰| 措勤县| 罗山县| 斗六市| 绥中县| 和田市| 怀安县| 富阳市| 南丹县| 崇州市| 吉隆县| 浠水县| 长岛县| 大关县| 炉霍县| 富蕴县| 榆社县| 山丹县| 绥中县| 当涂县| 怀集县|