做一個多層資料夾壓縮包的釋放的工具。
建立一個類別: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中文網其他相關文章!