Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. public static void main(String[] args) throws IOException {
  2.  
  3.  
  4. String jarName = args[0];
  5. String fileName = args[1];
  6.  
  7. // Create file descriptors for the jar and a temp jar.
  8.  
  9. File jarFile = new File(jarName);
  10. File tempJarFile = new File(jarName + ".tmp");
  11.  
  12. // Open the jar file.
  13.  
  14. JarFile jar = new JarFile(jarFile);
  15. System.out.println(jarName + " opened.");
  16.  
  17. // Initialize a flag that will indicate that the jar was updated.
  18.  
  19. boolean jarUpdated = false;
  20.  
  21. try {
  22. // Create a temp jar file with no manifest. (The manifest will
  23. // be copied when the entries are copied.)
  24.  
  25. Manifest jarManifest = jar.getManifest();
  26. JarOutputStream tempJar =
  27. new JarOutputStream(new FileOutputStream(tempJarFile));
  28.  
  29. // Allocate a buffer for reading entry data.
  30.  
  31. byte[] buffer = new byte[1024];
  32. int bytesRead;
  33.  
  34. try {
  35. // Open the given file.
  36.  
  37. FileInputStream file = new FileInputStream(fileName);
  38.  
  39. try {
  40. // Create a jar entry and add it to the temp jar.
  41.  
  42. JarEntry entry = new JarEntry(fileName);
  43. tempJar.putNextEntry(entry);
  44.  
  45. // Read the file and write it to the jar.
  46.  
  47. while ((bytesRead = file.read(buffer)) != -1) {
  48. tempJar.write(buffer, 0, bytesRead);
  49. }
  50.  
  51. System.out.println(entry.getName() + " added.");
  52. }
  53. finally {
  54. file.close();
  55. }
  56.  
  57. // Loop through the jar entries and add them to the temp jar,
  58. // skipping the entry that was added to the temp jar already.
  59.  
  60. for (Enumeration entries = jar.entries(); entries.hasMoreElements(); ) {
  61. // Get the next entry.
  62.  
  63. JarEntry entry = (JarEntry) entries.nextElement();
  64.  
  65. // If the entry has not been added already, add it.
  66.  
  67. if (! entry.getName().equals(fileName)) {
  68. // Get an input stream for the entry.
  69.  
  70. InputStream entryStream = jar.getInputStream(entry);
  71.  
  72. // Read the entry and write it to the temp jar.
  73.  
  74. tempJar.putNextEntry(entry);
  75.  
  76. while ((bytesRead = entryStream.read(buffer)) != -1) {
  77. tempJar.write(buffer, 0, bytesRead);
  78. }
  79. }
  80. }
  81.  
  82. jarUpdated = true;
  83. }
  84. catch (Exception ex) {
  85. System.out.println(ex);
  86.  
  87. // Add a stub entry here, so that the jar will close without an
  88. // exception.
  89.  
  90. tempJar.putNextEntry(new JarEntry("stub"));
  91. }
  92. finally {
  93. tempJar.close();
  94. }
  95. }
  96. finally {
  97. jar.close();
  98. System.out.println(jarName + " closed.");
  99.  
  100. // If the jar was not updated, delete the temp jar file.
  101.  
  102. if (! jarUpdated) {
  103. tempJarFile.delete();
  104. }
  105. }
  106.  
  107. // If the jar was updated, delete the original jar file and rename the
  108. // temp jar file to the original name.
  109.  
  110. if (jarUpdated) {
  111. jarFile.delete();
  112. tempJarFile.renameTo(jarFile);
  113. System.out.println(jarName + " updated.");
  114. }
  115.  
  116. Runtime.getRuntime().exec("zip -d pathmy.jar some_file.txt");
  117.  
  118. String path = SomeClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();
  119.  
  120. Process p = Runtime.getRuntime().exec("zip -d pathmy.jar some_file.txt");
  121. BufferedReader reader =
  122. new BufferedReader(new InputStreamReader(p.getInputStream()));
  123.  
  124. String line = "";
  125. StringBuilder sb = new StringBuilder();
  126. while ((line = reader.readLine())!= null) {
  127. sb.append(line + "n");
  128. System.out.println(line);
  129. }
  130. System.out.println(sb.toString());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement