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:
- Creates an output stream with a default size:
GZIPOutputStream(OutputStream out);
- Creates a new output stream with a default buffer size and the specified flush mode:
GZIPOutputStream(OutputStream out,boolean syncFlush);
- Creates a new output stream with the specified buffer size:
GZIPOutputStream(OutputStream out,int size);
- 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)
{
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:
- Initializes a new instance of the
GZipStream
class by using the specified stream and compression level:
GZipStream(Stream, CompressionLevel)
- Initializes a new instance of the
GZipStream
class by using the specified stream and compression mode:
GZipStream(Stream, CompressionMode)
- 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)
- 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:
GzipStream
of .NET: 353 BytesGZIPOutputStream
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