Code: Detail

C# - (English)

Compression and Decompression with GZipStream

GzipStream routine using the framework to compress and decompress files

Last update: 14/04/2010
C#       
 
N/A
N/A
N/A
N/A
 


//Declarations

using System.IO;
using System.IO.Compression;



//Compression function


static void CompressFile(string name_file_source, string compression_name_file_destination)
{
 FileStream sourceFile = File.OpenRead(name_file_source);
 FileStream destFile = File.Create(compression_name_file_destination);

 GZipStream compstream = new GZipStream(destFile, CompressionMode.Compress);

 const int buf_size = 8192;
 byte[] buffer = new byte[buf_size];
 int bytes_read = 0;
 do
 {
   bytes_read = sourceFile.Read(buffer, 0, buf_size);
   compstream.Write(buffer, 0, bytes_read);
 } while (bytes_read != 0);

 compstream.Close();
 sourceFile.Close();
 destFile.Close();
}



//Decompression Function

static void DecompressFile(string CompressFilename, string DecompressFileName)
{
 FileStream sourceFile = File.OpenRead(CompressFilename);
 FileStream destFile = File.Create(DecompressFileName);

 GZipStream compstream = new GZipStream(sourceFile, CompressionMode.Decompress);


 int theByte = compstream.ReadByte();
 while (theByte != -1)
 {
  destFile.WriteByte((byte)theByte);
  theByte = compstream.ReadByte();
 }

 compstream.Close();
 sourceFile.Close();
 destFile.Close();

}

/*

Using:

  CompressFile(@"c:\file.exe", @"c:\file.gz");

  DecompressFile(@"c:\file.gz", @"c:\file.exe");

*/

Source:
 
Users who have marked this routine as a favorite
 
 
 
The site ti4fun is not responsible for the content on sites for which you have external links

Articles, routines, tips, forums, blogs or any other content posted on ti4fun site is not tested and not validated, so you should test and validate any information collected on the ti4fun site before applying it to final use environment, such as example, production. the TI4FUN site is not responsible for quality or for any damages, direct, indirect or consequential, from use of any content posted by the authors in the site.

All content published on the ti4fun site is the responsibility of the author and do not necessarily express the views of the site ti4fun and its employees.