Please navigate to the bottom of the page for Table of Contents

Tuesday, May 31, 2011

Explain System.IO and System.IO.Compression namespaces with an example

A through understanding of the System.IO namespace is very important when you are developing applications that deal with the file system. In addition, if the files that you are dealing with are large in size, you should probably be very comfortable in System.IO and System.IO.Compression namespaces. A good deal of interview questions come from this area.

Let’s tackle relatively simple problem: Write a simple function that compresses a source file and writes it out a compressed zip archive and vice-versa. The solution is simple: We can create 2 streams: one to read the file and the second to write to a file. Then depending on whether we are compressing or decompressing, we can use a GZipStream class to wrap the read or write streams. The complete program below shows a function that allows you to compress/decompress a file.

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

namespace ConsoleApplication1
{
class FileExamples
{
public static void Compress(string inFile, string outFile, bool compress)
{
// error checking
if (!File.Exists(inFile))
throw new ArgumentException("inFile does not exist");
if (File.Exists(outFile))
throw new ArgumentException("outFile exists");

// now with pleasantaries out, let's create our streams
// input stream
using (Stream inStream = File.Open(
inFile, // the file to open
FileMode.Open, // try to open an existing file
FileAccess.Read, // read only access
FileShare.None)) // lock the file till we are done
{
// output stream
using (Stream outStream = File.Open(
outFile, // the file to write to
FileMode.Create, // create.overwrite
FileAccess.Write, // write access please
FileShare.None)) // lock it till we are done
{
// now that we have opened both in/out streams
// let's wrap either one of them based on
// compression or decompression
using (GZipStream gzipStream = new GZipStream(
// select the correct stream and compression type
compress ? outStream : inStream,
compress ? CompressionMode.Compress :
CompressionMode.Decompress))
{
// now we have wrapped the correct stream
// for reading; do the same for writing
Stream readFrom = compress ? inStream : gzipStream;
Stream writeTo = compress ? gzipStream : outStream;

// since we are reading using base Stream class
// we will have to read using byte array
// we could have also wrapped this is a higher
// stream (Buffered, etc, but that would
// complicate this example further

// we will use a buffer size of 16384
// as that maximizes the performance
byte[] buffer = new byte[16384];
int byteCount = 0;

// move data from in to out
do
{
// read
byteCount = readFrom.Read(buffer, 0, 16384);
// write
writeTo.Write(buffer, 0, byteCount);
} while (byteCount > 0);
}
}
}
}
}
}


There are some obvious improvements that you can do to this program to make it more efficient. I would like you to propose some changes to make this more robust. Please do refer to the MSDN documentation on the System.IO namespace and File and Stream IO section to read in more details about this powerful library.

12 comments:

  1. This information is impressive..I am inspired with your post writing style & how continuously you describe this topic.

    Apache Spark Training in Pune
    Spark Training Institute in Pune

    ReplyDelete
  2. This is one of the most important blogs that I have seen, keep it up!
    San Francisco graphic design studio

    ReplyDelete
  3. Once again thanks for sharing this amazing article with us. I love this type of good information. Keep it up. Now it's time to avail snow white soap for more information.

    ReplyDelete
  4. Fly Ash powder Louisiana is revolutionizing construction, providing a sustainable solution for soil stabilization and foundation strength. Hasten Chemical leads the way, offering high-quality Fly Ash products tailored to meet the diverse needs of the state's construction projects.

    ReplyDelete
  5. Indulge in the rich and decadent world of chocolates in Dammam, where a delightful array of premium confections awaits to satisfy every sweet craving. Explore the city's sweet treasures and treat yourself to the irresistible flavors of chocolate perfection.

    ReplyDelete
  6. Exploring the pivotal role of fit-out companies in harmonizing commercial space renovations with designers. For a perfect fusion of function and style in Dubai, consider partnering with top interior fit out Dubai to bring your commercial vision to life.

    ReplyDelete
  7. Fit-out contractors in Dubai redefine commercial spaces, seamlessly blending aesthetics and functionality. Elevate your projects with the expertise of these top-tier fit-out contractors in Dubai.

    ReplyDelete
  8. "Mastering the intricacies of System.IO and System.IO.Compression namespaces is crucial for robust file system management in application development. Just as precision is key in coding, our Oil and gas catering services in Houston Texas, deliver unparalleled excellence in satisfying appetites for success at every energy sector event."

    ReplyDelete
  9. For top-notch wood wood fencing contractor in Edmonton, trust our skilled contractors to enhance your property with quality craftsmanship. We specialize in custom designs, durability, and professional installations, ensuring your fence stands the test of time. Choose us for a perfect blend of aesthetic appeal and security.






    ReplyDelete
  10. Discover the potent benefits of epimedium royal performance honey with Monster Rabbit. This natural aphrodisiac enhances vitality and intimacy, providing a boost to your romantic life. Experience the finest quality for an invigorating experience.

    ReplyDelete