Using Java1.1 realistic ZIP-based compressiondecompression

zhaozj2021-02-08  310

Since the network bandwidth is limited, the compression of the data is favorable in the fast transmission of the data on the Internet, and the outer deposit space of the seductor is saved.

Java implements a single interface for I / O data streams and network data streams. Therefore, the data is compressed, network transmission and decompression is relatively easy. The following describes the programming method of using ZIPENTRY, ZipinputStream and ZipOutstReam three Java classes. .

ZIP compression documentation: A zip file is composed of multiple Entry, each entry has a unique name, and Entry's data item stores compression data.

Several Java classes with ZIP files

Class ZIPENTRY

Public zipentry (String name); Name is the specified data entry. The ZipoutputStream ZipoutputStream implements the write-output flow of ZIP compression documents, support compression and non-compression Entry. There are several functions of it below: public zipoutputStream (OutputStream out); // Use the output flow OUT to construct a ZIP output stream. Public void setMethod (INTMETHOD); // Setting the ENTRY compression method, the default value is deflated. Public void putnextentry (ZIPENTRY NEWE); // If the current entry exists and is in an active state, turn off it, write new Entry-Newe in the zip file, and set the data to the starting position of the Entry data item. The compression method is the method specified by SetMethod.

ZipinputStream

ZipinputStream realizes the readover flow of ZIP compression documents, support compression and non-compression Entry. There are several functions of it below:

Public ZipinputStream (InputStream IN); // Use an input stream in configured a ZIP output stream. Public zipentry getnextentry (); // returns the next Entry in the zip file and the starting position of the output stream is located in this Entry data item. Public void closeEntry (); // Close the previous Zip Entry and set the data to the starting position of the next Entry.

Procedure code and comment

The following procedure is realized in the compression and decompression method of data ZIP. The randomData () function randomly generates 50 Double data and placed in the DOC string variable; the OpenFile () function reads the ZIP compression document; the SaveFile () function will be randomly generated into the zip format compression document.

Import java.util.zip. *;

Import java.awt.event. *; import java.awt. *;

Import java.lang.math;

Import java.io. *;

Public Class Testzip Extends Frame

Implements anctionsListener {

Textarea textarea; //

Multi-line text display domain displaying data files

TextField Infotip; //

Display data file uncompressed size and compression size single line text display domain

String doc; // Store randomly generated data

Long doczipsize = 0; // The size of the compressed data file

Public testzip () {

/ / Build 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 of randomly generated data files

Add ("Center", Textarea = New TextArea ());

/ / Tip the original size, compressed size, 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, read the file content into the DOC string variable.

FileDialog Dlg = New FileDialog (this, "open", filedialog.load;

Dlg.show ();

String filename = dlg.getdirectory () DLG.GetFile ();

Try {

// Create a file instance

FILE F = New File (filename);

If (! f.exists ()) return; // The file does not exist, then return to // build ZIP compression input stream with file 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);

// Set up DataInputStream with ZIP input

DOC = dishdutf (); // read file content

Dis.close (); // Close file

Doczipsize = f.length (); // Get ZIP file length

ShowTextandInfo (); // Display 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.exiss ()) return; // does not exist, then return

// Building a zip compressed output stream with file output

ZipOutputstream zipos = new

ZipOutputStream (New FileoutputStream (f));

Zipos.SetMethod (zipoutputstream.deflated);

// Set compression method

Zipos.putNextentry (New Zipntry ("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 the ZIP output stream construction DataOutputStream;

Os.Writeutf (DOC); // Write the random data to the file

Os.close (); // Turn off the data stream

Doczipsize = f.length (); // Get 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 ("Uncompessed Size:" Doc.Length ()

"Compressed Size:" Doczips);

}

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 program

}

Else {system.out.println ("no this command!");

}

}

Program operation results

转载请注明原文地址:https://www.9cbs.com/read-865.html

New Post(0)