Make a tool for releasing multi-layer folder compression packages.
Create a class: UnZipDirectoryFrame
Use UnZipDirectoryFrame to inherit JFrame to build a form
There will be multiple folders in the compressed package , each folder may have folders or files, in order to restore the folder hierarchical relationship when decompressing.
The scheme of decompressing a folder containing subfolders is similar to that of decompressing a folder containing all files. The difference lies in how to find all the files in the folder containing subfolders, and when constructing ZipEntry, do not have duplicates. name situation.
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
Decompression successful:
The above is the detailed content of How to use Java to implement multi-layer folder compression function. For more information, please follow other related articles on the PHP Chinese website!