做一个多层文件夹压缩包的释放的工具。
创建一个类:UnZipDirectoryFrame
使用UnZipDirectoryFrame继承JFrame构建窗体
压缩包内会有多个文件夹,每个文件夹可能会有文件夹或是文件,为了解压缩时能还原出文件夹的层次关系。
解压缩包含子文件夹的文件夹方案和解压缩全是文件的文件夹类似,区别在于如何找出包含子文件夹的文件夹的所有文件,并且构造ZipEntry时,不要有重名的情况。
package com.xiaoxuzhu; 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.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.border.EmptyBorder; import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.table.DefaultTableModel; /** * Description: 多层文件夹压缩包的释放 * * @author xiaoxuzhu * @version 1.0 * * <pre class="brush:php;toolbar:false"> * 修改记录: * 修改后版本 修改人 修改日期 修改内容 * 2022/5/4.1 xiaoxuzhu 2022/5/4 Create ** @date 2022/5/4 */ public class UnZipDirectoryFrame extends JFrame { /** * */ private static final long serialVersionUID = 7178478435446172846L; private JPanel contentPane; private JTextField chooseTextField; private JTable table; private File zipFile; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { UnZipDirectoryFrame frame = new UnZipDirectoryFrame(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public UnZipDirectoryFrame() { setTitle("多层文件夹压缩包的释放"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(new BorderLayout(0, 0)); JPanel choosePanel = new JPanel(); contentPane.add(choosePanel, BorderLayout.NORTH); chooseTextField = new JTextField(); choosePanel.add(chooseTextField); chooseTextField.setColumns(18); JButton chooseButton = new JButton("选择压缩文件"); chooseButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { do_chooseButton_actionPerformed(arg0); } }); choosePanel.add(chooseButton); JPanel buttonPanel = new JPanel(); contentPane.add(buttonPanel, BorderLayout.SOUTH); JButton unzipButton = new JButton("开始解压缩"); unzipButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { do_unzipButton_actionPerformed(arg0); } }); buttonPanel.add(unzipButton); JScrollPane scrollPane = new JScrollPane(); contentPane.add(scrollPane, BorderLayout.CENTER); table = new JTable(); scrollPane.setViewportView(table); } protected void do_chooseButton_actionPerformed(ActionEvent arg0) { JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileFilter(new FileNameExtensionFilter("文本文件", "zip")); fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); int result = fileChooser.showOpenDialog(this); if (result == JFileChooser.APPROVE_OPTION) { zipFile = fileChooser.getSelectedFile(); chooseTextField.setText(zipFile.getAbsolutePath()); } } protected void do_unzipButton_actionPerformed(ActionEvent arg0) { DefaultTableModel model = (DefaultTableModel) table.getModel(); model.setColumnIdentifiers(new Object[] { "序号", "文件名" }); List
解压缩成功:
以上是怎么使用Java实现多层文件夹压缩功能的详细内容。更多信息请关注PHP中文网其他相关文章!