C# GZip/Zip Compression/Decompression

Simple zip compression in C#:

private static void ZipCompress()
{
const string srcFile = “DataFile.xml”;
const string desFile = “Compressed.zip”;

FileStream fsIn = null;
FileStream fsOut = null;
GZipStream gzip = null;
byte[] buffer;

try
{
fsOut = new FileStream(desFile, FileMode.Create, FileAccess.Write, FileShare.None);
gzip = new GZipStream(fsOut, CompressionMode.Compress, true);

fsIn = new FileStream(srcFile, FileMode.Open, FileAccess.Read, FileShare.Read);
buffer = new byte[fsIn.Length];
fsIn.Read(buffer, 0, buffer.Length);
fsIn.Close();

gzip.Write(buffer, 0, buffer.Length);
}
catch (Exception ex)
{
System.Diagnostics.Debug.Assert(false, ex.ToString());
}
finally
{
if (gzip != null)
gzip.Close();
if (fsOut != null)
fsOut.Close();
if (fsIn != null)
fsIn.Close();
}
}

Simple zip decompression in C#:

private static void ZipDecompress()
{
const string srcFile = “Compressed.zip”;
const string desFile = “Decompressed.xml”;

FileStream fsIn = null;
FileStream fsOut = null;
GZipStream gzip = null;
const int bufferSize = 4096;
byte[] buffer = new byte[bufferSize];

try
{
fsIn = new FileStream(srcFile, FileMode.Open, FileAccess.Read, FileShare.Read);
fsOut = new FileStream(desFile, FileMode.Create, FileAccess.Write, FileShare.None);
gzip = new GZipStream(fsIn, CompressionMode.Decompress, true);
while (true)
{
int count = gzip.Read(buffer, 0, bufferSize);
if (count != 0)
fsOut.Write(buffer, 0, count);
if (count != bufferSize)
break;
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.Assert(false, ex.ToString());
}
finally
{
if (gzip != null)
gzip.Close();
if (fsOut != null)
fsOut.Close();
if (fsIn != null)
fsIn.Close();
}
}

Published by

Tim Clark

Experienced Business Owner, Chief Information Officer, Vice President, Chief Software Architect, Application Architect, Project Manager, Software Developer, Senior Web Developer, Graphic Designer & 3D Modeler, University Instructor, University Program Chair, Academic Director. Specialties: Ruby, Ruby on Rails, JavaScript, JQuery, AJAX, Node.js, React.js, Angular.js, MySQL, PostgreSQL, MongoDB, SQL Server, Responsive Design, HTML5, XHTML, CSS3, C#, ASP.net, Project Management, System Design/Architecture, Web Design, Web Development, Adobe CS6 (Photoshop, Illustrator)

2 thoughts on “C# GZip/Zip Compression/Decompression

  1. Compress :

    public static byte[] ToByteArray(object o)
    {
    if (o == null)
    return new byte[0];

    using (MemoryStream outStream = new MemoryStream())
    {
    using (GZipStream zipStream = new GZipStream(outStream, CompressionMode.Compress))
    {
    using (MemoryStream stream = new MemoryStream())
    {
    new BinaryFormatter().Serialize(stream, o);
    stream.Position = 0;
    stream.CopyTo(zipStream);
    zipStream.Close();
    return outStream.ToArray();
    }
    }
    }
    }

    De Compress:

    public static object ToObject(byte[] byteArray)
    {
    if (byteArray.Length == 0)
    return null;

    using (MemoryStream decomStream = new MemoryStream(byteArray), ms = new MemoryStream())
    {
    using (GZipStream hgs = new GZipStream(decomStream, CompressionMode.Decompress))
    {
    hgs.CopyTo(ms);
    decomStream.Close();
    hgs.Close();
    ms.Position = 0;
    return new BinaryFormatter().Deserialize(ms);
    }
    }
    }

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s