elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Recuerda que debes registrarte en el foro para poder participar (preguntar y responder)


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  contribucion inspecionar archivos office por dentro
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: contribucion inspecionar archivos office por dentro  (Leído 1,866 veces)
sapito169


Desconectado Desconectado

Mensajes: 628



Ver Perfil
contribucion inspecionar archivos office por dentro
« en: 5 Octubre 2021, 01:03 am »

les presento una herramienta para que puedan ver sus  documento word excel y ppt de office 2003 2007 desde adentro

en futuras entregas mejorare la herramienta para que pueda comparar archivos de office
y también hacer una super búsqueda en varios archivos de office

Código
  1. package office;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.StringReader;
  7. import java.io.StringWriter;
  8. import java.util.ArrayList;
  9. import java.util.Collection;
  10. import java.util.Collections;
  11. import java.util.Comparator;
  12. import java.util.Enumeration;
  13. import java.util.HashMap;
  14. import java.util.HashSet;
  15. import java.util.LinkedHashMap;
  16. import java.util.Set;
  17. import java.util.List;
  18. import java.util.Map;
  19. import java.util.Scanner;
  20. import java.util.Stack;
  21. import java.util.zip.ZipEntry;
  22. import java.util.zip.ZipFile;
  23.  
  24. import javax.xml.XMLConstants;
  25. import javax.xml.parsers.DocumentBuilderFactory;
  26. import javax.xml.transform.OutputKeys;
  27. import javax.xml.transform.Source;
  28. import javax.xml.transform.Transformer;
  29. import javax.xml.transform.TransformerFactory;
  30. import javax.xml.transform.stream.StreamResult;
  31. import javax.xml.transform.stream.StreamSource;
  32.  
  33. import org.w3c.dom.Node;
  34. import org.w3c.dom.bootstrap.DOMImplementationRegistry;
  35. import org.w3c.dom.ls.DOMImplementationLS;
  36. import org.w3c.dom.ls.LSSerializer;
  37. import org.xml.sax.InputSource;
  38.  
  39. import javafx.application.Application;
  40. import javafx.beans.value.ChangeListener;
  41. import javafx.beans.value.ObservableValue;
  42. import javafx.event.ActionEvent;
  43. import javafx.geometry.Insets;
  44. import javafx.geometry.Pos;
  45. import javafx.scene.Scene;
  46. import javafx.scene.control.Alert;
  47. import javafx.scene.control.Button;
  48. import javafx.scene.control.Label;
  49. import javafx.scene.control.Menu;
  50. import javafx.scene.control.MenuBar;
  51. import javafx.scene.control.MenuItem;
  52. import javafx.scene.control.TextArea;
  53. import javafx.scene.control.TextField;
  54. import javafx.scene.control.TreeItem;
  55. import javafx.scene.control.TreeView;
  56. import javafx.scene.control.Alert.AlertType;
  57. import javafx.scene.layout.Background;
  58. import javafx.scene.layout.BorderPane;
  59. import javafx.scene.layout.HBox;
  60. import javafx.scene.layout.VBox;
  61. import javafx.stage.FileChooser;
  62. import javafx.stage.FileChooser.ExtensionFilter;
  63. import javafx.stage.Stage;
  64.  
  65. import java.util.Collection;
  66. import java.util.Collections;
  67. import java.util.regex.Matcher;
  68. import java.util.regex.Pattern;
  69.  
  70. import org.fxmisc.flowless.VirtualizedScrollPane;
  71. import org.fxmisc.richtext.CodeArea;
  72. import org.fxmisc.richtext.LineNumberFactory;
  73. import org.fxmisc.richtext.model.StyleSpans;
  74. import org.fxmisc.richtext.model.StyleSpansBuilder;
  75.  
  76. public class TreeViewExample extends Application {
  77.  
  78. Stage primaryStage;
  79. BorderPane root;
  80. Map<String, TreeItem> map;
  81. Map<String, String> inmemory;
  82.   CodeArea textArea = new    CodeArea();
  83.   TextField label  = new TextField("File:");
  84.  
  85. private static final Pattern XML_TAG = Pattern.compile("(?<ELEMENT>(</?\\h*)(\\w+)([^<>]*)(\\h*/?>))"
  86. +"|(?<COMMENT><!--[^<>]+-->)");
  87.  
  88. private static final Pattern ATTRIBUTES = Pattern.compile("(\\w+\\h*)(=)(\\h*\"[^\"]+\")");
  89.  
  90. private static final int GROUP_OPEN_BRACKET = 2;
  91. private static final int GROUP_ELEMENT_NAME = 3;
  92. private static final int GROUP_ATTRIBUTES_SECTION = 4;
  93. private static final int GROUP_CLOSE_BRACKET = 5;
  94. private static final int GROUP_ATTRIBUTE_NAME = 1;
  95. private static final int GROUP_EQUAL_SYMBOL = 2;
  96. private static final int GROUP_ATTRIBUTE_VALUE = 3;
  97.  
  98.  
  99. public static void main(String[] args) {
  100. Application.launch(args);
  101.  
  102. }
  103.  
  104. public void open(ActionEvent actionEvent) {
  105. textArea.clear();
  106. inmemory=new HashMap<String, String>();
  107. FileChooser fileChooser = new FileChooser();
  108. fileChooser.setTitle("selecione el excel");
  109.  
  110. fileChooser.getExtensionFilters().addAll(new ExtensionFilter("Excel", "*.xlsx"),
  111. new ExtensionFilter("Doc", "*.docx"), new ExtensionFilter("Ppt", "*..pptx"));
  112. File file = fileChooser.showOpenDialog(primaryStage);
  113.  
  114. if (file != null) {
  115. label.setText(file.getAbsolutePath());
  116. try (ZipFile zipFile = new ZipFile(file);) {
  117.  
  118. Enumeration<?> enu = zipFile.entries();
  119. Set<String> nodes = new HashSet<>();
  120. while (enu.hasMoreElements()) {
  121. ZipEntry zipEntry = (ZipEntry) enu.nextElement();
  122.  
  123. InputStream inputStream= zipFile.getInputStream(zipEntry);
  124.  
  125.  
  126.  
  127. String name = zipEntry.getName();
  128.  
  129. inmemory.put(name+"/", convertToString(inputStream));
  130. inputStream.close();
  131. String[] split = name.split("/");
  132.  
  133.  
  134.  
  135. String node = "";
  136. for (String current : split) {
  137. node += current + "/";
  138. nodes.add(node);
  139. }
  140.  
  141. }
  142. List<String> listnodes = new ArrayList<String>(nodes);
  143.  
  144. Collections.sort(listnodes, this::compare);
  145.  
  146. TreeItem rootItem = root(listnodes, file.getName());
  147.  
  148. TreeView treeView = new TreeView();
  149. treeView.getSelectionModel().selectedItemProperty().addListener(this::selectedFile);
  150. treeView.setRoot(rootItem);
  151.  
  152. treeView.setShowRoot(true);
  153.  
  154. root.setLeft(treeView);
  155.  
  156. } catch (Exception e) {
  157. Alert alert = new Alert(AlertType.ERROR);
  158. alert.setContentText(e.getMessage());
  159. e.printStackTrace();
  160. alert.showAndWait();
  161. }
  162.  
  163. }
  164.  
  165. }
  166.  
  167. String convertToString(InputStream in){
  168.    Scanner scanner = new Scanner(in);
  169.    scanner.useDelimiter("\\A");
  170.  
  171.    boolean hasInput = scanner.hasNext();
  172.    if (hasInput) {
  173.        return scanner.next();
  174.    } else {
  175.        return null;
  176.    }
  177.  
  178. }
  179.  
  180. void selectedFile(ObservableValue observable, Object oldValue,
  181.            Object newValue){
  182.        TreeItem<String> selectedItem = (TreeItem<String>) newValue;
  183.         for(String current: map.keySet()) {
  184.         if(map.get(current)==selectedItem) {
  185.         String text=inmemory.get(current);
  186.         if(text!=null) {
  187.         textArea.replaceText(prettyFormat(text));
  188.         }
  189.         }
  190.         }
  191. }
  192.  
  193. public int compare(String o1, String o2) {
  194. int result = (Integer.valueOf(o1.split("/").length)).compareTo(o2.split("/").length);
  195. if (result == 0)
  196. result = o1.compareTo(o2);
  197. return result;
  198. }
  199.  
  200. private TreeItem root(List<String> listnodes, String name) {
  201.  
  202. TreeItem rootItem = new TreeItem(name);
  203.  
  204. rootItem.setExpanded(true);
  205. map = new LinkedHashMap<String, TreeItem>();
  206. for (String current : listnodes) {
  207. String[] split = current.substring(0, current.length() - 1).split("/");
  208.  
  209. map.put(current, new TreeItem(split[split.length - 1]));
  210. }
  211.  
  212. for (String key : map.keySet()) {
  213.  
  214. if (key.split("/", -1).length == 2) {
  215.  
  216. rootItem.getChildren().add(map.get(key));
  217.  
  218. } else {
  219.  
  220. String currentName = map.get(key).getValue() + "";
  221. String result = key.substring(0, key.lastIndexOf(currentName + "/"));
  222.  
  223. TreeItem parent = map.get(result);
  224. parent.setExpanded(true);
  225. parent.getChildren().add(map.get(key));
  226.  
  227. }
  228.  
  229. }
  230.  
  231. return rootItem;
  232. }
  233.  
  234.  
  235.  
  236. public String prettyFormat(String xml) {
  237.  
  238.        try {
  239.            final InputSource src = new InputSource(new StringReader(xml));
  240.            final Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src).getDocumentElement();
  241.            final Boolean keepDeclaration = Boolean.valueOf(xml.startsWith("<?xml"));
  242.  
  243.        //May need this: System.setProperty(DOMImplementationRegistry.PROPERTY,"com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl");
  244.  
  245.  
  246.            final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
  247.            final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
  248.            final LSSerializer writer = impl.createLSSerializer();
  249.  
  250.            writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); // Set this to true if the output needs to be beautified.
  251.            writer.getDomConfig().setParameter("xml-declaration", keepDeclaration); // Set this to true if the declaration is needed to be outputted.
  252.  
  253.            return writer.writeToString(document);
  254.        } catch (Exception e) {
  255.            throw new RuntimeException(e);
  256.        }
  257.    }
  258.  
  259. @Override
  260. public void start(Stage primaryStage) {  
  261. label.setEditable(false);
  262. label.setStyle("-fx-background-color:#ced1d6;");
  263. textArea.setParagraphGraphicFactory(LineNumberFactory.get(textArea));
  264. this.primaryStage = primaryStage;
  265. Menu m = new Menu("Archivo");
  266.  
  267. MenuItem open = new MenuItem("Inspect");
  268.  
  269.  
  270. open.setOnAction(this::open);
  271. m.getItems().add(open);
  272.  
  273. MenuBar mb = new MenuBar();
  274. mb.getMenus().add(m);
  275.  
  276. root = new BorderPane();
  277.  
  278. VBox head = new VBox();
  279. head.getChildren().add(mb);
  280. head.getChildren().add(label);
  281. root.setTop(head);
  282.  
  283.  
  284. textArea.setEditable(false);
  285. textArea.textProperty().addListener((obs, oldText, newText) -> {
  286. textArea.setStyleSpans(0, computeHighlighting(newText));
  287.        });
  288. root.setCenter(new VirtualizedScrollPane<>(textArea));
  289.  
  290. Scene scene = new Scene(root, 550, 250);
  291. scene.getStylesheets().add(TreeViewExample.class.getResource("xml-highlighting.css").toExternalForm());
  292. primaryStage.setTitle("OpenXml Comparator");
  293. primaryStage.setScene(scene);
  294. // primaryStage.setMaximized(true);
  295.  
  296. primaryStage.show();
  297.  
  298. }
  299. private static StyleSpans<Collection<String>> computeHighlighting(String text) {
  300.  
  301.        Matcher matcher = XML_TAG.matcher(text);
  302.        int lastKwEnd = 0;
  303.        StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>();
  304.        while(matcher.find()) {
  305.  
  306.         spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd);
  307.         if(matcher.group("COMMENT") != null) {
  308.         spansBuilder.add(Collections.singleton("comment"), matcher.end() - matcher.start());
  309.         }
  310.         else {
  311.         if(matcher.group("ELEMENT") != null) {
  312.         String attributesText = matcher.group(GROUP_ATTRIBUTES_SECTION);
  313.  
  314.         spansBuilder.add(Collections.singleton("tagmark"), matcher.end(GROUP_OPEN_BRACKET) - matcher.start(GROUP_OPEN_BRACKET));
  315.         spansBuilder.add(Collections.singleton("anytag"), matcher.end(GROUP_ELEMENT_NAME) - matcher.end(GROUP_OPEN_BRACKET));
  316.  
  317.         if(!attributesText.isEmpty()) {
  318.  
  319.         lastKwEnd = 0;
  320.  
  321.         Matcher amatcher = ATTRIBUTES.matcher(attributesText);
  322.         while(amatcher.find()) {
  323.         spansBuilder.add(Collections.emptyList(), amatcher.start() - lastKwEnd);
  324.         spansBuilder.add(Collections.singleton("attribute"), amatcher.end(GROUP_ATTRIBUTE_NAME) - amatcher.start(GROUP_ATTRIBUTE_NAME));
  325.         spansBuilder.add(Collections.singleton("tagmark"), amatcher.end(GROUP_EQUAL_SYMBOL) - amatcher.end(GROUP_ATTRIBUTE_NAME));
  326.         spansBuilder.add(Collections.singleton("avalue"), amatcher.end(GROUP_ATTRIBUTE_VALUE) - amatcher.end(GROUP_EQUAL_SYMBOL));
  327.         lastKwEnd = amatcher.end();
  328.         }
  329.         if(attributesText.length() > lastKwEnd)
  330.         spansBuilder.add(Collections.emptyList(), attributesText.length() - lastKwEnd);
  331.         }
  332.  
  333.         lastKwEnd = matcher.end(GROUP_ATTRIBUTES_SECTION);
  334.  
  335.         spansBuilder.add(Collections.singleton("tagmark"), matcher.end(GROUP_CLOSE_BRACKET) - lastKwEnd);
  336.         }
  337.         }
  338.            lastKwEnd = matcher.end();
  339.        }
  340.        spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd);
  341.        return spansBuilder.create();
  342.    }
  343. }

