Three use Java to implement ZIP compressiondecompression method

xiaoxiao2021-03-06  53

Three use Java to implement ZIP compression / decompression method

2004-11-25 13:56:17

9cbs

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 stream and network data stream, so data compression, network transmission and solution

The implementation of compression is relatively easy, and the three Java classes using ZIPENTRY, ZipinputStream and ZipOutputStream implement Zip data compression mode. 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 · Class ZIPENTRY

Public zipentry (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); ∥ Take a ZIP output stream using the input stream in. 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 ) {∥ generated menu MenuBar menubar = new menubar (); setmenubar; menu file = new menu ("file", true);

MenuBar.Add (file); menuitem neww = new menuItem ("new"); neww.addActionli

Stener (this); file.add (neww); menuitem open = new menuItem ("open"); Open.AddActionListener (this); File.Add (Open); MenuItem Save = New MenuItem ("save"); Save. AddaStener (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 ()); ∥∥ Tips text original size, compressed size single line text display domain add ("South", infotip = new textfield ());} public static void main (String args [] ) {Testzip ok = new testzip (); "zip sample"); ok.setsize (600, 300); ok.show ();} private void randomData () {∥ Randomly generate 50 Double data, and put 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.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. 9cbs

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"))); DataoutputStream OS = New DataOutputStream (zipos); ∥ os z 输 输 输 (); os.Writeutf (DOC); ∥∥ Write randomly generated data into files os.close (); ∥ Close data stream doczipsize = f.Length ); ∥ Get the length showTextandInfo () of the compressed file; ∥∥ Display data} Catch (ieException ie) {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 programs} else {system.out.println ("no this command! ");}}}

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

New Post(0)