Home  >  Article  >  Java  >  How to use Java to implement multi-layer folder compression function

How to use Java to implement multi-layer folder compression function

PHPz
PHPzforward
2023-04-30 09:16:061754browse

Compressed file related technologies

1. Question

Make a tool for releasing multi-layer folder compression packages.

2. Problem-solving ideas

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.

3. Detailed code explanation

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 list = new ArrayList();         try {             unzip(zipFile, list);             for (int i = 0; i  list) throws IOException {         // 利用用户选择的ZIP文件创建ZipInputStream对象         ZipInputStream in = new ZipInputStream(new FileInputStream(zipFile));         ZipEntry entry;         while ((entry = in.getNextEntry()) != null) {// 遍历所有ZipEntry对象             if (!entry.isDirectory()) {// 如果是文件则创建并写入                 File tempFile = new File(zipFile.getParent() + File.separator + entry.getName());                 list.add(tempFile.getName());// 增加文件名                 new File(tempFile.getParent()).mkdirs();// 创建文件夹                 tempFile.createNewFile();// 创建新文件                 FileOutputStream out = new FileOutputStream(tempFile);                 int b;                 while ((b = in.read()) != -1) {// 写入数据                     out.write(b);                 }                 out.close();// 释放资源             }         }         in.close();     } }

How to use Java to implement multi-layer folder compression function

Decompression successful:

How to use Java to implement multi-layer folder compression function

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete