ZipInputStream的用法
package com.example.io; 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.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 ZipTest {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() { @Override
public void run() {
ZipTestFrame frame = new ZipTestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
} class ZipTestFrame extends JFrame { 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();
fileCombo.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();
fileCombo = new JComboBox();
fileCombo.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) {
loadZipFile((String) fileCombo.getSelectedItem());
}
}); add(fileCombo, BorderLayout.SOUTH);
add(new JScrollPane(fileText), BorderLayout.CENTER);
} public void scanZipFile() {
new SwingWorker<Void, String> () { @Override
protected void process(List<String> names) {
for (String name : names) {
fileCombo.addItem(name);
}
} @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;
}
}.execute();
} public void loadZipFile(final String name) {
fileCombo.setEnabled(false);
fileText.setText("");
new SwingWorker<Void, Void>() { @Override
protected void done() {
fileCombo.setEnabled(true);
} @Override
protected Void doInBackground() throws Exception {
try {
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();
} catch (Exception e) {
e.printStackTrace();
}
return null;
} }.execute();
} public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGHT = 300;
private JComboBox fileCombo;
private JTextArea fileText;
private String zipname;
}
ZipInputStream的用法的更多相关文章
- Java基础-IO流对象之压缩流(ZipOutputStream)与解压缩流(ZipInputStream)
Java基础-IO流对象之压缩流(ZipOutputStream)与解压缩流(ZipInputStream) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 之前我已经分享过很多的J ...
- EditText 基本用法
title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...
- jquery插件的用法之cookie 插件
一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...
- Java中的Socket的用法
Java中的Socket的用法 Java中的Socket分为普通的Socket和NioSocket. 普通Socket的用法 Java中的 ...
- [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法
一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...
- python enumerate 用法
A new built-in function, enumerate() , will make certain loops a bit clearer. enumerate(thing) , whe ...
- [转载]Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结
本文对Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法进行了详细的总结,需要的朋友可以参考下,希望对大家有所帮助. 详细解读Jquery各Ajax函数: ...
- 【JavaScript】innerHTML、innerText和outerHTML的用法区别
用法: <div id="test"> <span style="color:red">test1</span> tes ...
- chattr用法
[root@localhost tmp]# umask 0022 一.chattr用法 1.创建空文件attrtest,然后删除,提示无法删除,因为有隐藏文件 [root@localhost tmp] ...
随机推荐
- redhat 6上nis配置
redhat 6上nis有点改动.在这里记一下 新装系统以后,首先要 yum install ypserv 安装ypserv的包 然后是设置 # 设置nis服务器名字 ypdomainname cen ...
- [转]mysql binlog in realtime
原文:http://guweigang.com/blog/2013/11/18/mysql-binlog-in-realtime/ 众所周知,MySQL是最受欢迎的互联网数据库(没有之一)—————— ...
- WPF学习开发客户端软件-任务助手(下 2015年2月4日代码更新)
时光如梭,距离第一次写的 WPF学习开发客户端软件-任务助手(已上传源码) 已有三个多月,期间我断断续续地对该项目做了优化.完善等等工作,现在重新向大家介绍一下,希望各位可以使用,本软件以实用性为主 ...
- python 守护进程 daemon
python 守护进程 daemon # -*-coding:utf-8-*- import sys, os '''将当前进程fork为一个守护进程 注意:如果你的守护进程是由inetd启动的,不要这 ...
- 通过Anuglar Material串串学客户端开发 - NodeJS模块机制之Module.Exports
module.exports 前文讲到在Angular Material的第二个编译文件docs/gulpfile.js中却看到了一个奇怪的东西module.exports那么module.expor ...
- [C++] socket -9[匿名管道]
::怎么弄都不能读取信息....先把代码放着.... #include<windows.h> #include<stdio.h> int main() { HANDLE rea ...
- Redis教程(六):Sorted-Sets数据类型
转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/133.html 一.概述: Sorted-Sets和Sets类型极为相似, ...
- GCD使用dispatch_semaphore_t创建多线程网络同步请求
一.简介: dispatch_semaphore_t:表示信号,生成信号的方法是 dispatch_semaphore_t semaphore= dispatch_semaphore_create(0 ...
- 【Android 】Service 全面总结
1.Service的种类 按运行地点分类: 类别 区别 优点 缺点 应用 本地服务(Local) 该服务依附在主进程上, 服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另外L ...
- paip.提升性能----硬盘不同转速不同缓存对比转
paip.提升性能----硬盘不同转速不同缓存对比转 西数 WDC WD2500BEVT-08A23T1 ( 250 GB / 5400 转/分 ) 5400转 8M缓存 单碟250 GB ...