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<Void, Void>() { @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<Void, String>() { @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<String> chunks) {
for (String chunk : chunks) {
filecomBox.addItem(chunk);
} } }.execute(); } }

ZipDemo的更多相关文章

  1. Zip压缩和解压缩

    这个功能完全依靠一个第三方的类,ICSharpCode.SharpZipLib.dll,只是在网上搜了大半天,都没有关于这个类的详细解释,搜索的demo也是各种错误,感觉作者完全没有跑过,就那么贸贸然 ...

  2. Java zip and unzip demo

    目录结构如下: import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import ...

  3. Python:字符串

    一.序列的概念 序列是容器类型,顾名思义,可以想象,“成员”们站成了有序的队列,我们从0开始进行对每个成员进行标记,0,1,2,3,...,这样,便可以通过下标访问序列的一个或几个成员,就像C语言中的 ...

  4. java 多个文件打包zip

    /** * 多个文件打包成zip */ public class ZipDemo { private static void create() throws Exception{ String pat ...

  5. Zip4J最简单用法

    package com.chentao.MicroMessage.bussiness; import java.io.File; import java.util.ArrayList; import ...

  6. java8_api_misc

    属性文件处理    概念    加载并读取文件内容    修改文件内容    获取系统属性        该文件是一个文本文件,以properties作为其后缀,内容格式为    key1=value ...

  7. 【大数据】Scala学习笔记

    第 1 章 scala的概述1 1.1 学习sdala的原因 1 1.2 Scala语言诞生小故事 1 1.3 Scala 和 Java  以及 jvm 的关系分析图 2 1.4 Scala语言的特点 ...

  8. C# 压缩文件 的创建

    using System;using System.IO.Compression; using System.Collections.Generic;using System.Linq;using S ...

  9. 使用JDK的zip编写打包工具类

    JDK自带的zip AIP在java.util.zip包下面,主要有以下几个类: java.util.zip.ZipEntryjava.util.zip.ZipInputStreamjava.util ...

随机推荐

  1. redis作为mysql的缓存服务器(读写分离) (转)

    一.redis简介Redis是一个key-value存储系统.和Memcached类似,为了保证效率,数据都是缓存在内存中.区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录 ...

  2. MySQLdb 连接Mysql 数据库出错解决

      #coding=utf-8 import MySQLdb if __name__ == "__main__": db = MySQLdb.connect(host=<sp ...

  3. 每天一个JavaScript实例-html5拖拽

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  4. Java如何检查List<String> 里是否有想要的字符串?

    List<String> test = new ArrayList<String>(); test.add("a"); test.add("b&q ...

  5. 大约 Apple Metal API 一些想法

    看后 Metal 的开发文档后,除了官方所宣称的一些长处外(比方说更easy理解和使用的 API.更直接和精细的硬件控制,降低 GPU 使用过程中的 CPU 额外开销等等),从我有限的 GLES 开发 ...

  6. mysql主键设置成auto_increment时,进行并发性能測试出现主键反复Duplicate entry &#39;xxx&#39; for key &#39;PRIMARY&#39;

    mysql主键设置成auto_increment时,进行并发性能測试出现主键反复Duplicate entry 'xxx' for key 'PRIMARY' 解决方法: 在my.cnf的[mysql ...

  7. 设计模式六大原则(4):接口隔离原则(Interface Segregation Principle)

    接口隔离原则: 使用多个专门的接口比使用单一的总接口要好. 一个类对另外一个类的依赖性应当是建立在最小的接口上的. 一个接口代表一个角色,不应当将不同的角色都交给一个接口.没有关系的接口合并在一起,形 ...

  8. SQL Server 2005使用OSQL连接出错

    错误信息: [SQL Native Client] 命名管道提供程序:无法打开与 Sql Server 的连接[2]. 如下图: 解决方案: 设置Tcp/IP属性,将IP1,IP2,IPALL的TCP ...

  9. 如何在 Windows Phone 8 中获取手机的当前位置

    原文 如何在 Windows Phone 8 中获取手机的当前位置 适用于:仅限于 Windows Phone 8. 本主题演示如何使用 Windows Phone 位置 API 确定手机的当前位置. ...

  10. Linux应用环境实战05:在Ubuntu 14.10中借用Windows的字体 (转)

    阅读目录 设置系统字体 安装微软的英文字体 查看系统的配置文件 借用Windows的字体 编写配置文件 在前一篇随笔中,我详细讨论了字体的分类及用途,也以Fedora 20为例,展示了字体配置的思路和 ...