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

1 importjava.awt.EventQueue;2 importjava.awt.event.ActionEvent;3 importjava.awt.event.ActionListener;4 importjava.io.File;5 importjava.io.FileFilter;6 importjava.io.FileInputStream;7 importjava.util.zip.ZipEntry;8 importjava.util.zip.ZipInputStream;9

10 importjavax.swing.JButton;11 importjavax.swing.JFileChooser;12 importjavax.swing.JFrame;13 importjavax.swing.JLabel;14 importjavax.swing.JOptionPane;15 importjavax.swing.JPanel;16 importjavax.swing.JScrollPane;17 importjavax.swing.JSeparator;18 importjavax.swing.JSpinner;19 importjavax.swing.JTable;20 importjavax.swing.JTextField;21 importjavax.swing.border.EmptyBorder;22 importjavax.swing.table.DefaultTableModel;23

24 importjava.awt.GridBagLayout;25 importjava.awt.GridBagConstraints;26 importjava.awt.Insets;27

28 /**

29 * 获取文件列表的过滤器30 *31 *@author李钟尉32 */

33 public class UnZipTextFileFrame extendsJFrame {34 privateJPanel contentPane;35 privateJTextField forderField;36 privateJTextField templetField;37 privateFile file;38 privateFile dir;39 privateJTable table;40 privateJTextField extNameField;41 privateJSpinner startSpinner;42 privateJTextField textField;43 privateJTextField textField_1;44 privateDefaultTableModel model;45 privateString filesrc;46 /**

47 * Launch the application.48 */

49 public static voidmain(String[] args) {50 EventQueue.invokeLater(newRunnable() {51 public voidrun() {52 try{53 UnZipTextFileFrame frame = newUnZipTextFileFrame();54 frame.setVisible(true);55 } catch(Exception e) {56 e.printStackTrace();57 }58 }59 });60 }61

62 /**

63 * Create the frame.64 */

65 publicUnZipTextFileFrame() {66 setResizable(false);67 setTitle("压缩包解压到指定文件夹");68 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);69 setBounds(100, 100, 500, 300);70 getContentPane().setLayout(null);71

72 textField = newJTextField();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(newActionListener() {79 public voidactionPerformed(ActionEvent e) {80 do_btnZip_actionPerformed(e);81 }82 });83 btnZip.setBounds(178, 9, 93, 23);84 getContentPane().add(btnZip);85

86 textField_1 = newJTextField();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(newActionListener() {93 public voidactionPerformed(ActionEvent e) {94 do_btnNewButton_actionPerformed(e);95 }96 });97 btnNewButton.setBounds(391, 9, 93, 23);98 getContentPane().add(btnNewButton);99

100 JScrollPane scrollPane = newJScrollPane();101 scrollPane.setBounds(10, 41, 474, 170);102 getContentPane().add(scrollPane);103

104 table = newJTable();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(newActionListener() {110 public voidactionPerformed(ActionEvent e) {111 do_button_actionPerformed(e);112 }113 });114 button.setBounds(178, 221, 100, 23);115 getContentPane().add(button);116

117 }118 protected voiddo_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 voiddo_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 voiddo_button_actionPerformed(ActionEvent e){143 ZipInputStream zin;144 try{145 zin=new ZipInputStream(newFileInputStream(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 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值