博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ZipDemo
阅读量:6150 次
发布时间:2019-06-21

本文共 4629 字,大约阅读时间需要 15 分钟。

 

 

package swing.zip;import java.awt.BorderLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import java.io.FileInputStream;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.SwingUtilities;import javax.swing.SwingWorker;/*2015-7-8*/public class ZipTest {    public static void main(String[] args) {        SwingUtilities.invokeLater(new Runnable() {            @Override            public void run() {                ZipTestFrame frame = new ZipTestFrame();                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                frame.setVisible(true);                frame.setLocationRelativeTo(null);            }        });    }}class ZipTestFrame extends JFrame {    private static final long serialVersionUID = 1L;    public static final int DEFAULT_WIDTH = 400;    public static final int DEFAULT_HEIGHT = 400;    private JComboBox filecomBox;    private JTextArea fileText;    private String zipName;    public ZipTestFrame() {        setTitle("ZipTest");        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);        JMenuBar menuBar = new JMenuBar();        JMenu menu = new JMenu("File");        JMenuItem openItem = new JMenuItem("Open");        menu.add(openItem);        openItem.addActionListener(new ActionListener() {            @Override            public void actionPerformed(ActionEvent e) {                JFileChooser chooser = new JFileChooser();                chooser.setCurrentDirectory(new File("."));                int r = chooser.showOpenDialog(ZipTestFrame.this);                if (r == JFileChooser.APPROVE_OPTION) {                    zipName = chooser.getSelectedFile().getPath();                    filecomBox.removeAllItems();                    scanZipFile();                }            }        });        JMenuItem exitItem = new JMenuItem("Exit");        menu.add(exitItem);        exitItem.addActionListener(new ActionListener() {            @Override            public void actionPerformed(ActionEvent e) {                System.exit(0);            }        });        menuBar.add(menu);        setJMenuBar(menuBar);        fileText = new JTextArea();        filecomBox = new JComboBox();        filecomBox.addActionListener(new ActionListener() {            @Override            public void actionPerformed(ActionEvent e) {                loadZipFile((String) filecomBox.getSelectedItem());            }        });        add(filecomBox, BorderLayout.SOUTH);        add(new JScrollPane(fileText), BorderLayout.CENTER);    }    protected void loadZipFile(final String name) {        filecomBox.setEnabled(false);        fileText.setText("");        new SwingWorker
() { @Override protected Void doInBackground() throws Exception { ZipInputStream zin = new ZipInputStream(new FileInputStream(zipName)); 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(); return null; } @Override protected void done() { filecomBox.setEnabled(true); } }.execute(); } protected void scanZipFile() { new SwingWorker
() { @Override protected Void doInBackground() throws Exception { ZipInputStream zin = new ZipInputStream(new FileInputStream(zipName)); ZipEntry entry; while ((entry = zin.getNextEntry()) != null) { publish(entry.getName()); zin.closeEntry(); } zin.close(); return null; } @Override protected void process(List
chunks) { for (String chunk : chunks) { filecomBox.addItem(chunk); } } }.execute(); }}

 

转载于:https://www.cnblogs.com/softidea/p/4629024.html

你可能感兴趣的文章
安装Microsoft Dynamics CRM 2011时出现“Microsoft.Crm.Setup.Common.Analyzer+CollectAction 操作失败”的解决办法...
查看>>
js异步编程终级解决方案 async/await
查看>>
Android Studio 更新
查看>>
让urllib2的DNS亦通过Proxy查询
查看>>
transient和synchronized的使用
查看>>
Hello World
查看>>
277 div2 C Palindrome Transformation
查看>>
How to view file history in Git?
查看>>
Broadcast的动态注册与静态注册和Notification的使用
查看>>
VMware 设置桥接方式 本机使用WLAN
查看>>
[Asp.Net]获取客户端ip和mac地址
查看>>
selenium抓取动态网页数据
查看>>
Far Relative’s Problem (贪心 计算来的最多客人)
查看>>
【转】ASP.NET MVC教程
查看>>
多晶硅进口量飙升 明年供应或现过剩
查看>>
[转载] C#面向对象设计模式纵横谈——14 Template Method模板方法
查看>>
经典SQL语句大全
查看>>
String
查看>>
HDU 5536 字典树
查看>>
iOS Developer TODO
查看>>