GZIP app under Java

xiaoxiao2021-03-06  41

Gzip is a compressed method currently widely used, which has high compression ratio and compression efficiency. JAVA.UTIL.ZIP is included in the JDK released by Sun.

Package, providing direct support for Gzip. Gzip compression and decompression can be easily implemented using Java language. The following is the simplest program that compresses and decompress.

(Compressed: 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 the compressed file as a file input stream

FileInputStream Fin = New FileInputStream (Args [0]);

// Establish a compressed file output stream

FileOutputStream Fout = New FileOutputStream (args [1]);

/ / Establish GZIP compression output stream

Gzipoutputstream gzout = new gzipoutputStream (fout);

Byte [] buf = new byte [1024]; // Set the read buffer size

Int Num;

While (Num = Fin.Read (buf))! = -1)

{

Gzout.write (BUF, 0, NUM);

}

Gzout.close (); // !!! Close the stream, you must close all input and output flows. Ensure the input and output complete and release system resources.

Fout.close ();

Fin.close ();

} catch (ioException e)

{

System.out.println (e);

}

}

}

(Unzipped: 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

{

// Establish GZIP compressed file input stream

FileInputStream Fin = New FileInputStream (Args [0]);

// Establish GZIP to extract workflow

GzipinputStream gzin = new gzipinputstream (fin);

// Establish a decompression 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 languages ​​To implement saving work data (such as a drawing program to save a picture), often require a program data format to external

File format is stored, and the process of reverse conversion is in operation. Realization is more troublesome, programming is opaque. And Java language is an object-oriented language, using its object order

Increased characteristics, you can implement the working object (screen definition object data) directly to the hard disk, and then read in memory directly, no additional operation. Implement very

Convenient. However, due to the write object is a Java class format, the data redundancy is large. When the amount of data is large, it is often caused by the storage file. Excessive disk operation also leads to

Data reads take more time, accounting for a large amount of memory. The Gzip compressed storage object is a valid means to solve such problems (one of my own monitoring

In the system, transfer to a test screen (100,000 components, 7m compression, 600K after compression)

The degree also increased significantly. The effect is very obvious).

The Java program development network application is its biggest advantage, but in some low-speed networks (Internet, dial-up networks). Network often causes bottlenecks,

Affecting the application effect, which has a greater impact on the application of real-time requirements. Adopt compression can effectively improve communication effects.

It can be seen that the object Gzip compression under Java has a wide range of application. The following is a simple example program.

(Serialized data object file: data.java)

Import java.io. *;

Import java.util.zip. *;

Public Class Data Implements Serializable // Inherited Series Series

{

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 CompressObjectObject

{

// Seize the Data type data object to the object compression, return byte array, and compressed object arrays can write to file saving or for network transmission

Public static byte [] WriteCompressObject (data object_)

{

BYTE [] DATA_ = NULL;

Try

{

/ / Establish byte array output stream

ByteaRrayoutputstream O = New byteArrayoutputStream ();

/ / Establish GZIP compression output stream

GzipOutputStream Gzout = New GzipOutputStream (O);

// Establish an object serialized output stream

ObjectOutputStream out = new ObjectOutputStream (gzout);

Out.writeObject (Object_);

Out.flush ();

Out.close ();

gzout.close ();

/ / Return to the compressed word throttle

Data_ = o.tobyteaRray ();

O. close ();

} catch (ioException e)

{

System.out.println (e);

}

Return (DATA_);

}

// Restore the compressed byte array to Data type data object

Public Static Data ReadcompressObject (Byte [] DATA_)

{

Data Object_ = NULL;

Try

{

// Establish an array input stream

ByteaRrayinputStream I = New ByteArrayInputStream (DATA_);

// Establish GZIP decompression input stream

GzipinputStream Gzin = New GzipinputStream (i);

// Establish an object serialization input stream

ObjectInputStream in = New ObjectInputStream (gzin);

/ / Restore object by setting type

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 content

System.out.println ("Name =" TestData_.Name "AGE =" TestData_.age "Height =" TestData_.Height);

//compression

Byte [] i_ = compressObject.writecompressObject (TestData_);

/ *

Save or network transmission can be implemented, still restore or restore the peer

* /

//unzip

Data O_ = compressObject.readcompressObject (i_);

// Decompose the labilory object content

System.out.println ("Name =" O_.Name "Age =" O_.Age "Height =" O_.HeiGHT);

}

}

The above is just a simple example of applying Gzip compression under Java. Please use it to use it in your own development. Due to the level of my own level, improper

转载请注明原文地址:https://www.9cbs.com/read-73033.html

New Post(0)