Skip to content

Instantly share code, notes, and snippets.

@dineshr93
Last active November 6, 2017 14:36
Show Gist options
  • Select an option

  • Save dineshr93/226900b55492022c5055ca67a53a0f36 to your computer and use it in GitHub Desktop.

Select an option

Save dineshr93/226900b55492022c5055ca67a53a0f36 to your computer and use it in GitHub Desktop.
md5 source file lister
package com.din.own;
import java.awt.Desktop;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.xml.bind.DatatypeConverter;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class TreeStructure {
/**
* Pretty print the directory tree and its file names.
*
* @param folder
* must be a folder.
* @return
* @throws IOException
*/
static String root = null,DirectoryName = null,BesidesPath = null;;
static int rowCount = 0;
static int columnCount =0;
static XSSFSheet sheet = null;
static Row row = null;
static Cell cell = null;
static XSSFWorkbook workbook = null;
public static void performer(String directory) throws IOException, NoSuchAlgorithmException {
root=directory;
DirectoryName = new File(directory).getName();
BesidesPath = new File(directory).getParent();
//File folder = new File(string);
System.out.println(directory);
String outputPath = BesidesPath+"\\List of files with MD5 in "+DirectoryName+".xlsx";
FileOutputStream outputStream = new FileOutputStream(outputPath);
workbook = new XSSFWorkbook();
//header formating
XSSFFont font = workbook.createFont();
font.setBold(true);
CellStyle stylebold = workbook.createCellStyle(); //cell.setCellStyle(stylebold);
stylebold.setFont(font);
CellStyle stylecolor = workbook.createCellStyle(); //cell.setCellStyle(stylecolor);
stylecolor.setFillForegroundColor(IndexedColors.LEMON_CHIFFON.getIndex());
stylecolor.setFillPattern(CellStyle.SOLID_FOREGROUND);
//header formatting
//for second time operating row should be again zero
rowCount=0;
columnCount=0;
//for second time operating row should be again zero
sheet = workbook.createSheet(DirectoryName);
row = sheet.createRow(rowCount++);
cell = row.createCell(columnCount++);
cell.setCellValue("List of Files in "+"'"+DirectoryName+"'");
cell.setCellStyle(stylebold);
cell.setCellStyle(stylecolor);
//=====
cell = row.createCell(columnCount);
cell.setCellValue("MD5");
cell.setCellStyle(stylebold);
cell.setCellStyle(stylecolor);
//=====
printDirectoryTree(directory);
sheet.autoSizeColumn(0);
sheet.autoSizeColumn(1);
sheet.setAutoFilter(new CellRangeAddress(0,rowCount , 0, 1));
workbook.write(outputStream);
outputStream.close();
workbook.close();
JOptionPane.showMessageDialog (null, "Check output in "+ directory, "Info", JOptionPane.INFORMATION_MESSAGE);
Desktop.getDesktop().open(new File(outputPath));
}
private static void printDirectoryTree(String directory) throws NoSuchAlgorithmException, IOException {
if (!new File(directory).isDirectory()) {
throw new IllegalArgumentException("folder is not a Directory");
}
//String directory = <your_directory>;
File[] files = new File(directory).listFiles();
for (File file : files) {
if (file.isDirectory()) {
printDirectoryTree(file.getAbsolutePath());
} else {
printFile(file, directory); //prints file name
}
}
//return sb.toString();
}
private static void printFile(File file,String directory) throws NoSuchAlgorithmException, IOException {
// TODO Auto-generated method stub
//sb.append(file.getAbsolutePath().replace(directory, "")); //to list all files with out folder path
//sb.append(file.getAbsolutePath().replace(root, "")); //to list all files with folder path
//sb.append("\n");
columnCount=0;
row = sheet.createRow(rowCount++);
cell = row.createCell(columnCount++);
cell.setCellValue(file.getAbsolutePath().replace(root, ""));
cell = row.createCell(columnCount);
cell.setCellValue(calculateMD5(""+file.getAbsoluteFile()));
}
private static String calculateMD5(String string) throws IOException, NoSuchAlgorithmException {
byte[] b = Files.readAllBytes(Paths.get(string));
byte[] hash = MessageDigest.getInstance("MD5").digest(b);
//System.out.println();
return DatatypeConverter.printHexBinary(hash);
}
}
package com.din.own;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.TransferHandler;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileSystemView;
public class ConsolidatorDemo extends JPanel implements ActionListener {
private static final long serialVersionUID = -4487732343062917781L;
// JFileChooser fc;
JButton clear,compare;
static JTextArea fc;
JList dropZone;
DefaultListModel listModel;
JSplitPane childSplitPane, parentSplitPane;
PrintStream ps;
public ConsolidatorDemo() {
super(new BorderLayout());
/* fc = new JFileChooser();;
fc.setMultiSelectionEnabled(true);
fc.setDragEnabled(true);
fc.setControlButtonsAreShown(false);
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);*/
fc= new JTextArea();
fc.setText("Rules:\n1. Drag and drop file\n2. click Generate button.\n\nEnd\n\nIf errors -> rad9kor");
fc.setEditable(false);
JPanel fcPanel = new JPanel(new BorderLayout());
fcPanel.add(fc, BorderLayout.CENTER);
compare = new JButton("Generate");
compare.addActionListener(this);
JPanel buttonPanel1 = new JPanel(new BorderLayout());
buttonPanel1.setBorder(BorderFactory.createEmptyBorder(1,1,1,1));
buttonPanel1.add(compare, BorderLayout.LINE_END);
clear = new JButton("Clear All");
clear.addActionListener(this);
JPanel buttonPanel = new JPanel(new BorderLayout());
buttonPanel.setBorder(BorderFactory.createEmptyBorder(1,1,1,1));
buttonPanel.add(clear, BorderLayout.LINE_END);
JPanel leftUpperPanel = new JPanel(new BorderLayout());
leftUpperPanel.setBorder(BorderFactory.createEmptyBorder(3,3,3,3));
leftUpperPanel.add(fcPanel, BorderLayout.CENTER);
leftUpperPanel.add(buttonPanel1, BorderLayout.LINE_END);
leftUpperPanel.add(buttonPanel, BorderLayout.PAGE_END);
JScrollPane leftLowerPanel = new javax.swing.JScrollPane();
leftLowerPanel.setBorder(BorderFactory.createEmptyBorder(3,3,3,3));
listModel = new DefaultListModel();
dropZone = new JList(listModel);
dropZone.setCellRenderer(new FileCellRenderer());
dropZone.setTransferHandler(new ListTransferHandler(dropZone));
dropZone.setDragEnabled(true);
dropZone.setDropMode(javax.swing.DropMode.INSERT);
dropZone.setBorder(new TitledBorder("Drag and drop files here"));
leftLowerPanel.setViewportView(new JScrollPane(dropZone));
childSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, leftLowerPanel,leftUpperPanel);
childSplitPane.setDividerLocation(200);//400
childSplitPane.setPreferredSize(new Dimension(300, 400));//480, 650
/*console = new JTextArea();
console.setColumns(40);
console.setLineWrap(true);
console.setBorder(new TitledBorder("Console"));
parentSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
childSplitPane, console);
parentSplitPane.setDividerLocation(480);
parentSplitPane.setPreferredSize(new Dimension(800, 650));*/
add(childSplitPane, BorderLayout.CENTER);
}
public void setDefaultButton() {
getRootPane().setDefaultButton(clear);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == clear) {
listModel.clear();
FileCellRenderer.files = new String[50]; ;
}else if (e.getSource() == compare) {
if(FileCellRenderer.files[1]!=null || FileCellRenderer.files[0]==null)
{
JOptionPane.showMessageDialog (null, "Please drop a folder for viewing ", "Info", JOptionPane.INFORMATION_MESSAGE);
return;
} else
try {
TreeStructure.performer(FileCellRenderer.files[0]);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (NoSuchAlgorithmException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
try {
//UIManager.setLookAndFeel("de.javasoft.plaf.synthetica.SyntheticaBlackStarLookAndFeel");
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
}catch (Exception e){
e.printStackTrace();
}
//Create and set up the window.
JFrame frame = new JFrame("Files_Lister_Excel");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//Create and set up the menu bar and content pane.
ConsolidatorDemo demo = new ConsolidatorDemo();
demo.setOpaque(true); //content panes must be opaque
frame.setContentPane(demo);
//Display the window.
frame.pack();
frame.setVisible(true);
demo.setDefaultButton();
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
class FileCellRenderer extends DefaultListCellRenderer {
static String files[] = new String[50];
//int i=0;
public Component getListCellRendererComponent(JList list,Object value,int index,boolean isSelected,boolean cellHasFocus) {
Component c = super.getListCellRendererComponent(list,value,index,isSelected,cellHasFocus);
if (c instanceof JLabel && value instanceof File) {
JLabel l = (JLabel)c;
File f = (File)value;
l.setIcon(FileSystemView.getFileSystemView().getSystemIcon(f));
l.setText(f.getName());
//l.setText(f.getAbsolutePath());
l.setToolTipText(f.getAbsolutePath());
files[index]= f.getAbsolutePath();
}
return c;
}
}
class ListTransferHandler extends TransferHandler {
private JList list;
ListTransferHandler(JList list) {
this.list = list;
}
@Override
public boolean canImport(TransferHandler.TransferSupport info) {
// we only import FileList
if (!info.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
return false;
}
return true;
}
@Override
public boolean importData(TransferHandler.TransferSupport info) {
if (!info.isDrop()) {
return false;
}
// Check for FileList flavor
if (!info.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
displayDropLocation("List doesn't accept a drop of this type.");
return false;
}
// Get the fileList that is being dropped.
Transferable t = info.getTransferable();
List<File> data;
try {
data = (List<File>)t.getTransferData(DataFlavor.javaFileListFlavor);
}
catch (Exception e) { return false; }
DefaultListModel model = (DefaultListModel) list.getModel();
for (Object file : data) {
model.addElement((File)file);
}
return true;
}
private void displayDropLocation(String string) {
System.out.println(string);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment