Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of GZIP Compression function of Java and .NET

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "instance Analysis of GZIP Compression function of Java and .NET". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Code parsing

1) Java GZIPOutputStream class

This GZIPOutputStream class creates an input stream in a GZIP format file for compressed data. This class has the following constructors:

1. Create an output stream with the default size:

GZIPOutputStream (OutputStream out)

two。 Create a new output stream with a default buffer size and a 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:

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

Run the code. The output is as follows, because the source file I provided is only 481 bytes in size, and then the compressed file size is 207 bytes.

Now, let's use the same input file to see what the GZIP compression looks like.

2). NET GZipStream class

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

The GZipStream class provides the following constructors:

1. Initialize a new instance of the GZipStream class by using the specified byte stream and compression level:

GZipStream (Stream, CompressionLevel)

two。 Initialize a new instance of the GZipStream class by using the specified stream and compression mode:

GZipStream (Stream, CompressionMode)

3. Initializes a new instance of the GZipStream class with the specified stream and compression level, and optionally opens the stream:

GZipStream (Stream, CompressionLevel, Boolean)

4. Initializes a new instance of the GZipStream class by using the specified stream and compression mode, and optionally opens the stream:

GZipStream (Stream, CompressionMode, Boolean)

We need to write the following code to compress the 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);}}

Run the code. The output is as follows, because I provided a source file with a size of 481 bytes, and then the compressed output file size is 353 bytes.

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

.net GzipStream:353 bytes

GZIPOutputStream of Java: 207bytes

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

This is the end of the content of "GZIP Compression instance Analysis of Java and .NET". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report