"Compression and Decompression" are very common words when transfering data from one place to another. Sometimes you have a large amount of data that cannot be transfered (for example mail attachment). You use software, such as Zip, to compress your data and then it is possible to transfer it. And at the other end, you decompress your data and get the original data back.
But if you are developing your own application that transfers data from one place to another (FTP) then that can take much time to transfer data because of the size of the data. At that time, when you want to embed a zipping method into your application, you just pass the data and after zipping, the data can be transferred; the Zip software will not be helpful.
I searched many materials for an explanation of how to do it; I got some code. I modified that code to satisfy my requirements and developed two functions; one to compress the data and the other to decompress it. You just need to pass your data file with a complete path and it will compress the file and decompress it too. You may even use your own extension and embed it in your application.
System.IO.Compression is the package. Use the GZip compression and decompression methods.
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;
}
}
}
Tuhin PaulPosted Jan 6, 2024, 6:28 PM
Your provided code for compressing and decompressing data using GZipStream is clear and well-documented. This will undoubtedly be a valuable resource for developers working on data transfer applications. I appreciate your effort in modifying the code to suit specific requirements.
Naveen DaniyaPosted Nov 2, 2011, 7:22 AM
i have one .dat file in both format compress and decompress + decompress tool also. i want to know what kind of compression is this and how to compress it again same like decompressed file can anyone help me in this regards here are links http://www.4shared.com/file/PhqxsyC3/dbcivilization_compress_.html http://www.4shared.com/file/uwRprFkN/dbcivilization_Decompress_.html Thanx in Advance
sant deshPosted Feb 4, 2011, 6:50 AM
how to decompress on FTP server from loacal machine using .net
vishank desaiPosted Oct 24, 2010, 1:51 AM
hi i am getting error in this line 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 the error is Error 1 An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Application.get' C:\Documents and Settings\VISHANK\My Documents\Visual Studio 2008\WebSites\WebSite8\fileupload.aspx.cs 47 23 C:\...\WebSite8\ could you tell me the solution
Manoj MittalPosted Jul 6, 2010, 5:25 AM
I am using only compressed section only and for single file in dstFile variable Put dstFile = Path of file with extension+".gz"; <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> Now its working fine</o:p>
Manoj MittalPosted Jul 6, 2010, 5:21 AM
Actuallyi tried the same code. But while opening a compress file, it gives error like can not open file:This is not a valid archive.
Arnab PatraPosted Oct 1, 2009, 8:59 PM
hello, i am very new in .net. i want to know how can i zip and save a file when a user upload the file by file upload tools?pls help . it will be better for me if you ipload a demo project.or mailme at [email protected].
SimonPosted Jun 24, 2007, 6:00 AM
how do I go around handling multiple file compression into one gzip?