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.
Great Article
ReplyDeleteIEEE Final Year Projects for CSE
IEEE Project Centers in Chennai
This information is impressive..I am inspired with your post writing style & how continuously you describe this topic.
ReplyDeleteApache Spark Training in Pune
Spark Training Institute in Pune
Mua vé máy bay Aivivu, tham khảo
ReplyDeletesăn vé máy bay giá rẻ đi Mỹ
mua vé máy bay từ mỹ về việt nam
ve may bay tư duc ve viet nam
vé máy bay từ nga về việt nam
Đặt vé máy bay tại Aivivu, tham khảo
ReplyDeletevé máy bay đi Mỹ hạng thương gia
vé mỹ về việt nam
giá vé máy bay từ đức về việt nam
vé máy bay từ nhật về vn
vé máy bay từ hàn quốc về việt nam bao nhiêu tiền
vé máy bay từ Toronto về việt nam
danh sách khách sạn cách ly ở tphcm
chuyến bay dành cho chuyên gia
This is one of the most important blogs that I have seen, keep it up!
ReplyDeleteSan Francisco graphic design studio
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