ZIP compression / decompression using Java
--- Excerpt from the Internet
Since the network bandwidth is limited, the compression of the data file is conducive to the quick transmission of data on the Internet, and also
The deposit space of the provincial server.
Java 1.1 implements a single interface for I / O data stream and network data stream, so data compression, network transmission and solution
Compression is relatively easy, hereby introducing three Java using Zipentry, ZipinputStream and ZipOutputStream
Class implementation of the programming method of ZIP data compression.
ZIP compressed file structure: A zip file consists of multiple Entry, each entry has a unique name, entry
Data item stores compressed data.
Several Java classes related to ZIP files
· Class ZIPENTRY
Public Zipentry (String Name);
Name is the specified data item name.
· Class ZipOutputStream
ZipOutputStream implements the write output stream of ZIP compressed files, supports compression and non-compressed Entry. Here is its
Several functions:
Public ZipOutputStream (OutputStream Out);
∥ Use the output stream OUT to construct a ZIP output stream.
Public void setMethod (int method);
∥ Set the Entry compression method, the default value is deflated.
Public void Putnextentry (ZIPENTRY NEWE);
∥ If the current entry exists and in the active state, turn off it, write new entry-newe in the zip file
And the data is streamded in the starting position of the ENTRY data item, the compression method is the method specified by SetMethod.
· Class ZipinputStream
ZipinputStream implements the read input stream of ZIP compressed files, support compression and non-compressed Entry. Here is its
Several functions:
Public ZipinputStream (InputStream IN);
∥ Use the input stream in constructs a ZIP output stream.
Public zipentry getnextentry ();
∥ Return to the next entry in the zip file, and position the output stream in the starting position of this Entry data item.
Public void closeentry ();
∥ Turn off the current ZIP Entry and set the data to the starting position of the next Entry.
Program code and its comments
The following procedures implement the compression and decompression method of data file ZIP mode. RandomData () function randomly generates
50 Double data and placed in the DOC string variable; the openfile () function reads the ZIP compressed file; savefile () function
The randomly generated data is stored in the compressed file in the 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; ∥∥ Multi-line text display domain
TextField Infotip; ∥ Display data file uncompressed size and compression size single line text display domain
String Doc; ∥ Store randomly generated data
Long doczipsize = 0; ∥∥ Compressed data file size
Public testzip () {
∥ Generate menu
MenuBar MenuBar = New Menubar ();
setmenubar;
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 domain for randomly generated data files
Add ("Center", Textarea = New TextArea ());
∥∥ Tip text original size, compressed single line text display domain
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 generate 50 Double data and placed 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 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, then return
∥用 File input stream construction ZIP compression input stream
ZipinputStream Zipis = New ZipinputStream (New FileInputStream (f));
Zipis.getnextentry ();
∥ Position the input stream in the current ENTRY data item location
DataInputStream DIS = New DataInputStream (Zipis);
∥ Use ZIP input stream construction DataInputStream
DOC = dishdutf (); ∥ Read file content
Dis.close (); ∥ Close file
Doczipsize = f.length (); ∥ Get the ZIP file length
ShowTextandInfo (); ∥∥ Data
}
Catch (IOException IoE) {
System.out.println (IOE);
}
}
Private void savefile () {
∥ Open the ZIP file and write the DOC string variable into 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, then return
∥用 Use the file output stream construction ZIP compressed output stream
ZipOutputStream Zipos = New ZipOutputStream (New FileoutputStream (f));
Zipos.SetMethod (zipoutputStream.deflated); ∥ Set compression method
Zipos.putNextentry (New Zipentry ("zip"));
∥ Generate a zip entry, write into the file output stream, and position the output stream in the Entry start.
DataOutputstream OS = New DataOutputStream (zipos);
∥ Use zip output stream construction to build DataOutputStream;
Os.WriteUTF (DOC); ∥∥ Write random data into the file
Os.close (); ∥ Close data stream
DOCZIPSIZE = f.length ();
∥ Get the length of the compressed file
ShowTextandInfo (); ∥∥ 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); ∥ Close the program
}
Else {
System.out.println ("No this command!");
}
}
}