Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. public class Extractor {
  4. public static void main(String[] args) {
  5. if (args.length != 2) {
  6. System.out.println("Specify a file and output dir!");
  7. } else {
  8. File in = new File(args[0]);
  9. File out = new File(args[1]);
  10.  
  11. extract(in, out);
  12. }
  13. }
  14.  
  15. private static void extract(File inFile, File out) {
  16. try {
  17. DataInputStream in = new DataInputStream(new FileInputStream(inFile));
  18. byte[] filenameBytes = new byte[60];
  19. while (in.available() > 0) {
  20. int nameLen = in.read(filenameBytes);
  21. char[] chars = new char[nameLen];
  22. for (int i = 0; i < nameLen; i++) {
  23. if (filenameBytes[i] == 0) {
  24. break;
  25. }
  26. chars[i] = (char)filenameBytes[i];
  27. }
  28. String name = new String(chars).trim();
  29.  
  30. int length = in.readInt();
  31.  
  32. extractFile(out, name, length, in);
  33. }
  34. } catch (Exception e) {
  35. e.printStackTrace();
  36. }
  37. }
  38.  
  39. private static void extractFile(File outDir, String name, int length, DataInputStream in) throws IOException {
  40. File outFile = new File(outDir, name);
  41. makeDirs(outFile);
  42. System.out.println("Extracting " + name + " to " + outFile.getPath());
  43. OutputStream out = new FileOutputStream(outFile);
  44. byte[] buff = new byte[length];
  45. int read = in.read(buff);
  46. if (read != length) {
  47. System.err.println("Number of read bytes did not match header!");
  48. }
  49. out.write(buff);
  50. out.close();
  51. }
  52.  
  53. private static void makeDirs(File file) {
  54. String path = file.getPath();
  55. int idx = path.lastIndexOf('/');
  56. if (idx != -1) {
  57. File dir = new File(path.substring(0, idx + 1));
  58. dir.mkdirs();
  59. }
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement