Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need a java program: we have to iterate inside jar.file, find necessary file e

ID: 3591848 • Letter: I

Question

I need a java program: we have to iterate inside jar.file, find necessary file extract it and then delete this file from jar.file

For example:

inside dog.jar we have:

   0 Sat Oct 14 00:56:48 PDT 2017 META-INF/

68 Sat Oct 14 00:56:50 PDT 2017 META-INF/MANIFEST.MF

78438 Sun Oct 08 22:44:38 PDT 2017 I_need_help.docx

115030 Sat Oct 14 02:25:18 PDT 2017 I_need.docx

And I want to extract file I_need.docx and then delete it from jar.file

After deleting dog.jar will have:

jar -tvf .ResycleBin.jar

   0 Sat Oct 14 00:56:48 PDT 2017 META-INF/

68 Sat Oct 14 00:56:50 PDT 2017 META-INF/MANIFEST.MF

78438 Sun Oct 08 22:44:38 PDT 2017 I_need_help.docx

Explanation / Answer


import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.stream.Stream;

public class JarUtils {
public static void main(String[] args) throws Exception {

/* Define JAR File System Properies in HashMap */
Map<String, String> jar_properties = new HashMap<>();
/* We want to read an existing ZIP File, so we set this to False */
jar_properties.put("create", "false");

/* Specify the path to the ZIP File that you want to read as a File System */
URI zip_disk = URI.create("jar:file:/home/Documents/workspace-sts-3.6.3.RELEASE/demo-chegg/src/main/resources/demo.jar");

/* Create ZIP file System */
try (FileSystem zipfs = FileSystems.newFileSystem(zip_disk, jar_properties)) {
/* get Root path of Jar file */
Path rootpath = zipfs.getPath("/");

/* prepare Walk object which will walk through all files */
Stream<Path> walk = Files.walk(rootpath);

/* index and IndexToNameMap for more user friendly Output and delete purpose */
int index = 0;
Map<Integer, String> indexToNameMap = new HashMap<>();
for (Iterator<Path> it = walk.iterator(); it.hasNext();) {
Path p = it.next();
System.out.println("[" + index + "] " + p);

/* store index and it's corresponding file name */
indexToNameMap.put(index++, p.toString());
}

// close the stream.
walk.close();

System.out.print(" Enter Index of File you want to extract and delete from JAR file: ");
Scanner sc = new Scanner(System.in);

int userIndex = sc.nextInt();
sc.close();

if (indexToNameMap.containsKey(userIndex)) {
/* Get the Path inside ZIP File to delete the ZIP Entry */
Path pathInZipfile = zipfs.getPath(indexToNameMap.get(userIndex));

Path extractPath = Paths.get(System.getProperty("user.dir") + "/"
+ pathInZipfile.getFileName().toString());

System.out.println("Extracting File to " + extractPath.toString());

// COPY file before deleting it
Files.copy(pathInZipfile, extractPath);

System.out.println("About to delete an entry from ZIP File" + pathInZipfile.toUri());

/* Execute Delete */
Files.delete(pathInZipfile);

System.out.println("File successfully deleted");
}
}
}
}

Hope above code helps you to understand how Extracting, Deleting and listing files from jar is working.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote