Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Java to realize zip Compression and decompression

2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Xiaobian to share with you how to use Java to achieve zip compression and decompression, I believe most people do not know how, so share this article for your reference, I hope you have a lot of harvest after reading this article, let us go to understand it together!

zip file structure: A zip file consists of multiple entries, each entry has a unique name,entry data items store compressed data.

Several Java classes related to zip files

·ZipEntry

public ZipEntry(String name);

name is the name of the specified data item.

·ZipOutputStream

ZipOutputStream implements the write output stream of compressed files, supporting compressed and non-zip

public ZipOutputStream(OutputStream out);

Construct a ZIP output stream using the output stream out.

public void setMethod(int method);

Set entry compression method, default is DEFLATED.

public void putNextEntry(ZipEntry newe);

If the current entry exists and is active, close it in the zip file

Write new entry-newe in

and locate the data stream at the beginning of the entry data item, the compression method is setMethod

determined method.

·ZipInputStream

ZipInputStream implements a read input stream of compressed files, supporting compressed and non-zip

Compress entry. Below is his.

Several functions:

public ZipInputStream(InputStream in);

Construct a ZIP output stream from the input stream in.

public ZipEntry getNextEntry();

Returns the next entry in the ZIP file and positions the output stream at the beginning of this entry data item.

public void closeEntry();

Close the current zip entry and position the data stream at the beginning of the next entry.

Program code and its comments

The following program implements the zip compression and decompression method for data files. randomData() function randomly generates 50 double data and puts them in doc string variable;openFile() function reads ZIP compressed file;saveFile() function saves randomly generated data into ZIP 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; Multiline text display field for displaying data files

TextField infotip; Displays uncompressed and compressed size lists for data files

line text display field

String doc; Stores randomly generated data

long doczipsize = 0; Size of compressed data file

public TestZip(){

Create 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 for randomly generated data files

add("Center",textarea = new TextArea());

Tip Text Original size, compressed size, single line text display field

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 doubles and place them in the doc string variable.

doc="";

for(int i=1;i

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report