zoukankan      html  css  js  c++  java
  • ZipDemo

    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("
    ");
                            }
                        }
                        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();
    
        }
    
    }
  • 相关阅读:
    Linux下的SVN服务器搭建
    [转][osg]关于PagedLOD 加载卸载机制
    [原][osg]osg文件与osgb文件的区别
    [转][cesium]1.添加本地服务器
    [原][osg][osgearth]倾斜摄影2.文件格式分析:OSGB
    [原][数学][C++][osg]空间向量OA到转到空间向量OB、以及四元素Q1转到Q2的函数
    [原][osgEarth]添加自由飞行漫游器
    [c][c++]按位操作
    [转]QT中QString与string的转化,解决中文乱码问题
    [原][osg][osgearth]倾斜摄影1.介绍
  • 原文地址:https://www.cnblogs.com/softidea/p/4629024.html
Copyright © 2011-2022 走看看