Implement zip compression/Decompression using Java

Source: Internet
Author: User

Due to limited network bandwidth, Data File compression is conducive to fast data transmission over the Internet.

Saves the server's external storage space.

Java 1.1 implements a single interface between the I/O data stream and the network data stream, so data compression, network transmission, and reconciliation

The implementation of compression is relatively easy. The following describes the use of ZipEntry, ZipInputStream, and ZipOutputStream Java

Class.

Zip compressed file structure: a zip file consists of multiple entries, each entry has a unique name

Data items store compressed data.

Several Java classes related to zip files

· Class ZipEntry

Public ZipEntry (String name );

Name is the name of the specified data item.

· Class ZipOutputStream

ZipOutputStream implements the write and output stream of zip compressed files, and supports compression and non-compression entry. Below is its

Several functions:

Public ZipOutputStream (OutputStream out );

The producer uses the output stream out to construct a ZIP output stream.

Public void setMethod (int method );

Delimiter sets the entry compression method. The default value is DEFLATED.

Public void putNextEntry (ZipEntry newe );

If the current entry exists and is active, close it and write a new entry-newe to the zip file.

The data stream is located at the starting position of the entry data item, and the compression method is the method specified by setMethod.

· Class ZipInputStream

ZipInputStream implements the read input stream of zip compressed files and supports compression and non-compression of entries. Below is its

Several functions:

Public ZipInputStream (InputStream in );

Pipeline uses the input stream in to construct a ZIP output stream.

Public ZipEntry getNextEntry ();

The cursor returns the next entry in the ZIP file and locates the output stream at the starting position of the entry data item.

Public void closeEntry ();

∥ Closes the current zip entry and positions the data stream at the starting position of the next entry.

Program code and comments

The following program implements the zip compression and decompression methods for data files. Random generation of the randomData () function

50 double data entries and put them in doc string variables; openFile () function reads ZIP compressed files; saveFile () function

Save the randomly generated data to a compressed file in ZIP format.

Import java.util.zip .*;

Import java. awt. event .*;

Import java. awt .*;

Import java. lang. Math;

Import java. io .*;

Public class TestZip extends Frame implements ActionListener {

TextArea textarea; multiple lines of text display fields in the data file

TextField infotip; Compressed: displays the uncompressed data file size and compressed size. Single-line text display fields

String doc; random stores randomly generated data

Long doczipsize = 0; Bytes: the size of the compressed data file

Public TestZip (){

Renewal menu

MenuBar menubar = new MenuBar ();

SetMenuBar (menubar );

Menu file = new Menu ("File", true );

Menubar. add (file );

MenuItem neww = new MenuItem ("New ");

Neww. addActionListener (this );

File. add (neww );

MenuItem open = new MenuItem ("Open ");

Open. addActionListener (this );

File. add (open );

MenuItem save = new MenuItem ("Save ");

Save. addActionListener (this );

File. add (save );

MenuItem exit = new MenuItem ("Exit ");

Exit. addActionListener (this );

File. add (exit );

Multi-line text display field of the randomly generated data file

Add ("Center", textarea = new TextArea ());

Bytes indicates the single-line text display fields of the original text size and compressed size.

Add ("South", infotip = new TextField ());

}

Public static void main (String args []) {

TestZip OK = new TestZip ();

OK. setTitle ("zip sample ");

OK. setSize (600,300 );

OK. show ();

}

Private void randomData (){

The token randomly generates 50 double data records and stores them in the doc string variable.

Doc = "";

For (int I = 1; I <51; I ++ ){

Double rdm = Math. random () * 10;

Doc = doc + new Double (rdm). toString ();

If (I % 5 = 0) doc = doc + "n ";

Else doc = doc + "";

}

Doczipsize = 0;

ShowTextandInfo ();

}

Private void openFile (){

∥ Open the zip file and read the file content into the doc string variable.

FileDialog dlg = new FileDialog (this, "Open", FileDialog. loa d );

Dlg. show ();

String filename = dlg. getDirectory () + dlg. getFile ();

Try {

Creating a file instance

File f = new File (filename );

If (! F. exists () return; if the exist file does not exist, return

Using a file input stream to build a ZIP compressed input stream

ZipInputStream zipis = new ZipInputStream (new FileInputStream (f ));

Zipis. getNextEntry ();

Locate locates the input stream at the position of the current entry data item

DataInputStream dis = new DataInputStream (zipis );

Using a ZIP input stream to build a DataInputStream

Doc = dis. readUTF (); iterator reads the File Content

Dis. close (); close the file

Doczipsize = f. length (); extract to get the ZIP file length

ShowTextandInfo (); displays data

}

Catch (IOException ioe ){

System. out. println (ioe );

}

}

Private void saveFile (){

∥ Open the zip file and write the doc string variable to the zip file.

FileDialog dlg = new FileDialog (this, "Save", FileDialog. SAVE );

Dlg. show ();

String filename = dlg. getDirectory () + dlg. getFile ();

Try {

Creating a file instance

File f = new File (filename );

If (! F. exists () return; if the exist file does not exist, return

Using the file output stream to build a ZIP compressed output stream

ZipOutputStream zipos = new ZipOutputStream (new FileOutputStream (f ));

Zipos. setMethod (ZipOutputStream. DEFLATED); used to set the compression method

Zipos. putNextEntry (new ZipEntry ("zip "));

Worker generates a ZIP entry, writes it to the file output stream, and locates the output stream at the entry start point.

DataOutputStream OS = new DataOutputStream (zipos );

Using the ZIP output stream to build DataOutputStream;

OS. writeUTF (doc); random writes randomly generated data to the file

OS. close (); disconnect to close the data stream

Doczipsize = f. length ();

∥ Get the length of the compressed file

ShowTextandInfo (); displays data

}

Catch (IOException ioe ){

System. out. println (ioe );

}

}

Private void showTextandInfo (){

∥ Displays data files and compression Information

Textarea. replaceRange (doc, 0, textarea. getText (). length ());

Infotip. setText ("uncompressed size:" + doc. length () + "compressed size:" + dc zipsize );

}

Public void actionreceivmed (ActionEvent e ){

String arg = e. getActionCommand ();

If ("New". equals (arg) randomData ();

Else if ("Open". equals (arg) openFile ();

Else if ("Save". equals (arg) saveFile ();

Else if ("Exit". equals (arg )){

Dispose (); close the window

System. exit (0); program to close the program

}

Else {

System. out. println ("no this command! ");

}

}

}

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.