172 lines
6.0 KiB
Java
172 lines
6.0 KiB
Java
|
import java.awt.BorderLayout;
|
||
|
import java.awt.event.ActionEvent;
|
||
|
import java.awt.event.ActionListener;
|
||
|
import java.io.File;
|
||
|
import java.io.FileOutputStream;
|
||
|
import java.io.IOException;
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.Enumeration;
|
||
|
import java.util.List;
|
||
|
import java.util.zip.ZipEntry;
|
||
|
import java.util.zip.ZipOutputStream;
|
||
|
|
||
|
import javax.swing.JButton;
|
||
|
import javax.swing.JCheckBox;
|
||
|
import javax.swing.JFileChooser;
|
||
|
import javax.swing.JFrame;
|
||
|
import javax.swing.JLabel;
|
||
|
import javax.swing.JPanel;
|
||
|
import javax.swing.JScrollPane;
|
||
|
import javax.swing.JTree;
|
||
|
import javax.swing.SwingUtilities;
|
||
|
import javax.swing.UIManager;
|
||
|
import javax.swing.event.TreeSelectionEvent;
|
||
|
import javax.swing.event.TreeSelectionListener;
|
||
|
import javax.swing.tree.DefaultMutableTreeNode;
|
||
|
import javax.swing.tree.TreePath;
|
||
|
|
||
|
public class FileSelectionApp extends JFrame {
|
||
|
private JTree fileTree;
|
||
|
private JCheckBox[] checkBoxes;
|
||
|
private JLabel statusLabel;
|
||
|
|
||
|
public FileSelectionApp() {
|
||
|
setTitle("文件选择");
|
||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||
|
setSize(600, 400);
|
||
|
setLocationRelativeTo(null);
|
||
|
|
||
|
// 创建选择按钮
|
||
|
JButton selectButton = new JButton("打包选中的文件或文件夹");
|
||
|
selectButton.addActionListener(new ActionListener() {
|
||
|
public void actionPerformed(ActionEvent e) {
|
||
|
selectItems();
|
||
|
}
|
||
|
});
|
||
|
|
||
|
// 创建文件树
|
||
|
fileTree = new JTree(getFileTreeModel());
|
||
|
fileTree.setRootVisible(false);
|
||
|
fileTree.setShowsRootHandles(true);
|
||
|
fileTree.addTreeSelectionListener(new TreeSelectionListener() {
|
||
|
public void valueChanged(TreeSelectionEvent e) {
|
||
|
updateStatusLabel();
|
||
|
}
|
||
|
});
|
||
|
JScrollPane treeScrollPane = new JScrollPane(fileTree);
|
||
|
|
||
|
// 创建结果标签
|
||
|
statusLabel = new JLabel();
|
||
|
updateStatusLabel();
|
||
|
|
||
|
// 创建面板并添加组件
|
||
|
JPanel panel = new JPanel(new BorderLayout());
|
||
|
panel.add(selectButton, BorderLayout.NORTH);
|
||
|
panel.add(treeScrollPane, BorderLayout.CENTER);
|
||
|
panel.add(statusLabel, BorderLayout.SOUTH);
|
||
|
|
||
|
// 将面板添加到窗口
|
||
|
add(panel);
|
||
|
}
|
||
|
|
||
|
private DefaultMutableTreeNode getFileTreeModel() {
|
||
|
File targetDir = new File("target/classes/");
|
||
|
DefaultMutableTreeNode root = new DefaultMutableTreeNode(targetDir.getName());
|
||
|
buildFileTree(root, targetDir);
|
||
|
return root;
|
||
|
}
|
||
|
|
||
|
private void buildFileTree(DefaultMutableTreeNode parent, File directory) {
|
||
|
File[] files = directory.listFiles();
|
||
|
if (files != null) {
|
||
|
for (File file : files) {
|
||
|
DefaultMutableTreeNode node = new DefaultMutableTreeNode(file.getName());
|
||
|
parent.add(node);
|
||
|
if (file.isDirectory()) {
|
||
|
buildFileTree(node, file);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void selectItems() {
|
||
|
List<File> selectedFiles = new ArrayList<>();
|
||
|
DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) fileTree.getModel().getRoot();
|
||
|
Enumeration<?> enumeration = rootNode.breadthFirstEnumeration();
|
||
|
while (enumeration.hasMoreElements()) {
|
||
|
DefaultMutableTreeNode node = (DefaultMutableTreeNode) enumeration.nextElement();
|
||
|
if (node.isLeaf() && checkBoxes[node.getLevel()].isSelected()) {
|
||
|
TreePath path = new TreePath(node.getPath());
|
||
|
selectedFiles.add(new File(path.toString().substring(1).replace(", ", "/")));
|
||
|
}
|
||
|
}
|
||
|
if (!selectedFiles.isEmpty()) {
|
||
|
createZipFile(selectedFiles);
|
||
|
} else {
|
||
|
statusLabel.setText("请勾选要打包的文件或文件夹");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void createZipFile(List<File> files) {
|
||
|
JFileChooser fileChooser = new JFileChooser();
|
||
|
int result = fileChooser.showSaveDialog(this);
|
||
|
if (result == JFileChooser.APPROVE_OPTION) {
|
||
|
File zipFile = fileChooser.getSelectedFile();
|
||
|
try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile))) {
|
||
|
for (File file : files) {
|
||
|
addFileToZip(file, file.getName(), zipOutputStream);
|
||
|
}
|
||
|
statusLabel.setText("已将文件打包为:" + zipFile.getAbsolutePath());
|
||
|
} catch (IOException e) {
|
||
|
statusLabel.setText("打包文件时出现错误:" + e.getMessage());
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void addFileToZip(File file, String entryName, ZipOutputStream zipOutputStream) throws IOException {
|
||
|
if (file.isDirectory()) {
|
||
|
File[] files = file.listFiles();
|
||
|
if (files != null) {
|
||
|
for (File child : files) {
|
||
|
addFileToZip(child, entryName + "/" + child.getName(), zipOutputStream);
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
byte[] buffer = new byte[1024];
|
||
|
FileInputStream fis = new FileInputStream(file);
|
||
|
zipOutputStream.putNextEntry(new ZipEntry(entryName));
|
||
|
int length;
|
||
|
while ((length = fis.read(buffer)) > 0) {
|
||
|
zipOutputStream.write(buffer, 0, length);
|
||
|
}
|
||
|
zipOutputStream.closeEntry();
|
||
|
fis.close();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void updateStatusLabel() {
|
||
|
TreePath[] paths = fileTree.getSelectionPaths();
|
||
|
if (paths != null) {
|
||
|
int selectedCount = paths.length;
|
||
|
statusLabel.setText("已选中 " + selectedCount + " 个文件/文件夹");
|
||
|
} else {
|
||
|
statusLabel.setText("");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static void main(String[] args) {
|
||
|
// 设置外观和风格
|
||
|
try {
|
||
|
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
|
||
|
SwingUtilities.invokeLater(new Runnable() {
|
||
|
public void run() {
|
||
|
new FileSelectionApp().setVisible(true);
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
}
|