Advertisement
SforzandoCF

file manager

May 9th, 2025
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.48 KB | None | 0 0
  1. import java.awt.image.BufferedImage;
  2. import java.io.DataInputStream;
  3. import java.io.EOFException;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.IOException;
  7. import java.nio.file.Files;
  8. import java.nio.file.attribute.BasicFileAttributes;
  9. import java.util.Scanner;
  10.  
  11. import javax.imageio.ImageIO;
  12. import javax.sound.sampled.AudioFormat;
  13. import javax.sound.sampled.AudioSystem;
  14. import javax.sound.sampled.UnsupportedAudioFileException;
  15.  
  16. public class Main {
  17.     public static void main (String[] args) {
  18.         Scanner scanner = new Scanner(System.console().reader());
  19.         File root = null;
  20.         FileInputStream reading = null;
  21.         while (true) {
  22.             if (scanner.hasNextLine()) {
  23.                 String next = scanner.nextLine();
  24.                 if (next.equals("/view-files")) {
  25.                     root = File.listRoots()[0];
  26.                     print(root);
  27.                 } else if (next.startsWith("/view-file ")) {
  28.                     try {
  29.                         int number = Integer.parseInt(next.substring(11));
  30.                         root = root.listFiles()[number];
  31.                     } catch (NumberFormatException nfe) {
  32.                         String name = next.substring(11);
  33.                         for (File f : root.listFiles()) {
  34.                             if (f.getName().endsWith(name)) {
  35.                                 root = f;
  36.                                 break;
  37.                             }
  38.                             if (f.getName().endsWith(name + "/")) {
  39.                                 root = f;
  40.                                 break;
  41.                             }
  42.                         }
  43.                     }
  44.                     print(root);
  45.                 } else if (next.startsWith("/move-up")) {
  46.                     root = root.getParentFile();
  47.                     print(root);
  48.                 } else if (next.startsWith("/inspect-file")) {
  49.                     if (!root.isFile()) continue;
  50.                     inspect(root);
  51.                 } else if (next.startsWith("/inspect-text")) {
  52.                     if (!root.isFile()) continue;
  53.                     inspectAsText(root);
  54.                 } else if (next.startsWith("/jump-to ")) {
  55.                     File f = new File(next.substring(9));
  56.                     if (f.exists()) {
  57.                         root = f;
  58.                         print(root);
  59.                     }
  60.                 } else if (next.startsWith("/view-root")) {
  61.                     System.out.println(root);
  62.                 } else if (next.startsWith("/open")) {
  63.                     try {
  64.                         reading = new FileInputStream(root);
  65.                         System.out.println("file opened");
  66.                     } catch (IOException ioe) {}
  67.                 } else if (next.startsWith("/read ")) {
  68.                     read(reading, Integer.parseInt(next.substring(6)));
  69.                 }
  70.                 if (root == null) {
  71.                     System.out.println("internal error: file accesed does not exist");
  72.                     root = File.listRoots()[0];
  73.                 }
  74.             }
  75.         }
  76.     }
  77.    
  78.     public static void print (File file) {
  79.         if (!file.isDirectory()) printFile(file);
  80.         File[] files = file.listFiles();
  81.         if (files == null) return;
  82.         if (files.length == 0) {
  83.             System.out.println("-> empty directory\n");
  84.             return;
  85.         }
  86.         for (int i = 0; i < files.length; i++)
  87.             System.out.println("[" + i + "] -> " + files[i] + (files[i].isDirectory() ? "/" : ""));
  88.         System.out.println();
  89.     }
  90.    
  91.     public static void printFile (File f) {
  92.         BasicFileAttributes attrs;
  93.         try {
  94.             attrs = Files.readAttributes(f.toPath(), BasicFileAttributes.class);
  95.         } catch (IOException ioe) {
  96.             System.out.println(ioe);
  97.             return;
  98.         }
  99.         System.out.println("-> path: " + f.toString());
  100.         String name = f.getName();
  101.         String ext = (name.contains(".") ? name.substring(name.lastIndexOf(".")) : "none");
  102.         name = name.contains(".") ? name.substring(0, name.lastIndexOf(".")) : name;
  103.         if (f.getName().startsWith(".")) {
  104.             name = ext;
  105.         }
  106.         System.out.println("-> name: " + name);
  107.         System.out.println("-> extension: " + ext);
  108.         System.out.println();
  109.         System.out.println("-> time created: " + attrs.creationTime());
  110.         System.out.println("-> time last modified: " + attrs.lastModifiedTime());
  111.         System.out.println("-> time last accessed: " + attrs.lastAccessTime());
  112.         System.out.println();
  113.         System.out.println("-> executable: " + f.canExecute());
  114.         System.out.println("-> readable: " + f.canRead());
  115.         System.out.println("-> writable: " + f.canWrite());
  116.         System.out.println("-> hidden: " + f.isHidden());
  117.         System.out.println();
  118.         String type = "file";
  119.         if (attrs.isSymbolicLink()) type = "symlink";
  120.         if (attrs.isOther()) type = "other";
  121.         System.out.println("-> type: " + type);
  122.         System.out.println("-> file size: " + attrs.size());
  123.         System.out.println();
  124.         System.out.println("-> partition size: " + f.getTotalSpace());
  125.     }
  126.    
  127.     private static void inspect (File f) {
  128.         if (tryInspectImage(f)) return;
  129.         if (tryInspectAudio(f)) return;
  130.         try {
  131.             DataInputStream d = new DataInputStream(new FileInputStream(f));
  132.             StringBuilder sb = new StringBuilder();
  133.             sb.append("-> ");
  134.             int index = 0;
  135.             int line = 0;
  136.             int chunk = 0;
  137.             while (true) {
  138.                 try {
  139.                     String b = Integer.toHexString(d.readByte());
  140.                     if (b.length() < 2) b = "0".concat(b);
  141.                     if (b.length() > 2) b = b.substring(b.length() - 2);
  142.                     sb.append(b);
  143.                     index++;
  144.                     if (index == 16) {
  145.                         index = 0;
  146.                         line++;
  147.                         if (line == 16) {
  148.                             line = 0;
  149.                             chunk++;
  150.                             if (chunk == 3) {
  151.                                 System.out.println(sb.toString());
  152.                                 System.out.println("\n...\n");
  153.                                 return;
  154.                             }
  155.                             System.out.println(sb.toString());
  156.                             sb = new StringBuilder();
  157.                         }
  158.                         sb.append("\n-> ");
  159.                     } else {
  160.                         sb.append(" ");
  161.                     }
  162.                 } catch (EOFException eof) {
  163.                     System.out.println(sb.toString());
  164.                 }
  165.             }
  166.         } catch (IOException ioe) {
  167.             System.out.println(ioe);
  168.         }
  169.     }
  170.    
  171.     private static void inspectAsText (File f) {
  172.         try {
  173.             DataInputStream d = new DataInputStream(new FileInputStream(f));
  174.             StringBuilder sb = new StringBuilder();
  175.             sb.append("-> ");
  176.             int index = 0;
  177.             int line = 0;
  178.             int chunk = 0;
  179.             while (true) {
  180.                 try {
  181.                     sb.append(toChar(d.readByte()));
  182.                     index++;
  183.                     if (index == 16) {
  184.                         index = 0;
  185.                         line++;
  186.                         if (line == 16) {
  187.                             line = 0;
  188.                             chunk++;
  189.                             if (chunk == 3) {
  190.                                 System.out.println(sb.toString());
  191.                                 System.out.println("\n-> ...\n");
  192.                                 return;
  193.                             }
  194.                             System.out.println(sb.toString());
  195.                             sb = new StringBuilder();
  196.                         }
  197.                         sb.append("\n-> ");
  198.                     }
  199.                 } catch (EOFException eof) {
  200.                     System.out.println(sb.toString());
  201.                 }
  202.             }
  203.         } catch (IOException ioe) {
  204.             System.out.println(ioe);
  205.         }
  206.     }
  207.    
  208.     private static boolean tryInspectImage (File f) {
  209.         return tryInspectImage(f, true);
  210.     }
  211.    
  212.     private static boolean tryInspectImage (File f, boolean loopInternally) {
  213.         try {
  214.             BufferedImage i = ImageIO.read(f);
  215.             if (i == null) return false;
  216.             System.out.println("-> width: " + i.getWidth());
  217.             System.out.println("-> height: " + i.getHeight());
  218.             System.out.println();
  219.             return true;
  220.         } catch (IOException ioe) {
  221.             if (loopInternally) return tryInspectImage(f, false);
  222.             return false;
  223.         }
  224.     }
  225.    
  226.     private static boolean tryInspectAudio (File f) {
  227.         return tryInspectAudio(f, true);
  228.     }
  229.    
  230.     private static boolean tryInspectAudio (File f, boolean loopInternally) {
  231.         try {
  232.             AudioFormat format = AudioSystem.getAudioInputStream(f).getFormat();
  233.             System.out.println("-> encoding: " + format.getEncoding());
  234.             String channels;
  235.             if (format.getChannels() == 1) channels = "mono";
  236.             else if (format.getChannels() == 2) channels = "stereo";
  237.             else channels = format.getChannels() + " channels";
  238.             System.out.println("-> channels: " + channels);
  239.             System.out.println("-> sample rate: " + format.getSampleRate());
  240.             System.out.println("-> sample size: " + format.getSampleSizeInBits() + "-bit");
  241.             System.out.println("-> big-endian: " + format.isBigEndian());
  242.             System.out.println();
  243.             return true;
  244.         } catch (IOException ioe) {
  245.             if (loopInternally) return tryInspectImage(f, false);
  246.             return false;
  247.         } catch (UnsupportedAudioFileException uafe) {
  248.             return false;
  249.         }
  250.     }
  251.    
  252.     private static char toChar (byte b) {
  253.         if (b < 20) return '.';
  254.         return (char) b;
  255.     }
  256.    
  257.     private static void read (FileInputStream fs, int amt) {
  258.         if (fs == null) return;
  259.         try {
  260.             for (int i = 0; i < amt; i++)
  261.                 System.out.println(Integer.toHexString(fs.read()));
  262.         } catch (IOException ioe) {
  263.             System.out.println("io error occured");
  264.         }
  265.     }
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement