• 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("
    ");
                                }
                            }
                            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;
    }
  • 相关阅读:
    Hibernate映射之数组属性, 集合属性(list、set和map)
    org.hibernate.type.SerializationException: could not deserialize 反序列化失败
    linux下同一个tomcat部署多个项目
    Nexus私服 Maven
    maven 父子项目 聚合与继承 生命周期 打包插件 依赖打包 jar pom war
    IDEA中创建Web聚合项目(Maven多模块项目)
    为什么要在linux命令前加上 ./ 什么时候才需要在命令前加上./
    web.xml 介绍 and pom.xml 介绍
    有趣的linux命令
    SpringMVC 中常见注释
  • 原文地址:https://www.cnblogs.com/qixin622/p/5215687.html
Copyright © 2020-2023  润新知