java 加压zip_Java中用原生ZipInputStream压缩加压zip文件

这个博客展示了如何使用Java的ZipInputStream类来压缩和解压ZIP文件。通过创建一个Swing应用,用户可以选择ZIP文件,并从下拉列表中选择文件进行查看。当选定ZIP条目时,内容会在文本区域中显示。
摘要由CSDN通过智能技术生成

3105794893043230274.jpg

package com.xudeyu.stream;

import java.awt.BorderLayout;

import java.awt.EventQueue;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.util.List;

import java.util.Scanner;

import java.util.zip.ZipEntry;

import java.util.zip.ZipInputStream;

import javax.swing.JComboBox;

import javax.swing.JFileChooser;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import javax.swing.SwingWorker;

public class ZipInput

{

public static void main(String[] args)

{

EventQueue.invokeLater(new Runnable()

{

@Override

public void run()

{

ZipFileTest zt = new ZipFileTest();

zt.setDefaultCloseOperation

(JFrame.EXIT_ON_CLOSE);

zt.setVisible(true);

}

});

}

}

class ZipFileTest extends JFrame

{

private int DEFAULT_WIDTH = 400;

private int DEFAULT_HEIGHT = 500;

private String zipFileName;

private JComboBox fileComb;

private JTextArea fileText;

public ZipFileTest()

{

setTitle("Zip file test");

setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

JMenuBar menuBar = new JMenuBar();

JMenu menuFile = new JMenu("File");

// Open Item add to FileMenu

JMenuItem openItem = new JMenuItem("Open");

menuFile.add(openItem);

// if chooser a file ,add all file name in combox witch in thezip;

openItem.addActionListener(new ActionListener()

{

@Override

public void actionPerformed(ActionEvent e)

{

JFileChooser chooser = new JFileChooser(new File("."));

int r = chooser.showOpenDialog(null);

if (r == JFileChooser.APPROVE_OPTION)

{

zipFileName = chooser.getSelectedFile().getPath();

fileComb.removeAllItems();

scanZipFile();

}

}

});

// exit Item add to FileMenu

JMenuItem exitItem = new JMenuItem("exitt");

menuFile.add(exitItem);

// exit if on click

exitItem.addActionListener(new ActionListener()

{

@Override

public void actionPerformed(ActionEvent e)

{

System.exit(0);

}

});

// add the file menu on the menu bar;

menuBar.add(menuFile);

setJMenuBar(menuBar);

fileText = new JTextArea();

fileComb = new JComboBox();

// when selecet in combox ,output a file info in textAreawitch in zip

fileComb.addActionListener(new ActionListener()

{

@Override

public void actionPerformed(ActionEvent e)

{

loadZipFile((String) fileComb.getSelectedItem());

}

});

// add two complent on frame

add(fileComb, BorderLayout.SOUTH);

add(new JScrollPane(fileText), BorderLayout.CENTER);

}

public void scanZipFile()

{

// 执行后台计算操作

new SwingWorker()

{

@Override

protected Void doInBackground() throws Exception

{

ZipInputStream zin = new ZipInputStream(

new FileInputStream(zipFileName));

ZipEntry entry;

while ((entry = zin.getNextEntry()) != null)

{

// see api publish,every publish ,push it to process;

publish(entry.getName());

zin.closeEntry();// only close can read next entry

}

zin.close();

return null;

}

@Override

protected voidprocess(List names)

{

for (String name : names)

{

fileComb.addItem(name);

}

}

}.execute();

}

public void loadZipFile(final String name)

{

fileComb.setEditable(false);

fileText.setText("");

new SwingWorker()

{

@Override

protected Void doInBackground() throws Exception

{

try

{

ZipInputStream zin = new ZipInputStream(

new FileInputStream(zipFileName));

ZipEntry entry;

while ((entry = zin.getNextEntry()) != null)

{

if (entry.getName().equals(name))

{

Scanner in = new Scanner(zin);

while (in.hasNextLine())

{

fileText.append(in.nextLine());

fileText.append("\n");

}

}

zin.closeEntry();

}

zin.close();

}

catch (IOException e)

{

e.printStackTrace();

}

return null;

}

@Override

protected void done()

{

fileComb.setEditable(true);

}

}.execute();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值