+ Reply to Thread
Results 1 to 6 of 6

Thread: Tutorial: Visual Studio 2008 C# Compressing

  1. #1
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Tutorial: Visual Studio 2008 C# Compressing

    COMPRESSING AND DECOMPRESSING FILES WITH C#


    1. INTRODUCTION


    In this tutorial our subject will be the compression and decompression of files. With the .Net environment you can compress and decompress files by using the open-source compression format GZIP. Besides that, you can also compress files with the DEFLATE algorithm. The two methods are identical in the compression process but GZIP also adds some extra information which results in slightly larger files sizes. If you want to archive your data only through .Net applications you can use the DEFLATE stream class. Otherwise, you should prefer the GZip stream class for compatibility with the popular open source Gzip application.


    2. GRAPHICAL USER INTERFACE

    For this tutorial the graphical user interface doesn’t provide us with much useful functions, so you can create a console-based project but for simplicity we will create a typical form with a button and an openFileDialog so as to choose which file you want to compress. You can also try importing a saveFileDialog so as to be able to choose the destination of the compressed or extracted file. For this tutorial we will choose the input, the text file to be compressed, but the output will be a pre-specified file.


    Picture 1. Our light-weight form



    3. CODE DEVELOPMENT

    In order to write to or read from a file in your disk you need to create stream objects that do the actual writing/reading process. With stream objects you can write to a resource such as the memory or a particular file. For the compression of a file you need a compression stream. These streams are in the “System.IO.Compression” namespace. These streams behave differently than other streams since they can only read from or write to other streams. So, you need to create one stream for the reading of a file, one for the writing and one compression stream to compress the data that the writing stream gets. The compression stream targets the writing stream. The next step is to read the file and send chunks of data to the compression stream which automatically forwards the compressed data to the writing stream.

    The next snippet of code demonstrates the declaration of the three streams:

    Code:
    private void button1_Click(object sender, EventArgs e)
    
                {
    
                    openFileDialog1.Filter = "txt files |*.txt";
    
                    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    
                    {
    
                        FileStream source = File.OpenRead(openFileDialog1.FileName);
    
                        FileStream destination = File.Create(@"E:\stream.gz");
    
                        GZipStream myCompressionStream = new GZipStream(destination, CompressionMode.Compress);
    The first line of code filters the openDialog’s contents to display only text files. If the user correctly chose a file to open then the three streams are created. The source is the stream that will be compressed. The destination is the compressed file and the “myCompressionStream” comes between the two other streams. As you can see the CompressionMode has been set to “Compress”. When we will decompress the file this parameter will be set to “Decompress”.

    Now it is time to read the source one byte at a time and write the compressed data to the output, as shown here:

    Code:
    int myByte = source.ReadByte();
    
                        while (myByte != -1)
    
                        {
    
                            myCompressionStream.WriteByte((byte)myByte);
    
                            myByte = source.ReadByte();
    
                        }
    
                        destination.Close();
    When the method “ReadByte” returns “-1” it means that it has reached the end of file. So we read and compress data one byte at a time until the integer named “myByte” has a value different than “-1”. The compression of the file has been completed.

    To perform the decompressing algorithm we must, again define the streams that will perform the decompression. This time the Gzip stream wraps around the compressed file named here “dest2”. The destination of the compress process becomes the source in the decompression method. We use the ReadByte() method of the Gzip DecompressionStream to read and decompress the data. Next we send the decompressed data to the FileStream used for writing the new output file.

    Code:
    // Start Decompressing
    
                         FileStream dest2 = File.OpenRead(@"E:\stream.gz");
    
                         GZipStream myDecompressionStream = new GZipStream(dest2, CompressionMode.Decompress);
    
                         FileStream ExtractedFile = File.Create(@"E:\extracted.txt");
    
                         int mySecondByte = myDecompressionStream.ReadByte();
    
                         while (mySecondByte != -1)
    
                         {
    
                             ExtractedFile.WriteByte((byte)mySecondByte);
    
                             mySecondByte = myDecompressionStream.ReadByte();
    
                         }
    
                         source.Close();
    
                         destination.Close();
    
                         ExtractedFile.Close();
    
                    }
    
                }
    After the process is over we must close all streams in order to free our files for use by other streams/processes. If you wanted to compress and decompress the files with the DEFLATE method you would only have to change the “GZipStream” to a “DeflateStream” to accomplish your goal. However, bear in mind that these algorithms can process data up to 4GB.


    Need Help?
    Ask your questions here or create a new thread in the appropriate forum!

  2. #2
    Guru ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski's Avatar
    Join Date
    Mar 2009
    Posts
    1,374

    Re: Tutorial: Visual Studio 2008 C# Compressing

    I just made a prototype with both GZipStream and DeflateStream classes. A JPEG wallpaper I used had 0.99MB while the ahem compressed files had 1.52MB in size.

    Can you provide us with some reliable statistics regarding compression ratio?

  3. #3
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: Tutorial: Visual Studio 2008 C# Compressing

    Are you saying the compressed file was larger than the uncompressed file? I had forgotten I even wrote this tutorial.

  4. #4
    Guru ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski's Avatar
    Join Date
    Mar 2009
    Posts
    1,374

    Re: Tutorial: Visual Studio 2008 C# Compressing

    Yes, it was 150% bigger after compression. I do not know what is wrong with this technology. Maybe they just made those classes to support some HTTP-compression protocols? So the ratio was not the main purpose of it.

  5. #5
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,698
    Blog Entries
    57

    Re: Tutorial: Visual Studio 2008 C# Compressing

    zipping is NOT guaranteed to result in a smaller file. A jpg is a good candidate for a file that will get bigger when zipped.
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  6. #6
    Guru ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski's Avatar
    Join Date
    Mar 2009
    Posts
    1,374

    Re: Tutorial: Visual Studio 2008 C# Compressing

    Let us move the debate to another thread. I will post in this tutorial when we find some reliable explanation or solution. Is GZipStream compression useless?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Replies: 10
    Last Post: 09-25-2009, 07:28 AM
  2. Visual Studio 2008: C# Hello World Tutorial
    By Jordan in forum CSharp Tutorials
    Replies: 27
    Last Post: 07-23-2009, 04:32 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts