Java vs .NET GZIP Compression

Introduction

This article explains GZIP compression provided by Java and .NET. I explain here which is the best for compression with examples.
In Java, we have the GZIPOutputStream class, that provides GZIP compression. This class is in the "Java.util.zip" package. Whereas in .NET, we have the GZipStream class that does GZIP compression. This class is present in the "System.IO.Compression" namespace in the "System.dll" assembly.
I am explaining the better approach relevant to small file size because I have checked this on small files such as when we want to compress our message file before sending to someone.
 
1) Java GZIPOutputStream Class

The GZIPOutputStream class creates an input stream for compressing data in GZIP format file. This class has the following constructors:

  1. Creates an output stream with a default size:

     GZIPOutputStream(OutputStream out); 
     
  2. Creates a new output stream with a default buffer size and the specified flush mode:

    GZIPOutputStream(OutputStream out,boolean syncFlush);
     
  3.  Creates a new output stream with the specified buffer size:

    GZIPOutputStream(OutputStream out,int size);

  4. Creates a new output stream with the specified buffer size and flush mode:

    GZIPOutputStream(OutputStream out,int size,boolean syncFlush);


We need to write the following code to compress a file:

import java.io.*;

import java.util.zip.*;

 

class abc{

       public static void main(String args[])

        {

              String srcfile="D:/abhi.txt";

               String dstfile="D:/abhi1.txt";

              try{

                     FileInputStream fin= new FileInputStream(srcfile);

                     GZIPOutputStream fout=new GZIPOutputStream(new FileOutputStream(dstfile));

 

                   byte[] buffer = new byte[1024];

                   int bytesRead;

 

                   while ((bytesRead = fin.read(buffer)) != -1) //srcfile.getBytes()

                   {

                     fout.write(buffer, 0, bytesRead);

                   }

                     fin.close();

                    fout.close();

                     File file =new File(srcfile);

                  System.out.println("Before Compression file Size :

                    " + file.length()+" Bytes");

                     File file1 =new File(dstfile);

                     System.out.println("After Compression file Size :

                           " + file1.length()+" Bytes");

        }catch(Exception ex)

          {

              System.out.println(ex);

          }

   }

 

}  

Now run the code. The output will be as follows, since I am providing a source file of 481 bytes in size then the size of output file after compression is 207 bytes.

Now, we will check the GZIP compression with .NET with the same input file.

2) .NET GZipStream Class

"GZipStream" compresses a string or file. It lets you save data efficiently such as in compressed log files and message files. This class is present in the System.IO.Compression namespace. It creates GZIP files and writes them to the disk.

The GZipStream class provides the following constructors:

  1. Initializes a new instance of the GZipStream class by using the specified stream and compression level:

     GZipStream(Stream, CompressionLevel)
     
  2. Initializes a new instance of the GZipStream class by using the specified stream and compression mode:

     GZipStream(Stream, CompressionMode)
     
  3. Initializes a new instance of the GZipStream class by using the specified stream and compression level, and optionally leaves the stream open:

     GZipStream(Stream, CompressionLevel, Boolean) 
     
  4. Initializes a new instance of the GZipStream class by using the specified stream and compression mode, and optionally leaves the stream open:

     GZipStream(Stream, CompressionMode, Boolean)  

We need to write the following code to compress a file:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

using System.IO.Compression;

 

namespace Compress

{

   class Program

    {

        static void Main(string[] args)

        {

            string srcfile = "D:\\abhi.txt";

            string dstfile = "D:\\abhi2.txt";

            byte[] b;

            using (FileStream f = new FileStream(srcfile, FileMode.Open))

            {

                b = new byte[f.Length];

               f.Read(b, 0, (int)f.Length);

            }

           using (FileStream fs = new FileStream(dstfile, FileMode.Create))

           using (GZipStream gzip = new GZipStream(fs, CompressionMode.Compress, false))

            {

               gzip.Write(b, 0, b.Length);

            }

 

            FileInfo f2 = new FileInfo(srcfile);

            System.Console.WriteLine("Size Of File Before Compression :"+f2.Length);

            FileInfo f1 = new FileInfo(dstfile);

            System.Console.WriteLine("Size Of File Before Compression :" + f1.Length);

        }

}

Now run the code. The output will be as follows since I am providing the source file of size 481 bytes, then the size of output file after compression is 353 bytes.

So as you can see, the source file size is 481 bytes and the compressed file size is:

GzipStream of .NET: 353 Bytes
GZIPOutputStream of Java: 207 Bytes

The differences between the compressed sizes is big enough with respect to the source file. So we can conclude that Java's GZIP compression is better than .NET.

Summary

 I became familiar with this when I was working on interoperability between Java and .NET using IKVM.NET. So I thought it will be good to share this with everyone.

References

http://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream.aspx
http://docs.oracle.com/javase/7/docs/api/java/util/zip/GZIPOutputStream.html

Up Next
    Ebook Download
    View all
    Learn
    View all