"Compression and Decompression" is most common word when you transfer your data from one place to another. Sometimes you have large size of data that can not transfer (e.g. mail attachment). You use a software (Zip) to compress your data and able to transfer it. And on other end, you decompress your data and get the original data.
But if you are developing your own application that transfer data from one place to another (FTP) that can take much time to transfer data because of size of data. At that time, when you want to embed zipping method in your application so that you just pass the data and after zipping, data can transfer; Zip software will not be helpful.
I search a lot of material, how to do; I got some code. I modify that code according to my requirement and develop two function; one compress data and other decompress it. You just need to pass your data file with complete path and it will compress the file & decompress too. Even you may use your own extension and embed it in your application.
System.IO.Compression is package and using GZip Compression & decompression method.
Use it and Enjoy!
Compression
public static void Compress(string strPath)
{
DateTime current;
string dstFile = "";// "C:\\temp\\compressed-file.gzip";
FileStream fsIn = null;
FileStream fsOut = null;
GZipStream gzip = null;
byte[] buffer;
int count = 0;
try
{
current = DateTime.Now;
dstFile = Application.StartupPath + "\\" + "Request" + current.Day.ToString() + current.Month.ToString() + current.Year.ToString() + current.Hour.ToString() + current.Minute.ToString() + current.Second.ToString() + ".zip"; // may use own extension
fsOut = new FileStream(dstFile, FileMode.Create, FileAccess.Write, FileShare.None);
gzip = new GZipStream(fsOut, CompressionMode.Compress, true);
fsIn = new FileStream(strPath, FileMode.Open, FileAccess.Read, FileShare.Read);
buffer = new byte[fsIn.Length];
count = fsIn.Read(buffer, 0, buffer.Length);
fsIn.Close();
fsIn = null;
// compress to the destination file
gzip.Write(buffer, 0, buffer.Length);
}
catch (Exception ex)
{
// handle or display the error
System.Diagnostics.Debug.Assert(false, ex.ToString());
}
finally
{
if (gzip != null)
{
gzip.Close();
gzip = null;
}
if (fsOut != null)
{
fsOut.Close();
fsOut = null;
}
if (fsIn != null)
{
fsIn.Close();
fsIn = null;
}
}
}
public static void Decompress(string strPath)
{
DateTime current;
string dstFile = "";
FileStream fsIn = null;
FileStream fsOut = null;
GZipStream gzip = null;
const int bufferSize = 4096;
byte[] buffer = new byte[bufferSize];
int count = 0;
try
{
current = DateTime.Now;
dstFile = Application.StartupPath + "\\" + current.Day.ToString() + current.Month.ToString() + current.Year.ToString() + current.Hour.ToString() + current.Minute.ToString() + current.Second.ToString() + ".txt";
fsIn = new FileStream(strPath, FileMode.Open, FileAccess.Read, FileShare.Read);
fsOut = new FileStream(dstFile, FileMode.Create, FileAccess.Write, FileShare.None);
gzip = new GZipStream(fsIn, CompressionMode.Decompress, true);
while (true)
{
count = gzip.Read(buffer, 0, bufferSize);
if (count != 0)
{
fsOut.Write(buffer, 0, count);
}
if (count != bufferSize)
{
// have reached the end
break;
}
}
}
catch (Exception ex)
{
// handle or display the error
System.Diagnostics.Debug.Assert(false, ex.ToString());
}
finally
{
if (gzip != null)
{
gzip.Close();
gzip = null;
}
if (fsOut != null)
{
fsOut.Close();
fsOut = null;
}
if (fsIn != null)
{
fsIn.Close();
fsIn = null;
}
}
}