We Recommend

Accelerated C# 2008 Accelerated C# 2008
This book is both a rapid tutorial and a permanent reference. You’ll quickly master C# syntax while learning how the CLR simplifies many programming tasks. You’ll also learn best practices that ensure your code will be efficient, reusable, and robust. Why spend months or years discovering the best ways to design and code C# when this book will show you how to do things the right way, right from the start?


Posted By

rtipton on 05/15/09


Tagged

GZipStream


Versions (?)


Who likes this?

2 people have marked this snippet as a favorite

paelgr
umang_nine


GZipStream Class


Published in: C# 


  1. using System;
  2. using System.IO;
  3. using System.IO.Compression;
  4.  
  5. public class GZipTest
  6. {
  7. private static void usage()
  8. {
  9. Console.WriteLine("My test GZip program");
  10. Console.WriteLine("gziptest [filename to be compressed]");
  11. }
  12.  
  13. static void Main(string[] args)
  14. {
  15. if (args.Length < 1)
  16. {
  17. usage();
  18. return;
  19. }
  20. else
  21. {
  22. string inputFile = args[0];
  23. string outputFile = inputFile + ".gz";
  24.  
  25. try
  26. {
  27. // Get bytes from input stream
  28. FileStream inFileStream = new FileStream(Path.Combine(Environment.CurrentDirectory, inputFile), FileMode.Open);
  29. byte[] buffer = new byte[inFileStream.Length];
  30. inFileStream.Read(buffer, 0, buffer.Length);
  31. inFileStream.Close();
  32.  
  33. // Create GZip file stream and compress input bytes
  34. FileStream outFileStream = new FileStream(Path.Combine(Environment.CurrentDirectory, outputFile), FileMode.Create);
  35. GZipStream compressedStream = new GZipStream(outFileStream, CompressionMode.Compress);
  36. compressedStream.Write(buffer, 0, buffer.Length);
  37. compressedStream.Close();
  38. outFileStream.Close();
  39.  
  40. Console.WriteLine("The file has been compressed. UR Da Bomb!!!");
  41. }
  42. catch(FileNotFoundException)
  43. {
  44. Console.WriteLine("Error: Specified file cannot be found.");
  45. }
  46. }
  47. }
  48. }

Report this snippet 

You need to login to post a comment.