JAVA basics: Implement zip compression and decompression using Java
Source: Internet
Author: User
JAVA basics: use Java to implement zip compression and decompression-Linux general technology-Linux programming and kernel information. The following is a detailed description. Due to limited network bandwidth, Data File compression is conducive to fast data transmission over the Internet, while also saving the server's external storage space. Java 1.1 implements a single interface between I/O data streams and network data streams. Therefore, data compression, network transmission, and decompression are easier to implement, the following describes how to use the ZipEntry, ZipInputStream, and ZipOutputStream Java classes to compress zip data.
Zip compression file structure: a zip file consists of multiple entries, each entry has a unique name, And the 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 implements the write and output stream of zip compressed files. It supports compression and non-compression.
Public ZipOutputStream (OutputStream out );
The producer uses the output stream out to construct a ZIP output stream.
Public void setMethod (int method );
Delimiter sets the entry compression method. The default value is DEFLATED.
Public void putNextEntry (ZipEntry newe );
If the current entry exists and is active, disable it in the zip file
Write a new entry-newe
The data stream is located at the starting position of the entry data item. The compression method is setMethod
Method.
· Class ZipInputStream
ZipInputStream implements the read and input stream of zip compressed files. It supports compression and non-compression.
Compress the entry. Below is its
Several functions:
Public ZipInputStream (InputStream in );
Pipeline uses the input stream in to construct a ZIP output stream.
Public ZipEntry getNextEntry ();
The cursor returns the next entry in the ZIP file and locates the output stream at the starting position of the entry data item.
Public void closeEntry ();
∥ Closes the current zip entry and positions the data stream at the starting position of the next entry.
Program code and comments
The following program implements the zip compression and decompression methods for data files. The randomData () function randomly generates 50 double data records and stores them in the doc string variable. The openFile () function reads the ZIP compressed file and saveFile () the function saves randomly generated data to a compressed file in 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; multiple lines of text display fields in the data file
TextField infotip; Compressed: displays the uncompressed data file size and the compressed size.
Line text display field
String doc; random stores randomly generated data
Long doczipsize = 0; Bytes: the size of the compressed data file
Public TestZip (){
Renewal 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 );
Multi-line text display field of the randomly generated data file
Add ("Center", textarea = new TextArea ());
Bytes indicates the single-line text display fields of the original text size and compressed size.
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 (){
The token randomly generates 50 double data records and stores them 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 file content into the doc string variable.
FileDialog dlg = new
FileDialog (this, "Open", FileDialog. loa d );
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.