Click here to Skip to main content
Click here to Skip to main content

GZIP Compression Java vs .NET

, 19 May 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

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

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:

    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 for compress 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 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)

Share

About the Author

Abhishek Kumar Goswami
Software Developer
India India
I like to code and I really enjoy to share my knowledge with all, Its my passion.
 
http://allaboutdotnet.com/

Comments and Discussions

 
GeneralDefault compression level in java ? PinmemberSB_CodeForFun19-Mar-14 23:20 
GeneralRe: Default compression level in java ? PinmemberSB_CodeForFun19-Mar-14 23:32 
GeneralRe: Default compression level in java ? PinprofessionalAbhishek Kumar Goswami20-Mar-14 3:37 
QuestionDefault Compression Mode PinmemberMember 24974519-May-13 22:57 
AnswerRe: Default Compression Mode Pinmemberabhishek_jmk20-May-13 4:46 
NewsRe: Default Compression Mode PinmemberChad3F22-May-13 12:32 
AnswerRe: Default Compression Mode Pinmemberabhishek_jmk22-May-13 18:10 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 19 May 2013
Article Copyright 2013 by Abhishek Kumar Goswami
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid