欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现解压GZip文件的方法

程序员文章站 2022-07-01 17:25:40
本文实例讲述了c#实现解压gzip文件的方法。分享给大家供大家参考。具体实现方法如下: public void ungzip(string path, strin...

本文实例讲述了c#实现解压gzip文件的方法。分享给大家供大家参考。具体实现方法如下:

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#程序设计有所帮助。