Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How do you make a Zip/Jar in Java that will not contain the absolute pathname?

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation How do you make a Zip/Jar in Java that will not contain the absolute pathname?

    I'm generating a .jar file in Java, but the .jar contains an absolute pathname of where it is in the system (/tmp/tempXXX/foo instead of /foo). The tree is like this:

    .
    |-- META-INF
    |-|- ....
    |-- tmp
    |-|- tempXXX
    |-|-|- foo
    |-|-|- bar

    Instead of this:

    .
    |-- META-INF
    |-|- ....
    |-- foo
    |-- bar

    Is it possible to fix this? Here is the function that makes it:

    public static void add(File source, JarOutputStream target, String removeme)
            throws IOException
    {
        BufferedInputStream in = null;
        try
        {
            File source2 = source;
            if (source.isDirectory())
            {
                String name = source2.getPath().replace("\\", "/");
                if (!name.isEmpty())
                {
                    if (!name.endsWith("/"))
                        name += "/";
                    JarEntry entry = new JarEntry(name);
                    entry.setTime(source.lastModified());
                    target.putNextEntry(entry);
                    target.closeEntry();
                }
                for (File nestedFile : source.listFiles())
                    add(nestedFile, target, removeme);
                return;
            }
     
            JarEntry entry = new JarEntry(source2.getPath().replace("\\", "/"));
            entry.setTime(source.lastModified());
            target.putNextEntry(entry);
            in = new BufferedInputStream(new FileInputStream(source));
     
            byte[] buffer = new byte[2048];
            while (true)
            {
                int count = in.read(buffer);
                if (count == -1)
                    break;
                target.write(buffer, 0, count);
            }
            target.closeEntry();
        }
        finally
        {
            if (in != null)
                in.close();
        }
    }

    The source2 variable was made for modifying the path, but when modifying, it gave an "Invalid .jar file" error.
    The modification was this:

        File source2 = new File(source.getPath().replaceAll("^" + removeme, ""));
    Last edited by lkjoel; February 14th, 2012 at 10:49 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,462
    My Mood
    Hungover
    Thanks
    144
    Thanked 639 Times in 547 Posts

    Default Re: How do you make a Zip/Jar in Java that will not contain the absolute pathname?

    I've never seen a Jar being created this way, so I'm only guessing, but have you tried removing removeme (which I assume is the pathname leading up to the directory you're working with) from the String name instead of from the File? And in that case, wouldn't remove me just be your source path?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. How to create directory in Java?
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: December 24th, 2011, 11:53 AM
  2. How to make HyperLink in java code?
    By abhiM in forum Java Theory & Questions
    Replies: 1
    Last Post: August 24th, 2011, 07:16 AM
  3. [SOLVED] This absolute uri (http://displaytag.sf.net) cannot be resolved in either web.xml or
    By santosh.dhulipala in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: November 4th, 2010, 06:35 AM
  4. make dir using mkdir() in java io
    By mssi in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 6th, 2010, 04:51 PM
  5. How to create directory in Java?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:43 AM

Tags for this Thread