Comparison of GZIP compression functions of Java and. NET

Source: Internet
Author: User

Comparison of GZIP compression functions of Java and. NET

This article mainly compares the GZIP compression functions provided by Java and. NET.

Introduction

In this article, we will discuss the GZIP compression functions provided by Java and. NET, and use examples to illustrate which compression method is better.

In Java, We have gzipcompressed gzipoutputstreamclass, which is included in the java.util.zip package. In. NET, we have a GZipStream class for GZIP Compression, which is in the namespace System. IO. Compression.

The better method I mentioned here is for small files, because I have checked the effect of small files, for example, when we want to compress our information files before sending them.

Code Parsing

1) Java GZIPOutputStream class

The GZIPOutputStream class creates an input stream for compressed data in a GZIP file. This class has the following constructor types:

1. Create an output stream with the default size:

GZIPOutputStream (OutputStream out );

2. Create a new output stream with default buffer size and specified refresh mode:

GZIPOutputStream (OutputStream out, boolean syncFlush );

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

GZIPOutputStream (OutputStream out, int size );

4. Create a new output stream with the specified buffer size and refresh mode:

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

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

 
 
  1. import java.io.*; 
  2. import java.util.zip.*; 
  3.  
  4. class abc{ 
  5.  
  6. public static void main(String args[]) 
  7.   { 
  8.    String srcfile="D:/abhi.txt"; 
  9.          String dstfile="D:/abhi1.txt"; 
  10.  
  11.   try{ 
  12.  
  13.    FileInputStream fin= new FileInputStream(srcfile); 
  14.        GZIPOutputStream fout=new GZIPOutputStream(new FileOutputStream(dstfile)); 
  15.  
  16.              byte[] buffer = new byte[1024]; 
  17.              int bytesRead; 
  18.  
  19.              while ((bytesRead = fin.read(buffer)) != -1) //srcfile.getBytes() 
  20.              { 
  21.                fout.write(buffer, 0, bytesRead); 
  22.              } 
  23.  
  24.                fin.close(); 
  25.                   fout.close(); 
  26.  
  27.                      File file =new File(srcfile); 
  28.                    System.out.println("Before Compression file Size : 
  29.                     " + file.length()+" Bytes"); 
  30.                      File file1 =new File(dstfile); 
  31.                      System.out.println("After Compression file Size : 
  32.                       " + file1.length()+" Bytes"); 
  33.  
  34.   }catch(Exception ex) 
  35.     { 
  36.   System.out.println(ex); 
  37.     } 
  38.    } 
  39.  

Run the code. The output is as follows, because the source file I provided only contains 481 bytes, and the size of the output file after compression is 207 bytes.

Now, we use the same input file to see the effect of GZIP compression.

2). NET GZipStream class

GZipStream compresses string or file. It allows you to effectively save data, such as compressing log files and message files. This class exists in the namespace of System. IO. Compression. It creates a GZIP file and writes it to the disk.

The GZipStream class provides the following constructor functions:

1. Use the specified byte stream and compression level to initialize a new instance of the GZipStream class:

GZipStream (Stream, CompressionLevel)

2. Use the specified stream and compression mode to initialize a new instance of the GZipStream class:

GZipStream (Stream, CompressionMode)

3. initialize a new instance of the GZipStream class by using the specified stream and compression level, and choose whether to enable the stream:

GZipStream (Stream, CompressionLevel, Boolean)

4. initialize a new instance of the GZipStream class by using the specified stream and compression mode, and choose whether to enable the stream:

GZipStream (Stream, CompressionMode, Boolean)

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

 
 
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5. using System.IO; 
  6. using System.IO.Compression; 
  7.  
  8. namespace Compress 
  9.     class Program 
  10.     { 
  11.         static void Main(string[] args) 
  12.         { 
  13.             string srcfile = "D:\\abhi.txt"; 
  14.             string dstfile = "D:\\abhi2.txt"; 
  15.  
  16.             byte[] b; 
  17.  
  18.             using (FileStream f = new FileStream(srcfile, FileMode.Open)) 
  19.             { 
  20.                 b = new byte[f.Length]; 
  21.                 f.Read(b, 0, (int)f.Length); 
  22.             } 
  23.  
  24.             using (FileStream fs = new FileStream(dstfile, FileMode.Create)) 
  25.  
  26.             using (GZipStream gzip = new GZipStream(fs, CompressionMode.Compress, false)) 
  27.             { 
  28.                 gzip.Write(b, 0, b.Length); 
  29.             } 
  30.  
  31.             FileInfo f2 = new FileInfo(srcfile); 
  32.             System.Console.WriteLine("Size Of File Before Compression :"+f2.Length); 
  33.  
  34.             FileInfo f1 = new FileInfo(dstfile); 
  35.             System.Console.WriteLine("Size Of File Before Compression :" + f1.Length); 
  36.         } 

Run the code. The output is as follows, Because I provide a 481-byte source file, and the compressed output file size is 353 bytes.

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

The size difference after compression is obvious. Therefore, we can conclude that the GZIP compression ratio of Java is better than that of. NET.

Point of interest

I found out when I used IKVM. NET to study the interoperability between Java and. NET. I think this is very interesting, so I will share it with you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.