Java和.NET的GZIP压缩功能对比

开发 后端
我是在使用IKVM.NET研究Java和.NET之间的互操作性时发现的。我认为这很有意思,所以分享给大家。

本文主要比较了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);

我们需要编写以下代码来压缩文件:

 

  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.  

运行代码。输出如下,因为我提供的源文件只有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)

我们需要编写以下代码来压缩文件:

  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.         } 

运行代码。输出如下,由于我提供的是481字节大小的源文件,然后压缩后的输出文件大小为353个字节。

大家可以看到,源文件为481字节,压缩文件大小为:

  1.  .NET的GzipStream:353字节

  2. Java的GZIPOutputStream :207字节

压缩后的尺寸大小差距很明显。因此,我们可以得出结论,Java的GZIP压缩比.NET更好。

兴趣点

我是在使用IKVM.NET研究Java和.NET之间的互操作性时发现的。我认为这很有意思,所以分享给大家。

责任编辑:王雪燕 来源: 码农网
相关推荐

2010-07-19 09:01:54

.NET 4.0缓存

2011-03-29 13:56:12

SQL Server 数据压缩

2011-08-10 17:38:56

Windows7自带的WinZip

2011-10-09 11:41:22

OracleZFSPillar Axio

2013-07-05 09:28:21

软路由路由技术

2010-02-03 15:11:53

C++内存区域

2009-11-06 17:10:34

ChromeFirefox功能对比

2010-09-14 13:10:36

2009-03-16 12:10:02

Windows 7微软对比

2013-03-22 13:31:52

App制作工具非编程

2023-09-14 22:57:52

2010-04-26 13:48:17

iPhone OSiOSWindows Pho

2017-06-06 09:56:03

hypervisor成本功能

2010-03-09 19:39:37

python程序调试

2019-10-29 09:14:52

ETL架构DataPipelin

2010-01-22 11:06:03

GNUkFreeBSDLinux

2015-04-13 10:54:42

java.netHashSet

2018-01-12 17:03:29

HTTPgzip压缩

2022-01-10 09:33:59

Firefox 95Chrome 97 Linux

2013-11-08 10:59:17

Hadoop虚拟化VMware vSph
点赞
收藏

51CTO技术栈公众号