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.

ProgramCodeAnd its annotations

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; 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 = 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.