Java和.NET的GZIP壓縮功能對比

2015-07-11    分類:.NET開發、JAVA開發、程式設計開發、首頁精華12人評論發表於2015-07-11

本文由碼農網 – 王國峰原創翻譯,轉載請看清文末的轉載要求,歡迎參與我們的付費投稿計劃

本文主要比較了Java和.NET提供的GZIP壓縮功能。

介紹

在本文中,我們將討論Java和.NET提供的GZIP壓縮功能,並且用例項來說明哪個壓縮方法更佳。

在Java中,我們有提供GZIP壓縮的GZIPOutputStream類,這個類在Java.util.zip包中。而在.NET中,我們有執行GZIP壓縮的GZipStream類,這個類在System.IO.Compression名稱空間下。

我這裡所說的更好方法針對的是小尺寸檔案,因為我已經檢驗過小檔案的效果,比如說當我們想在傳送之前壓縮我們的資訊檔案。

程式碼解析

1)Java GZIPOutputStream類

該GZIPOutputStream類為壓縮資料在GZIP格式檔案中建立了輸入流。這個類有以下幾種的建構函式:

1.建立具有預設大小的輸出流:

GZIPOutputStream(OutputStream out);

2.建立新的具有預設緩衝區大小和指定重新整理模式的輸出流:

GZIPOutputStream(OutputStream out,boolean syncFlush);

3.建立新的具有指定緩衝區大小的輸出流:

GZIPOutputStream(OutputStream out,int size);

4.建立新的具有指定的緩衝區大小和重新整理模式的輸出流:

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

我們需要編寫以下程式碼來壓縮檔案:

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);
	   }
   }

}

執行程式碼。輸出如下,因為我提供的原始檔只有481個位元組大小,然後經過壓縮後輸出的檔案大小為207個位元組。

現在,我們用相同的輸入檔案來看看GZIP壓縮後的效果。

2).NET GZipStream類

GZipStream壓縮string或檔案。它可以讓你有效地儲存資料,如壓縮日誌檔案,訊息檔案。這個類存在於System.IO.Compression的名稱空間。它建立GZIP檔案,並將其寫入磁碟。

GZipStream類提供以下建構函式:

1.通過使用指定位元組流和壓縮等級初始化GZipStream類的新例項:

GZipStream(Stream, CompressionLevel)

2.通過使用指定流和壓縮模式初始化GZipStream類的新例項:

GZipStream(Stream, CompressionMode)

3.通過使用指定流和壓縮等級初始化GZipStream類的新例項,並可選是否開啟流:

GZipStream(Stream, CompressionLevel, Boolean)

4.通過使用指定流和壓縮模式初始化GZipStream類的新例項,並可選是否開啟流:

GZipStream(Stream, CompressionMode, Boolean)

我們需要編寫以下程式碼來壓縮檔案:

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); 
        }
}

執行程式碼。輸出如下,由於我提供的是481位元組大小的原始檔,然後壓縮後的輸出檔案大小為353個位元組。

大家可以看到,原始檔為481位元組,壓縮檔案大小為:

  1.  .NET的GzipStream:353位元組
  2. Java的GZIPOutputStream :207位元組

壓縮後的尺寸大小差距很明顯。因此,我們可以得出結論,Java的GZIP壓縮比.NET更好。

興趣點

我是在使用IKVM.NET研究Java和.NET之間的互操作性時發現的。我認為這很有意思,所以分享給大家。

譯文連結:http://www.codeceo.com/article/java-net-gzip.html
英文原文:GZIP Compression Java vs .NET
翻譯作者:碼農網 – 王國峰
轉載必須在正文中標註並保留原文連結、譯文連結和譯者等資訊。]

相關文章