Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / C#

Programmatically Compress and Decompress Files

Rate me:
Please Sign up or sign in to vote.
4.80/5 (8 votes)
18 Mar 2010CPL3 min read 67.9K   23   7
Learn how to compress and decompress files programmatically using C#

This lesson is very easy. This lesson focuses on how to compress and decompress files programmatically using .NET Framework and C# -or any language that supports .NET of course.-

Currently .NET Framework supports two types of compression algorithms:

  • Deflate
    This is a very simple algorithm, used for simple compression and decompression operations. Don't use this algorithm except if you intend to use compressed data only in your application because this algorithm is not compatible with any compression tool.
  • GZIP
    This algorithm uses internally the Deflate algorithm. This algorithm includes a cyclic redundancy check value for detecting data corruption. Also it’s compatible with most compression tools because it writes headers to the compressed file, so compression tools -like WinZip and WinRAR- can easily access the compressed file and decompress it as well. This algorithm also can be extended to use other algorithms internally other than Deflate.

For a complete GZIP reference, see RFC 1952 (GZIP File Format Specification).

The most powerful compression tool now is WinRAR.

Fortunately, whether using Deflate or GZIP in .NET, code is the same; the only thing that needs to change is the class name.

Deflate and GZIP can be found in the namespace System.IO.Compression that resides on assembly System.dll. This namespace contains only three types, the two algorithm implementation classes DeflateStream and GZipStream -both inherit directly from System.IO.Stream class-, and an enumeration CompressionMode that defines the operation (compression or decompression).

Compressing a File

The code for compression is very simple:

Code snippets in this lesson rely on the fact that you added two using (Imports in VB) statements, System.IO and System.IO.Compression.

C#
string fileToBeCompressed =
    "D:My Great Word Document.doc";
string zipFilename =
    "D:CompressedGreatDocument.zip";

using (FileStream target =
    new FileStream(zipFilename,
        FileMode.Create, FileAccess.Write))
using (GZipStream alg =
    new GZipStream(target, CompressionMode.Compress))
{
    byte[] data = File.ReadAllBytes(fileToBeCompressed);
    alg.Write(data, 0, data.Length);
    alg.Flush();
}

Code Explanation

If you are going to compress a file, then you must specify the CompressionMode.Compress option, and also you must specify a Stream that the data will be written to.
After creating the class, you can compress the data by using its Write() method.

If you intended to use the Deflate algorithm, just change the class name to DeflateStream.

Decompressing a File

The code that decompresses a compressed file is very similar:

C#
string compressedFile =
    "D:CompressedGreatDocument.zip";
string originalFileName =
    "D:My Great Word Document.doc";

using (FileStream zipFile =
    new FileStream(compressedFile,
        FileMode.Open, FileAccess.Read))
using (FileStream originalFile =
    new FileStream(originalFileName,
        FileMode.Create, FileAccess.Write))
using (GZipStream alg =
    new GZipStream(zipFile, CompressionMode.Decompress))
{
    while (true)
    {
        // Reading 100bytes by 100bytes
        byte[] buffer = new byte[100];
        // The Read() method returns the number of bytes read
        int bytesRead = alg.Read(buffer, 0, buffer.Length);

        originalFile.Write(buffer, 0, returnedBytes);

        if (bytesRead != buffer.Length)
            break;
    }
}

Code Explanation

First, we create a file stream to read the ZIP file, and then we created our algorithm class that encapsulates the file stream for decompressing it.
Then we created a file stream for the original file, for writing the decompressed data.
After that, we read the data compressed 100bytes after each other -you may prefer more- and write this data to the original file stream.

By encapsulating disposable objects into using statements, you become assured that every object will be disposed in a certain point -end of the using statement- and no object will retain in memory.

Try It Yourself

Develop a simple application that can be used to compress and decompress files.

This application can compress several files into one file and decompress them as well.

Besides algorithms classes, you may use System.IO.BinaryWriter and System.IO.BinaryReader to get full control of how the file contents will be.

Posted in File System Tagged: .NET, CodeProject, CSharp, File System

License

This article, along with any associated source code and files, is licensed under The Common Public License Version 1.0 (CPL)


Written By
Technical Lead
Egypt Egypt
Mohammad Elsheimy is a developer, trainer, and technical writer currently hired by one of the leading fintech companies in Middle East, as a technical lead.

Mohammad is a MCP, MCTS, MCPD, MCSA, MCSE, and MCT expertized in Microsoft technologies, data management, analytics, Azure and DevOps solutions. He is also a Project Management Professional (PMP) and a Quranic Readings college (Al-Azhar) graduate specialized in Quranic readings, Islamic legislation, and the Arabic language.

Mohammad was born in Egypt. He loves his machine and his code more than anything else!

Currently, Mohammad runs two blogs: "Just Like [a] Magic" (http://JustLikeAMagic.com) and "مع الدوت نت" (http://WithdDotNet.net), both dedicated for programming and Microsoft technologies.

You can reach Mohammad at elsheimy[at]live[dot]com

Comments and Discussions

 
GeneralMy vote of 5 Pin
Wael Galal El Deen17-Mar-17 2:17
Wael Galal El Deen17-Mar-17 2:17 
Very usefull
QuestionI Try This project Pin
Member 1268432422-Sep-16 18:49
Member 1268432422-Sep-16 18:49 
GeneralGood Article Pin
aarif moh shaikh27-Sep-15 19:48
professionalaarif moh shaikh27-Sep-15 19:48 
SuggestionUse full io.stream instead of byte array Pin
Frédéric Malenfant3-Sep-14 8:26
Frédéric Malenfant3-Sep-14 8:26 
GeneralRe: Use full io.stream instead of byte array Pin
Wael Galal El Deen17-Mar-17 2:08
Wael Galal El Deen17-Mar-17 2:08 
QuestionCompressed File Pin
NandyX23-Nov-13 13:34
NandyX23-Nov-13 13:34 
Questionzip vs rar Pin
kirchoff_0d0a20-Dec-11 6:52
kirchoff_0d0a20-Dec-11 6:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.