Choose from
AECLIPSER's blog
Since the network bandwidth is limited, the compression of the data file is conducive to the quick transfer of the data on the Internet, and also saves the extent of the server. Java 1.1 implements a single interface for I / O data streams and network data streams, so data compression, network transmission and decompression implementation is relatively easy, and the three Java classes that use ZIPENTRY, ZipinputStream and ZipOutputStream implement Zip data compression mode programming method. ZIP Compressed File Structure: A zip file consists of multiple Entry, each entry has a unique name, and Entry data item stores compressed data. Several Java classes related to ZIP files (String Name); Name is the specified data item name. • The zipoutputstream zipoutputStream implements the write output stream of ZIP compressed files, supports compression and non-compressed Entry. Below is a few functions: public zipoutputstream (OutputStream out); ∥ Using the output stream OUT to construct a ZIP output stream. Public void setMethod (int method); ∥ Set the entry compression method, the default 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 set the data to the starting position of the Entry data item, compression method The method specified for SetMethod. • Class ZipinputStream ZipinputStream implements the read input stream of ZIP compressed files, support compression and non-compressed Entry. Below is a few functions: public zipinputstream (InputStream IN); ∥ Using the input stream in constructs a ZIP output stream. Public zipentry getnextentry (); Returns the next entry in the zip file, and position the output stream in the starting position of this Entry data item. Public void closeEntry (); Close the current Zip Entry and set the data to the starting position of the next Entry. The program code and the following programs implements the compression and decompression method of the data file ZIP mode. The randomData () function randomly generates 50 Double data and placed in the DOC string variable; the OpenFile () function reads the ZIP compressed file; the savefile () function will randomly generate data into the zip format compressed file.
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 data file 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 ) {∥ ∥ 菜 菜 m (); menu file = new menu ("file"; menuatchm new; menuitem new; menuItem neww = new menuItem ("newwww); newwW .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); ∥∥ Randomly generated data file multi-line text display domain Add ("center", TextArea = new textarea ()); ∥∥ Prompt the original size, compressed size single line text display domain add ("South", Infotip = new textfield ());} public static void main (String args ]) {TE Stzip ok = new testzip (); ok.settitle ("zip sample"); ok.setsize (600, 300); ok.show ();} private void randomData () {∥ Randomly generate 50 Double data, and put it in DOC String variables. 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.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, return ∥ ∥ ∥ 文件 输入 流 流 建 建 z 压 压 输入 (new fileInputstream (f)); zipis.getNextentry; zipis.getNextentry ); ∥ Positioning the input stream in the current ENTRY data item location DataInputStream DIS = New DataInputStream (zipis); ∥ ∥ z 输入 进行 流 流 建建 = (); ∥ Read file content Dis.close (); ∥ Close Document doczipsize = f.length (); ∥ Get zip file length showTextAndInfo (); ∥∥ Display data} catch (ieException ie) {system.out.println (IOE);}} private void savefile () {∥∥ Open zip file, 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, it returns the compressed output stream ∥ construct ZIP ZipOutputStream zipos = new ZipOutputStream (new FileOutputStream (f)) by the file output stream; zipos.setMethod (ZipOutputStream .Deflated); ∥ Set compression method zipos.putNextentry (New ZiPentry ("zip")));