优化输出

dev
youhong.ai 2023-07-11 12:20:25 +08:00
parent a78f69bead
commit aa29abdcfe
4 changed files with 2 additions and 191 deletions

View File

@ -1,171 +0,0 @@
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);
}
});
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

View File

@ -6,11 +6,8 @@ import builderpackage.FileTreeBuilder;
import javafx.application.Application; import javafx.application.Application;
import javafx.geometry.Insets; import javafx.geometry.Insets;
import javafx.geometry.Pos; import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.scene.control.*; import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane; import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox; import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
@ -34,20 +31,11 @@ public class BuilderPackageEcology extends Application {
private TextField packageNumberTextField; private TextField packageNumberTextField;
private TextField functionNameTextField; private TextField functionNameTextField;
private final Node folderIcon = new ImageView(
new Image(Objects.requireNonNull(getClass().getResourceAsStream("icons/folder.png")))
);// 定义一个图片类型节点对象
private final Node fileIcon = new ImageView(
new Image(Objects.requireNonNull(getClass().getResourceAsStream("icons/file.png")))
);// 定义一个图片类型节点对象
@Override @Override
public void start(Stage primaryStage) { public void start(Stage primaryStage) {
primaryStage.setTitle("EBU7部开发一部自动打包工具"); primaryStage.setTitle("EBU7部开发一部自动打包工具");
FileInfo fileInfo = FileTreeBuilder.buildFileTree(); FileInfo fileInfo = FileTreeBuilder.buildFileTree();
rootItem = createTreeItem(fileInfo); rootItem = createTreeItem(fileInfo);
treeView = new TreeView<>(rootItem); treeView = new TreeView<>(rootItem);
treeView.setShowRoot(false); treeView.setShowRoot(false);
treeView.setCellFactory(treeViewParam -> { treeView.setCellFactory(treeViewParam -> {
@ -131,6 +119,7 @@ public class BuilderPackageEcology extends Application {
functionNameTextField.setText(""); functionNameTextField.setText("");
showSuccessAlert("升级包制作成功!"); showSuccessAlert("升级包制作成功!");
System.out.println("压缩包位置: " + outputFile.getPath()); System.out.println("压缩包位置: " + outputFile.getPath());
System.out.println(outputFile.getParentFile());
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
@ -163,11 +152,6 @@ public class BuilderPackageEcology extends Application {
private TreeItem<FileInfo> createTreeItem(FileInfo fileInfo) { private TreeItem<FileInfo> createTreeItem(FileInfo fileInfo) {
TreeItem<FileInfo> treeItem = new TreeItem<>(fileInfo); TreeItem<FileInfo> treeItem = new TreeItem<>(fileInfo);
if (fileInfo.getFileType().equals(FileTreeBuilder.DIRECTORY_TYPE)) {
treeItem.setGraphic(folderIcon);
} else {
treeItem.setGraphic(fileIcon);
}
List<FileInfo> children = fileInfo.getChildren(); List<FileInfo> children = fileInfo.getChildren();
if (children != null) { if (children != null) {
for (FileInfo child : children) { for (FileInfo child : children) {
@ -225,7 +209,6 @@ public class BuilderPackageEcology extends Application {
} else { } else {
filePaths.remove(filePath); filePaths.remove(filePath);
} }
System.out.println(fileInfo.getFilePath());
}); });
} }
@ -237,7 +220,7 @@ public class BuilderPackageEcology extends Application {
setGraphic(null); setGraphic(null);
} else { } else {
checkBox.setSelected(item.isSelected()); checkBox.setSelected(item.isSelected());
checkBox.setText(item.getFileName()); checkBox.setText(item.getFileName().replace("_", "__"));
setGraphic(checkBox); setGraphic(checkBox);
// setGraphic(folderIcon); // setGraphic(folderIcon);
} }
@ -258,7 +241,6 @@ public class BuilderPackageEcology extends Application {
for (Map.Entry<String, String> entry : placeholderMap.entrySet()) { for (Map.Entry<String, String> entry : placeholderMap.entrySet()) {
String placeholder = "${" + entry.getKey() + "}"; String placeholder = "${" + entry.getKey() + "}";
String value = entry.getValue(); String value = entry.getValue();
// 检查占位符是否存在于字符串中 // 检查占位符是否存在于字符串中
if (outputString.indexOf(placeholder) != -1) { if (outputString.indexOf(placeholder) != -1) {
// 使用 String 类的 replace 方法进行占位符的替换 // 使用 String 类的 replace 方法进行占位符的替换

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB