溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么使用Java實(shí)現(xiàn)多層文件夾壓縮功能

發(fā)布時(shí)間:2022-08-13 10:15:29 來源:億速云 閱讀:389 作者:iii 欄目:開發(fā)技術(shù)

這篇“怎么使用Java實(shí)現(xiàn)多層文件夾壓縮功能”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“怎么使用Java實(shí)現(xiàn)多層文件夾壓縮功能”文章吧。

壓縮文件相關(guān)技術(shù)

1.題目

做一個(gè)多層文件夾壓縮包的釋放的工具。

2.解題思路

創(chuàng)建一個(gè)類:UnZipDirectoryFrame

使用UnZipDirectoryFrame繼承JFrame構(gòu)建窗體

壓縮包內(nèi)會(huì)有多個(gè)文件夾,每個(gè)文件夾可能會(huì)有文件夾或是文件,為了解壓縮時(shí)能還原出文件夾的層次關(guān)系。

解壓縮包含子文件夾的文件夾方案和解壓縮全是文件的文件夾類似,區(qū)別在于如何找出包含子文件夾的文件夾的所有文件,并且構(gòu)造ZipEntry時(shí),不要有重名的情況。

3.代碼詳解

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>
 * 修改記錄:
 * 修改后版本	        修改人		修改日期			修改內(nèi)容
 * 2022/5/4.1	    xiaoxuzhu		2022/5/4		    Create
 * </pre>
 * @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[] { "序號(hào)", "文件名" });
        List<String> list = new ArrayList<String>();
        try {
            unzip(zipFile, list);
            for (int i = 0; i < list.size(); i++) {
                model.addRow(new Object[] { i + 1, list.get(i) });
            }
            table.setModel(model);
            JOptionPane.showMessageDialog(this, "解壓縮完成");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void unzip(File zipFile, List<String> list) throws IOException {
        // 利用用戶選擇的ZIP文件創(chuàng)建ZipInputStream對象
        ZipInputStream in = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry entry;
        while ((entry = in.getNextEntry()) != null) {// 遍歷所有ZipEntry對象
            if (!entry.isDirectory()) {// 如果是文件則創(chuàng)建并寫入
                File tempFile = new File(zipFile.getParent() + File.separator + entry.getName());
                list.add(tempFile.getName());// 增加文件名
                new File(tempFile.getParent()).mkdirs();// 創(chuàng)建文件夾
                tempFile.createNewFile();// 創(chuàng)建新文件
                FileOutputStream out = new FileOutputStream(tempFile);
                int b;
                while ((b = in.read()) != -1) {// 寫入數(shù)據(jù)
                    out.write(b);
                }
                out.close();// 釋放資源
            }
        }
        in.close();
    }
}

怎么使用Java實(shí)現(xiàn)多層文件夾壓縮功能

解壓縮成功:

怎么使用Java實(shí)現(xiàn)多層文件夾壓縮功能

以上就是關(guān)于“怎么使用Java實(shí)現(xiàn)多層文件夾壓縮功能”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI