GZIP Application under Java

Source: Internet
Author: User
Tags format array definition exit implement object serialization serialization zip
Gzip is a widely used compression mode, it has a high compression ratio and compression efficiency. Java.util.zip is included in the JDK published by Sun Inc.
Package, providing direct support for Gzip. Using the Java language makes it easy to implement gzip compression and decompression. Here is a simplified procedure for compression and decompression.
(Compression: Gzip.java)
Import java.io.*;
Import java.util.zip.*;

public class gzip
{
public static void Main (string[] args)
{
if (Args.length!=2)
{
SYSTEM.OUT.PRINTLN ("Usage:java gzip");
System.exit (1);
}
Try
{
Open a compressed file as a file input stream
FileInputStream fin=new FileInputStream (args[0]);
Create a compressed file output stream
FileOutputStream fout=new FileOutputStream (args[1]);
Establish gzip compressed output stream
Gzipoutputstream gzout=new Gzipoutputstream (fout);
Byte[] buf=new byte[1024];//set Read buffer size
int num;

while ((Num=fin.read (BUF))!=-1)
{
Gzout.write (Buf,0,num);
}
Gzout.close ()//!!! Close the stream and all input and output streams must be closed. Ensure input and output integrity and release system resources.
Fout.close ();
Fin.close ();
}catch (IOException e)
{
System.out.println (e);
}
}
}
(Decompression: Unzip.java)
Import java.io.*;
Import java.util.zip.*;

public class Ungzip
{
public static void Main (string[] args)
{
if (Args.length!=2)
{
System.out.println ("Usage:java ungzip");
System.exit (1);
}
Try
{
Create gzip compressed file input stream
FileInputStream fin=new FileInputStream (args[0]);
Establish gzip decompression Workflow
Gzipinputstream gzin=new Gzipinputstream (Fin);
Create uncompressed file output stream
FileOutputStream fout=new FileOutputStream (args[1]);
Byte[] Buf=new byte[1024];
int num;

while ((Num=gzin.read (buf,0,buf.length))!=-1)
{
Fout.write (Buf,0,num);
}
Gzin.close ();
Fout.close ();
Fin.close ();
}catch (IOException e)
{
System.out.println (e);
}
}
}
Non-object-oriented language to implement the preservation of work data (such as a drawing program to save a picture of the definition), often requires a program data format to be converted to external
File format storage, the process of reverse conversion when working. Implementation is more cumbersome, programming is not transparent. The Java language is an object-oriented language that uses its object order
Column characteristics, you can implement the work object (screen definition object data) directly to the hard disk, when needed, and then read into the memory directly, without any additional operation. Implemented very
Convenient. However, the data redundancy is large because the object is written in Java class format. When the volume of data is large, it often results in large storage files. Excessive disk operation also causes
Data reading takes more time and has a lot of memory. Using gzip to compress storage objects is an effective way to solve such problems. (In my implementation of a monitoring system
The integration of a test screen (100,000 components, compressed before 7M, compressed 600K), compression before the image after the memory for 26M, compressed read into memory for 12M, read into the speed
Degree is also greatly increased. The effect is very obvious.
Java programs that develop network applications are the biggest advantages, but in some low speed networks (Internet, dial-up Networking). Networks often cause transmission bottlenecks,
Impact on the application effect, for real-time requirements of the application of high impact. Using compression can effectively improve the communication effect.
Visible, in Java, the object gzip compression has a wide range of application value. The following is a simple case program.
(Serialized data Object file: Data.java)
Import java.io.*;
Import java.util.zip.*;
The public class Data implements serializable//inherits the serial serialization interface
{
String name= "Match";
int age=123;
float height=1.902f;
}
(Object compression Decompression class file: Compressobject.java)
Import java.util.zip.*;
Import java.io.*;
Public final class Compressobject
{
Serializes object compression with data type, returns byte array, compressed object array can be written to file save or used for network transmission
public static byte[] Writecompressobject (Data object_)
{
Byte[] Data_=null;
Try
{
To establish a byte array output stream
Bytearrayoutputstream o = new Bytearrayoutputstream ();
Establish gzip compressed output stream
Gzipoutputstream gzout=new Gzipoutputstream (o);
Create an object serialization output stream
ObjectOutputStream out = new ObjectOutputStream (gzout);
Out.writeobject (object_);
Out.flush ();
Out.close ();
Gzout.close ();
return compressed byte stream
Data_=o.tobytearray ();
O.close ();
}catch (IOException e)
{
System.out.println (e);
}
return (Data_);
}
To restore a compressed byte array to a data-type Date Object
public static Data Readcompressobject (byte[] data_)
{
Data Object_=null;
Try
{
Create a byte array input stream
Bytearrayinputstream i = new Bytearrayinputstream (Data_);
Create gzip Decompression input stream
Gzipinputstream gzin=new Gzipinputstream (i);
Create an object serialization input stream
ObjectInputStream in = new ObjectInputStream (Gzin);
Restore objects by type of development
object_= (Data) in.readobject ();
I.close ();
Gzin.close ();
In.close ();
}catch (ClassNotFoundException e)
{
System.out.println (e);
}catch (IOException e)
{
System.out.println (e);
}
return (OBJECT_);
}
}
(Main program: Test.java)
Import java.io.*;
Import java.util.zip.*;

public class Test
{
public static void Main (string[] args)
{
Data testdata_=new data ();
Uncompressed data Object Contents
System.out.println ("name=" +testdata_.name+ "age=" +testdata_.age+ "height=" +testdata_.height);
Compression
Byte[] I_=compressobject.writecompressobject (Testdata_);

/*
Perform save or network transfer, restore or restore on end when needed
*/

Decompression
Data O_=compressobject.readcompressobject (i_);
Object content after decompression
System.out.println ("name=" +o_.name+ "age=" +o_.age+ "height=" +o_.height);
}
}
Above is only Java under the application of gzip compression A simple example, please extrapolate in their own development. Due to my level, the improper place


Related Article

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.