Click here to Skip to main content
15,881,882 members
Articles / General Programming / Compression
Tip/Trick

GZIP Compression Java vs .NET

Rate me:
Please Sign up or sign in to vote.
3.86/5 (3 votes)
18 May 2013CPOL2 min read 25.5K   5   8
This tip compares GZIP compression facility provided by Java and .NET.

Introduction

In this tip, I am going to discuss about GZIP compression provided by Java and .NET. Here, I will explain which is the best approach for compression with example.

In Java, we have GZIPOutputStream class, which provides GZIP compression.This class presents in Java.util.zip package. While in .NET, we have GZipStream class which performs GZIP compression. This class is present in System.IO.Compression package.

I am explaining the better approach in respect of small size file because I have checked this on small files such as when we want to compress our message file before sending to any one.

Using the Code 

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:

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

    Java
    GZIPOutputStream(OutputStream out,boolean syncFlush);

  3. Creates a new output stream with the specified buffer size:

    Java
    GZIPOutputStream(OutputStream out,int size);

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

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

We need to write the following code to compress file:

Java
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 source file of size 481 bytes then the size of output file after compression is 207 bytes.

Image 1

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

2) .NET GZipStream Class

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

GZipStream class provides the following constructors:

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

    Java
    GZipStream(Stream, CompressionLevel) 
  2. Initializes a new instance of the GZipStream class by using the specified stream and compression mode:
    Java
    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:
    Java
    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:
    Java
    GZipStream(Stream, CompressionMode, Boolean)   

We need to write the following code for compress file:

Java
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.

Image 2

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

  1. GzipStream of .NET: 353 Bytes
  2. GZIPOutputStream of Java: 207 Bytes
Differences between the size of compressed size is big enough with respect to the source file. So we can conclude that Java's GZIP compression is better than .NET.

Points of Interest

I come to know this when I was working on interoperability between Java and .NET using IKVM.NET. So I thought it will be good to share this thing with all.

References

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
I like to code and I really enjoy to share my knowledge with all, Its my passion.

http://abhishekgoswami.com/

Comments and Discussions

 
Generalhave to give you a star Pin
JRoger_26-Sep-15 0:33
JRoger_26-Sep-15 0:33 
GeneralDefault compression level in java ? Pin
SB_CodeForFun19-Mar-14 22:20
SB_CodeForFun19-Mar-14 22:20 
GeneralRe: Default compression level in java ? Pin
SB_CodeForFun19-Mar-14 22:32
SB_CodeForFun19-Mar-14 22:32 
GeneralRe: Default compression level in java ? Pin
Abhishek Kumar Goswami20-Mar-14 2:37
professionalAbhishek Kumar Goswami20-Mar-14 2:37 
QuestionDefault Compression Mode Pin
Hybertz19-May-13 21:57
Hybertz19-May-13 21:57 
AnswerRe: Default Compression Mode Pin
Abhishek Kumar Goswami20-May-13 3:46
professionalAbhishek Kumar Goswami20-May-13 3:46 
NewsRe: Default Compression Mode Pin
Chad3F22-May-13 11:32
Chad3F22-May-13 11:32 
AnswerRe: Default Compression Mode Pin
Abhishek Kumar Goswami22-May-13 17:10
professionalAbhishek Kumar Goswami22-May-13 17:10 

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.