Código
  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>tool</groupId>
  6. <artifactId>office</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <properties>
  10. <maven.compiler.source>1.8</maven.compiler.source>
  11. <maven.compiler.target>1.8</maven.compiler.target>
  12. </properties>
  13.  
  14. <dependencies>
  15. <dependency>
  16. <groupId>org.fxmisc.richtext</groupId>
  17. <artifactId>richtextfx</artifactId>
  18. <version>0.10.6
  19.    </version>
  20. </dependency>
  21. </dependencies>
  22.  
  23. </project>


Código
  1. .tagmark {
  2.    -fx-fill: gray;
  3. }
  4. .anytag {
  5.    -fx-fill: crimson;
  6. }
  7. .paren {
  8.    -fx-fill: firebrick;
  9.    -fx-font-weight: bold;
  10. }
  11. .attribute {
  12.    -fx-fill: darkviolet;
  13. }
  14. .avalue {
  15.    -fx-fill: black;
  16. }
  17.  
  18. .comment {
  19. -fx-fill: teal;
  20. }
  21.  
  22. .red-text {
  23.    -fx-font-color: red;
  24. }
  25. .red-green {
  26.    -fx-font-color: blue;
  27. }
  28. .styleClassName {
  29.  
  30.        -fx-background-color:gray;
  31.  
  32.     }


« Última modificación: 5 Octubre 2021, 01:34 am por sapito169 » En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
crackear archivos de office con password
Ingeniería Inversa
5v5 2 2,227 Último mensaje 25 Agosto 2005, 16:14 pm
por Tado77
Archivos .ini dentro de .exe
Programación Visual Basic
__LoG26 4 2,522 Último mensaje 3 Diciembre 2005, 11:38 am
por Syphroot
Borrar archivos dentro de un EXE...??
Software
radamantis2707 5 2,838 Último mensaje 15 Mayo 2012, 09:26 am
por Saberuneko
Microsoft Office me cifra los archivos guardados
Software
delanoche86 4 1,200 Último mensaje 13 Junio 2015, 00:39 am
por delanoche86
Office 365 comenzará a bloquear enlaces a sitios maliciosos dentro de Word, ...
Noticias
wolfbcn 0 1,313 Último mensaje 5 Abril 2017, 14:20 pm
por wolfbcn
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines