Java和.NET的GZIP壓縮功能對(duì)比
本文主要比較了Java和.NET提供的GZIP壓縮功能。
介紹
在本文中,我們將討論Java和.NET提供的GZIP壓縮功能,并且用實(shí)例來(lái)說(shuō)明哪個(gè)壓縮方法更佳。
在Java中,我們有提供GZIP壓縮的GZIPOutputStream類,這個(gè)類在Java.util.zip包中。而在.NET中,我們有執(zhí)行GZIP壓縮的GZipStream類,這個(gè)類在System.IO.Compression命名空間下。
我這里所說(shuō)的更好方法針對(duì)的是小尺寸文件,因?yàn)槲乙呀?jīng)檢驗(yàn)過(guò)小文件的效果,比如說(shuō)當(dāng)我們想在發(fā)送之前壓縮我們的信息文件。
代碼解析
1)Java GZIPOutputStream類
該GZIPOutputStream類為壓縮數(shù)據(jù)在GZIP格式文件中創(chuàng)建了輸入流。這個(gè)類有以下幾種的構(gòu)造函數(shù):
1.創(chuàng)建具有默認(rèn)大小的輸出流:
GZIPOutputStream(OutputStream out);
2.創(chuàng)建新的具有默認(rèn)緩沖區(qū)大小和指定刷新模式的輸出流:
GZIPOutputStream(OutputStream out,boolean syncFlush);
3.創(chuàng)建新的具有指定緩沖區(qū)大小的輸出流:
GZIPOutputStream(OutputStream out,int size);
4.創(chuàng)建新的具有指定的緩沖區(qū)大小和刷新模式的輸出流:
GZIPOutputStream(OutputStream out,int size,boolean syncFlush);
我們需要編寫以下代碼來(lái)壓縮文件:
- 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);
- }
- }
- }
運(yùn)行代碼。輸出如下,因?yàn)槲姨峁┑脑次募挥?81個(gè)字節(jié)大小,然后經(jīng)過(guò)壓縮后輸出的文件大小為207個(gè)字節(jié)。
現(xiàn)在,我們用相同的輸入文件來(lái)看看GZIP壓縮后的效果。
2).NET GZipStream類
GZipStream壓縮string或文件。它可以讓你有效地保存數(shù)據(jù),如壓縮日志文件,消息文件。這個(gè)類存在于System.IO.Compression的命名空間。它創(chuàng)建GZIP文件,并將其寫入磁盤。
GZipStream類提供以下構(gòu)造函數(shù):
1.通過(guò)使用指定字節(jié)流和壓縮等級(jí)初始化GZipStream類的新實(shí)例:
GZipStream(Stream, CompressionLevel)
2.通過(guò)使用指定流和壓縮模式初始化GZipStream類的新實(shí)例:
GZipStream(Stream, CompressionMode)
3.通過(guò)使用指定流和壓縮等級(jí)初始化GZipStream類的新實(shí)例,并可選是否打開(kāi)流:
GZipStream(Stream, CompressionLevel, Boolean)
4.通過(guò)使用指定流和壓縮模式初始化GZipStream類的新實(shí)例,并可選是否打開(kāi)流:
GZipStream(Stream, CompressionMode, Boolean)
我們需要編寫以下代碼來(lái)壓縮文件:
- 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);
- }
- }
運(yùn)行代碼。輸出如下,由于我提供的是481字節(jié)大小的源文件,然后壓縮后的輸出文件大小為353個(gè)字節(jié)。
大家可以看到,源文件為481字節(jié),壓縮文件大小為:
-
.NET的GzipStream:353字節(jié)
-
Java的GZIPOutputStream :207字節(jié)
壓縮后的尺寸大小差距很明顯。因此,我們可以得出結(jié)論,Java的GZIP壓縮比.NET更好。
興趣點(diǎn)
我是在使用IKVM.NET研究Java和.NET之間的互操作性時(shí)發(fā)現(xiàn)的。我認(rèn)為這很有意思,所以分享給大家。