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

  • Home
  • SEARCH
  • 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 7037601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:33:54+00:00 2026-05-28T01:33:54+00:00

Im using the following Code to try an package a directory with all its

  • 0

Im using the following Code to try an package a directory with all its subdirectories and files into one jar-file.

private void writeFile(JarOutputStream jos, File f) throws IOException {
  byte buffer[] = new byte[BUFFER_SIZE];

  if (f == null || !f.exists())
      return; //

  JarEntry jarAdd = new JarEntry(f.getName());
  jarAdd.setTime(f.lastModified());
  jos.putNextEntry(jarAdd);

  if(f.isDirectory()){
      File[] files = f.listFiles();
      for(int i = 0; i < files.length ; i++){
          writeFile(jos,files[i]);
      }
  }
  else{
      FileInputStream in = new FileInputStream(f);
      while (true) {
        int nRead = in.read(buffer, 0, buffer.length);
        if (nRead <= 0)
          break;
        jos.write(buffer, 0, nRead);
      }
      in.close();
  }
}

However FileInputStream does not work on directories, so i assume adding the JarEntry will suffice.

Since I do have another problem, I am not able to see if this assumption is correct.
This problem is that many entries made are duplicate.

When trying to add the structure…

activation
/--activation
   /-- somefile.txt

…this code fails, because activation is added twice to the jar.
Since those are different directories nested within each other I do not see why this should not work in some way.

It seems that adding the entry using file.getName() will get entry-ids that do not
differentiate between the directories those files are in.
However when I use

file.getAbsolutePath() 

for example, the jar structure is messed up with
directories like “C_” and the contained files are not readable anymore.

Can anyone recommend a way to package directory structures in a jar file correctly?

  • 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-05-28T01:33:55+00:00Added an answer on May 28, 2026 at 1:33 am

    Note that a JarEntry representing a directory (folder) must end with a slash (“/“) in order to be treated as such; otherwise it will be considered a plain file.

    JarEntry dir = new JarEntry("activation/");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently downloading an HTML page, using the following code: Try Dim req As
I'm using the following code to try to read the results of a df
I am using the following code to try and display in a time in
The following code is a try to encrypt data using AES with asymmetric key:
We try to convert from string to Byte[] using the following Java code: String
I am using the following C# code to connect to active directory and validate
I am using the following code to validate an an XML file against a
I am using the following code for 2D-QR code decoder. package com.test.rim; import java.util.*;
I am using the following code for parsing of small xml files and it
I am running the following code to try and read from a text file.

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.