Using Java to implement ZIP compression/decompression _jsp programming

Source: Internet
Author: User
Because of limited network bandwidth, the compression of data files facilitates the rapid transmission of data over the Internet, and also

The storage space of the province server.

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

The implementation of compression is easier, as described below, using the ZipEntry, Zipinputstream, and Zipoutputstream three Java

Class to implement the ZIP data compression method of programming.

Zip compressed file structure: A ZIP file consists of multiple entry, each entry has a unique name, 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 realizes the write output stream of Zip compressed file, and supports compression and uncompressed entry. Here's what it's

Several functions:

Public Zipoutputstream (OutputStream out);

∥ uses output stream out to construct a zip output stream.

public void Setmethod (int method);

∥ sets the entry compression method with the default value of deflated.

public void Putnextentry (ZipEntry newe);

∥ if the current entry exists and is active, close it and write a new Entry-newe in the zip file

The data flow is positioned at the starting position of the entry data item, and the compression method is the method specified by Setmethod.

• Class Zipinputstream

Zipinputstream realizes the read input stream of Zip compressed file, and supports compression and uncompressed entry. Here's what it's

Several functions:

Public Zipinputstream (InputStream in);

∥ uses input stream in to construct a zip output stream.

Public ZipEntry getnextentry ();

∥ returns the next entry in the zip file and positions the output stream at the beginning of the entry data item.

public void Closeentry ();

∥ closes the current zip entry and positions the data flow at the beginning of the next entry.

program code and its annotations

The following program implements the compression and decompression method of the data file Zip mode. Random generation of Randomdata () function

50 double data and placed in a DOC string variable; the OpenFile () function reads a zip compressed file; SaveFile () function

Saves randomly generated data to a compressed file in a 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; ∥ display a multiple-text display field for a data file

TextField InfoTip; ∥ Display data file uncompressed size and compression size single-line text display field

String Doc; ∥ store randomly generated data

Long doczipsize = 0;∥ size of the compressed data file

Public Testzip () {

∥ Generate 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);

∥ a multiple-line text display field for randomly generated data files

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

∥ prompt text The original size, the compressed size of a single line of text display field

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 () {

∥ randomly generates 50 double data and is placed in a 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 () {

∥ opens the zip file and reads the contents of the file into the DOC string variable.

FileDialog dlg=new FileDialog (This, "Open", Filedialog.loa D);

Dlg.show ();

String filename=dlg.getdirectory () +dlg.getfile ();

try{

∥ Create a file instance

File F=new file (filename);

if (!f.exists ()) return; ∥ file does not exist, it returns

∥ using file input stream to construct zip compressed input stream

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

Zipis.getnextentry ();

∥ position the input stream at the current entry data item location

DataInputStream dis=new DataInputStream (Zipis);

∥ using zip input stream to construct DataInputStream

Doc=dis.readutf (); ∥ read the contents of the file

Dis.close (); ∥ close File

Doczipsize = F.length (); ∥ get zip file length

Showtextandinfo (); ∥ Display data

}

catch (IOException IoE) {

System.out.println (IoE);

}

}

private void SaveFile () {

∥ opens the zip file and writes 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{

∥ Create a file instance

File F=new file (filename);

if (!f.exists ()) return; ∥ file does not exist, it returns

∥ using file output stream to construct zip compressed output stream

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

Zipos.setmethod (zipoutputstream.deflated); ∥ Set Compression method

Zipos.putnextentry (New ZipEntry ("zip"));

∥ generates a ZIP entry, writes to the file output stream, and locates the output stream at the beginning of the entry.

DataOutputStream os=new DataOutputStream (Zipos);

∥ using a zip output stream to construct the DataOutputStream;

Os.writeutf (DOC); ∥ writes randomly generated data to a file

Os.close (); ∥ Shutdown Data Flow

Doczipsize = F.length ();

∥ gets the length of the compressed file

Showtextandinfo (); ∥ Display data

}

catch (IOException IoE) {

System.out.println (IoE);

}

}

private void Showtextandinfo () {

∥ display data files and compressed information

Textarea.replacerange (Doc,0,textarea.gettext (). Length ());

Infotip.settext ("Uncompressed Size:" +doc.length () + "Compressed size:" +DC zipsize);

}

public void actionperformed (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 window

System.exit (0); ∥ shutdown 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.