Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8507713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:56:51+00:00 2026-06-11T02:56:51+00:00

Can anyone recommend a Java library to manipulate jar files? I am finding the

  • 0

Can anyone recommend a Java library to manipulate jar files? I am finding the facilities available at java.util.jar rather terse (or maybe I don’t have some example code to look at). I am interested in things like:

  1. adding files to a jar
  2. removing files from a jar
  3. create a jar file from a folder
  4. deflate a jar file to a folder
  5. reading the contents of a jar entry (perhaps in memory, without having to deflate the jar on disk)
  6. creating a jar entry in memory without having to read from a file on disk.
  7. asking whether a jar file entry is a Manifest file or some other special file or not.

If anyone can point to example code using either the java.util.jar or some other library that would help. In particular looking at the classes and methods of java.util.jar it is not clear to me how to read the contents of a particular Jar entry or even how to deflate the jar (without spawning an external process).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T02:56:53+00:00Added an answer on June 11, 2026 at 2:56 am

    1 & 2 are simple manual operations of copying the entries from Jar to another adding or leaving the entries you want, deleting he original file and renaming the new to replace it.

    As jtahlbom has pointed out, the Java Jar API handles the rest out of the box.

    UPDATE with example

    These are REALLY basic examples. They show you how to read and write a Jar. Basically from that you can derive just about everything else you need to do.

    I’ve setup a personal library (not included) which basically allows me to pass in an InputStream and have it written to an OutputStream and visa versa. This means you could read from any where and write to anywhere, which could cover most of you requirements.

    public void unjar(File jar, File outputPath) throws IOException {
        JarFile jarFile = null;
        try {
            if (outputPath.exits() || outputPathFile.mkdirs()) {
                jarFile = new JarFile(jar);
                Enumeration<JarEntry> entries = jarFile.entries();
                while (entries.hasMoreElements()) {
                    JarEntry entry = entries.nextElement();
                    File path = new File(outputPath + File.separator + entry.getName());
                    if (entry.isDirectory()) {
                        if (!path.exists() && !path.mkdirs()) {
                            throw new IOException("Failed to create output path " + path);
                        }
                    } else {
                        System.out.println("Extracting " + path);
    
                        InputStream is = null;
                        OutputStream os = null;
                        try {
                            is = jarFile.getInputStream(entry);
                            os = new FileOutputStream(path);
    
                            byte[] byteBuffer = new byte[1024];
                            int bytesRead = -1;
                            while ((bytesRead = is.read(byteBuffer)) != -1) {
                                os.write(byteBuffer, 0, bytesRead);
                            }
                            os.flush();
                        } finally {
                            try {
                                os.close();
                            } catch (Exception e) {
                            }
                            try {
                                is.close();
                            } catch (Exception e) {
                            }
                        }
                    }
                }
            } else {
                throw IOException("Output path does not exist/could not be created");
            }
        } finally {
            try {
                jarFile.close();
            } catch (Exception e) {
            }
        }
    }
    
    public void jar(File jar, File sourcePath) throws IOException {
        JarOutputStream jos = null;
        try {
            jos = new JarOutputStream(new FileOutputStream(jar));
    
            List<File> fileList = getFiles(sourcePath);
            System.out.println("Jaring " + fileList.size() + " files");
    
            List<String> lstPaths = new ArrayList<String>(25);
            for (File file : fileList) {
                String path = file.getParent().replace("\\", "/");
                String name = file.getName();
    
                path = path.substring(sourcePath.getPath().length());
                if (path.startsWith("/")) {
                    path = path.substring(1);
                }
    
                if (path.length() > 0) {
                    path += "/";
                    if (!lstPaths.contains(path)) {
                        JarEntry entry = new JarEntry(path);
                        jos.putNextEntry(entry);
                        jos.closeEntry();
                        lstPaths.add(path);
                    }
                }
    
                System.out.println("Adding " + path + name);
    
                JarEntry entry = new JarEntry(path + name);
                jos.putNextEntry(entry);
    
                FileInputStream fis = null;
                try {
                    fis = new FileInputStream(file);
                    byte[] byteBuffer = new byte[1024];
                    int bytesRead = -1;
                    while ((bytesRead = fis.read(byteBuffer)) != -1) {
                        jos.write(byteBuffer, 0, bytesRead);
                    }
                    jos.flush();
                } finally {
                    try {
                        fis.close();
                    } catch (Exception e) {
                    }
                }
                jos.closeEntry();
            }
            jos.flush();
        } finally {
            try {
                jos.close();
            } catch (Exception e) {
            }
        }
    }
    

    If you take the time look, there are a number of examples of how to add/remove entries from the from zip/jar files on SO

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to work with a third-party Java library from .NET. Can anyone recommend
Can anyone recommend me a java library to allow me XPath Queries over URLs?
Can anyone recommend a Java library that would allow me to create a video
Can anyone recommend an xml rpc library or client for wordpress using java? I've
Can anyone recommend any Java or .NET library that I can use to ingest
Can anyone recommend a Java Graph Visualisation library in which graph nodes can be
Can anyone recommend a good Java game engine for developing simple tile-based games? I'm
Can anyone recommend a good gem or library for managing a mailing list with
Possible Duplicate: Can anyone recommend a Java rich text editor? i need something like
I would like to know if anyone can recommend a good library to generate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.