C#完毕解压GZip文件的格局

发布时间:2019-05-17  栏目:MySQL  评论:0 Comments

public void ungzip(string path, string decomPath, bool overwrite)
{
  //for overwriting purposes
  if (File.Exists(decomPath))
  {
 if (overwrite)
 {
   File.Delete(decomPath);
 }
 else
 {
   throw new IOException("The decompressed path you specified already exists and cannot be overwritten.");
 }
  }
  //create our file streams
  GZipStream stream = new GZipStream(new FileStream(path, FileMode.Open, FileAccess.ReadWrite), CompressionMode.Decompress);
  FileStream decompressedFile = new FileStream(decomPath, FileMode.OpenOrCreate, FileAccess.Write);
  //data represents a byte from the compressed file
  //it's set through each iteration of the while loop
  int data;
  while ((data = stream.ReadByte()) != -1) //iterates over the data of the compressed file and writes the decompressed data
  {
 decompressedFile.WriteByte((byte)data);
  }
  //close our file streams 
  decompressedFile.Close();
  stream.Close();
}

意在本文所述对我们C#次第设计有所援助。

期望本文所述对大家的C#先后设计有着协理。

你大概感兴趣的篇章:

本文实例讲述了C#落到实处解压GZip文件的点子。分享给大家供大家参照他事他说加以考察。具体落到实处格局如下:

愈来愈多关于C#有关内容感兴趣的读者可查阅本站专项论题:《C#分布控件用法教程》、《WinForm控件用法总结》、《C#数据结构与算法教程》、《C#面向对象程序设计入门教程》及《C#先后设计之线程使用能力总计

您或者感兴趣的稿子:

本文实例讲述了C#落到实处公文减弱与解压的情势。分享给大家供大家参照他事他说加以调查,具体如下:

/*
 * Gary Zhang -- cbcye@live.com
 * www.cbcye.com
 * www.quicklearn.cn
 * cbcye.cnblogs.com
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using System.Diagnostics;
using ICSharpCode.SharpZipLib.Core;
namespace TestConsole
{
  class Program
  {
    static void Main()
    {
      //CreateZipFile(@"d:\", @"d:\a.zip");
      UnZipFile(@"d:\a.zip");
      Console.Read();
    }
    private static void CreateZipFile(string filesPath, string zipFilePath)
    {
      if (!Directory.Exists(filesPath))
      {
        Console.WriteLine("Cannot find directory '{0}'", filesPath);
        return;
      }
      try
      {
        string[] filenames = Directory.GetFiles(filesPath);
        using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath)))
        {
          s.SetLevel(9); // 压缩级别 0-9
          //s.Password = "123"; //Zip压缩文件密码
          byte[] buffer = new byte[4096]; //缓冲区大小
          foreach (string file in filenames)
          {
            ZipEntry entry = new ZipEntry(Path.GetFileName(file));
            entry.DateTime = DateTime.Now;
            s.PutNextEntry(entry);
            using (FileStream fs = File.OpenRead(file))
            {
              int sourceBytes;
              do
              {
                sourceBytes = fs.Read(buffer, 0, buffer.Length);
                s.Write(buffer, 0, sourceBytes);
              } while (sourceBytes > 0);
            }
          }
          s.Finish();
          s.Close();
        }
      }
      catch (Exception ex)
      {
        Console.WriteLine("Exception during processing {0}", ex);
      }
    }
    private static void UnZipFile( string zipFilePath)
    {
      if (!File.Exists(zipFilePath))
      {
        Console.WriteLine("Cannot find file '{0}'", zipFilePath);
        return;
      }
      using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
      {
        ZipEntry theEntry;
        while ((theEntry = s.GetNextEntry()) != null)
        {
          Console.WriteLine(theEntry.Name);
          string directoryName = Path.GetDirectoryName(theEntry.Name);
          string fileName = Path.GetFileName(theEntry.Name);
          // create directory
          if (directoryName.Length > 0)
          {
            Directory.CreateDirectory(directoryName);
          }
          if (fileName != String.Empty)
          {
            using (FileStream streamWriter = File.Create(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;
                }
              }
            }
          }
        }
      }
    }
  }
}

组件的采取相比较简单,请参谋上面包车型大巴代码。点击下载系列源码.rar)。

在店堂开荒进程中不经常会超过文件的回落与解压,即使网络海人民广播广播台湾大学流行的压缩文件格式都以RA宝马X3的,然而出于RA福特Explorer不是1个绽放的正经,因而ZIP成了更四人的取舍。假若您不想本人费用的话能够选拔开源的花色,举例SharpZipLib便是三个没有错的选用。

留下评论

网站地图xml地图