首页 新闻 会员 周边

用Java写了个解压缩zip文件的小工具,遇到个问题,被压缩的文件名有中文字符的话就会报错,求指点

0
悬赏园豆:5 [已解决问题] 解决于 2014-03-01 11:42

代码如下:

 

  1 import java.awt.EventQueue;
  2 import java.awt.event.ActionEvent;
  3 import java.awt.event.ActionListener;
  4 import java.io.File;
  5 import java.io.FileFilter;
  6 import java.io.FileInputStream;
  7 import java.util.zip.ZipEntry;
  8 import java.util.zip.ZipInputStream;
  9 
 10 import javax.swing.JButton;
 11 import javax.swing.JFileChooser;
 12 import javax.swing.JFrame;
 13 import javax.swing.JLabel;
 14 import javax.swing.JOptionPane;
 15 import javax.swing.JPanel;
 16 import javax.swing.JScrollPane;
 17 import javax.swing.JSeparator;
 18 import javax.swing.JSpinner;
 19 import javax.swing.JTable;
 20 import javax.swing.JTextField;
 21 import javax.swing.border.EmptyBorder;
 22 import javax.swing.table.DefaultTableModel;
 23 
 24 import java.awt.GridBagLayout;
 25 import java.awt.GridBagConstraints;
 26 import java.awt.Insets;
 27 
 28 /**
 29  * 获取文件列表的过滤器
 30  * 
 31  * @author 李钟尉
 32  */
 33 public class UnZipTextFileFrame extends JFrame {
 34     private JPanel contentPane;
 35     private JTextField forderField;
 36     private JTextField templetField;
 37     private File file;
 38     private File dir;
 39     private JTable table;
 40     private JTextField extNameField;
 41     private JSpinner startSpinner;
 42     private JTextField textField;
 43     private JTextField textField_1;
 44     private DefaultTableModel model;
 45     private String filesrc;
 46     /**
 47      * Launch the application.
 48      */
 49     public static void main(String[] args) {
 50         EventQueue.invokeLater(new Runnable() {
 51             public void run() {
 52                 try {
 53                     UnZipTextFileFrame frame = new UnZipTextFileFrame();
 54                     frame.setVisible(true);
 55                 } catch (Exception e) {
 56                     e.printStackTrace();
 57                 }
 58             }
 59         });
 60     }
 61     
 62     /**
 63      * Create the frame.
 64      */
 65     public UnZipTextFileFrame() {
 66         setResizable(false);
 67         setTitle("压缩包解压到指定文件夹");
 68         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 69         setBounds(100, 100, 500, 300);
 70         getContentPane().setLayout(null);
 71         
 72         textField = new JTextField();
 73         textField.setBounds(10, 10, 158, 21);
 74         getContentPane().add(textField);
 75         textField.setColumns(10);
 76         
 77         JButton btnZip = new JButton("Zip\u6587\u4EF6");
 78         btnZip.addActionListener(new ActionListener() {
 79             public void actionPerformed(ActionEvent e) {
 80                 do_btnZip_actionPerformed(e);
 81             }
 82         });
 83         btnZip.setBounds(178, 9, 93, 23);
 84         getContentPane().add(btnZip);
 85         
 86         textField_1 = new JTextField();
 87         textField_1.setBounds(281, 10, 100, 21);
 88         getContentPane().add(textField_1);
 89         textField_1.setColumns(10);
 90         
 91         JButton btnNewButton = new JButton("解压到");
 92         btnNewButton.addActionListener(new ActionListener() {
 93             public void actionPerformed(ActionEvent e) {
 94                 do_btnNewButton_actionPerformed(e);
 95             }
 96         });
 97         btnNewButton.setBounds(391, 9, 93, 23);
 98         getContentPane().add(btnNewButton);
 99         
100         JScrollPane scrollPane = new JScrollPane();
101         scrollPane.setBounds(10, 41, 474, 170);
102         getContentPane().add(scrollPane);
103         
104         table = new JTable();
105         scrollPane.setViewportView(table);
106         model= (DefaultTableModel) table.getModel();
107         model.setColumnIdentifiers(new Object[] { "序号", "文件名"});
108         JButton button = new JButton("\u5F00\u59CB\u89E3\u538B\u7F29");
109         button.addActionListener(new ActionListener() {
110             public void actionPerformed(ActionEvent e) {
111                 do_button_actionPerformed(e);
112             }
113         });
114         button.setBounds(178, 221, 100, 23);
115         getContentPane().add(button);
116         
117     }
118     protected void do_btnZip_actionPerformed(ActionEvent e){
119         JFileChooser chooser = new JFileChooser();// 创建文件选择器
120         int option = chooser.showOpenDialog(this);// 显示文件打开对话框
121         if (option == JFileChooser.APPROVE_OPTION) {
122             file = chooser.getSelectedFile();// 获取选择的文件数组
123             filesrc=file.getAbsolutePath();
124             textField.setText(filesrc);// 清空文本框
125         } else {
126             textField.setText("");// 清空文本框
127         }
128     }
129     protected void do_btnNewButton_actionPerformed(ActionEvent e){
130         JFileChooser chooser = new JFileChooser();// 创建文件选择器
131         // 设置选择器只针对文件夹生效
132         chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
133         int option = chooser.showOpenDialog(this);// 显示文件打开对话框
134         if (option == JFileChooser.APPROVE_OPTION) {
135             dir = chooser.getSelectedFile();// 获取选择的文件夹
136             textField_1.setText(dir.toString());// 显示文件夹到文本框
137         } else {
138             dir = null;
139             textField_1.setText("");
140         }
141     }
142     protected void do_button_actionPerformed(ActionEvent e){
143         ZipInputStream zin;        
144         try{
145             zin=new ZipInputStream(new FileInputStream(filesrc));
146             ZipEntry entry;
147             int i=1;
148             while(((entry=zin.getNextEntry())!=null)&&!entry.isDirectory()){
149                 File file=new File(dir.toString()+"/"+entry.getName());
150                 if(!file.exists()){
151                     file.createNewFile();
152                 }
153                 zin.closeEntry();
154                 Object[] property = new Object[2];
155                 property[0] = i;
156                 property[1] = entry.getName();
157                 model.addRow(property);
158                 i++;
159             }
160         }catch(Exception ex){
161             ex.printStackTrace();
162         }        
163     }
164 }
源子陌的主页 源子陌 | 初学一级 | 园豆:6
提问于:2014-02-28 20:36
< >
分享
最佳答案
0

不支持中文,你可以google有人已经借用库来实现中文解压了。

收获园豆:5
Beyond-bit | 老鸟四级 |园豆:2885 | 2014-03-01 10:19

谢谢,在http://zwllxs.iteye.com/blog/871260找到解决方案了

源子陌 | 园豆:6 (初学一级) | 2014-03-01 11:42
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册