JAVA basics: Implement zip compression and decompression using Java

Source: Internet
Author: User
JAVA basics: use Java to implement zip compression and decompression-Linux general technology-Linux programming and kernel information. The following is a detailed description. Due to limited network bandwidth, Data File compression is conducive to fast data transmission over the Internet, while also saving the server's external storage space. Java 1.1 implements a single interface between I/O data streams and network data streams. Therefore, data compression, network transmission, and decompression are easier to implement, the following describes how to use the ZipEntry, ZipInputStream, and ZipOutputStream Java classes to compress zip data.
Zip compression file structure: a zip file consists of multiple entries, each entry has a unique name, And the entry 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. It supports compression and non-compression.

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, disable it in the zip file
Write a new entry-newe

The data stream is located at the starting position of the entry data item. The compression method is setMethod
Method.

· Class ZipInputStream

ZipInputStream implements the read and input stream of zip compressed files. It supports compression and non-compression.
Compress the entry. 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. The randomData () function randomly generates 50 double data records and stores them in the doc string variable. The openFile () function reads the ZIP compressed file and saveFile () the function saves 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 the compressed size.
Line text display field

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); set the compression party
Method

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

Worker generates a ZIP entry, writes it to the file output stream, and locates the output stream in
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! ");

}

}

}

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.