package com.mingrisoft;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.ZipOutputStream;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.table.DefaultTableModel;public class ZipDirectoryFrame extends JFrame { /** * */ private static final long serialVersionUID = -138842864977841594L; private JPanel contentPane; private JTextField chooseTextField; private JTable table; private File selectFile; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { ZipDirectoryFrame frame = new ZipDirectoryFrame(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public ZipDirectoryFrame() { setTitle("\u538B\u7F29\u6240\u6709\u5B50\u6587\u4EF6\u5939"); 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("\u9009\u62E9\u6587\u4EF6\u5939"); 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 zipButton = new JButton("\u5F00\u59CB\u538B\u7F29"); zipButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { do_zipButton_actionPerformed(arg0); } }); buttonPanel.add(zipButton); 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.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); fileChooser.setMultiSelectionEnabled(false); int result = fileChooser.showOpenDialog(this); if (result == JFileChooser.APPROVE_OPTION) { selectFile = fileChooser.getSelectedFile(); chooseTextField.setText(selectFile.getAbsolutePath()); } } protected void do_zipButton_actionPerformed(ActionEvent arg0) { List<String> path = new ArrayList<String>(); getPath(selectFile, path); DefaultTableModel model = (DefaultTableModel) table.getModel(); model.setColumnIdentifiers(new Object[] { "序号", "文件" }); int id = 1; for (String string : path) { model.addRow(new Object[] { id , new File(string).getName() }); } String targetZipFilePath = selectFile.getParent() selectFile.getName() ".zip"; try { zipFile(path, new File(targetZipFilePath), selectFile.getAbsolutePath()); JOptionPane.showMessageDialog(this, "文件夹压缩成功"); } catch (IOException e) { e.printStackTrace(); } } private static void getPath(File rootFile, List<String> path) { File[] files = rootFile.listFiles(); for (File file : files) { if (file.isDirectory()) { getPath(file, path); } else { path.add(file.getAbsolutePath()); } } } private void zipFile(List<String> path, File targetZipFile, String base) throws IOException { // 根据给定的targetZipFile创建文件输出流对象 FileOutputStream fos = new FileOutputStream(targetZipFile); ZipOutputStream zos = new ZipOutputStream(fos);// 利用文件输出流对象创建Zip输出流对象 byte[] buffer = new byte[1024]; for (String string : path) {// 遍历所有要压缩文件的路径 File currentFile = new File(string); ZipEntry entry = new ZipEntry(string.substring(base.length() 1, string.length()));// 利用要压缩文件的相对路径创建ZipEntry对象 FileInputStream fis = new FileInputStream(currentFile); zos.putNextEntry(entry); int read = 0; while ((read = fis.read(buffer)) != -1) {// 将数据写入到Zip输出流中 zos.write(buffer, 0, read); } zos.closeEntry();// 关闭ZipEntry对象 fis.close(); } zos.close();// 释放资源 fos.close(); }}
下载java 压缩所有子文件夹用户还喜欢
- 18480 文章数
- 500万+ 热度
作者专栏
编辑推荐
- 淡抹u2引擎,修复内容较多,物有所值
- 界域传说·经典巨作=传世单机(一键安装)
- 丸子版本(175个传世版本大集合)
- GS版本:神话公益服务端+客户端
- 图片放大工具(放大图片不模糊)
- 剪映无限制VIP版
- 传奇世界客户端下载器,史上最全传世客户端
- 传世GS20220920商业引擎注册+登录配置器 解压密码是1
- U2官方排行榜游戏网关 支持元神,支持传家宝
- GS开战传世客户端+服务端
- (淡漠夕阳)u2引擎合区工具
- 传世GS引擎消除“你的游戏客户端版本号过旧,请及时更新”提示
- 传世一机多区双线路配置器--免密码版本
- 传世凤凰登陆器劫持修复软件
- SQLite3 for Navicat
- 传奇世界npc对话框编辑工具
- 传世GS落霞铭文服务器端
- gs_20210409引擎包+注册机(无限制)
- 传奇世界NPC对话封包查看器[支持时长版和极速版]
- 彩虹引擎传世脚本编辑工具1.7版来了,支持函数脚本翻译
评论