0

I want to copy all files inside jar file to outside the current directory.

Here is my code. It's writing all files name inside jar, so.. But I want to copy all the files from inside the jar to outside the jar.

import java.io.*;
import java.util.Enumeration;
import java.util.jar.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

   public class JarRead 
   {
     public static void main (String args[]) throws IOException 
     {
         ZipFile file = new ZipFile("jarfile.jar");
         if (file != null) {
            Enumeration<? extends ZipEntry> entries = file.entries(); 

            if (entries != null) {
               while (entries.hasMoreElements()) {
                   ZipEntry entry = entries.nextElement();
                   System.out.println(entry);

               }
             }
         }
     }
   }
Ankit
  • 6,554
  • 6
  • 49
  • 71
user2559055
  • 73
  • 3
  • 11

2 Answers2

0

You do not need to write a Java program to do this. You could use a shell script. You could just unzip the jar file, and then find the files in the directory and mv them into the directory you have.

Kaushik Shankar
  • 5,491
  • 4
  • 30
  • 36
0

Here is a class that does this. You probably have to modify it a little.

public class HtDocsExtractor {
    private final String htDocsPath;

    public HtDocsExtractor(String htDocsPath) {
        this.htDocsPath = htDocsPath;
    }

    public void extract() throws Exception {

        InputStream is = HtDocsExtractor.class.getResourceAsStream("/htdocs.zip");
        ZipInputStream zis = new ZipInputStream(is);
        try {
            byte[] buf = new byte[8192];
            ZipEntry zipentry;

            zipentry = zis.getNextEntry();
            while (zipentry != null) {
                String entryName = htDocsPath + zipentry.getName();
                entryName = entryName.replace('/', File.separatorChar);
                entryName = entryName.replace('\\', File.separatorChar);
                int n;
                File newFile = new File(entryName);
                if (zipentry.isDirectory()) {
                    if (!newFile.exists() && !newFile.mkdirs()) {
                        throw new Exception("Could not create directory: " + newFile);
                    }
                    zipentry = zis.getNextEntry();
                }
                else {
                    FileOutputStream fos = new FileOutputStream(entryName);
                    try {
                        while ((n = zis.read(buf)) > 0) {
                            fos.write(buf, 0, n);
                        }
                    } finally {
                        fos.close();
                    }
                    zis.closeEntry();
                    zipentry = zis.getNextEntry();
                }
            }
        } finally {
            zis.close();
        }

    }
}
Sarel Botha
  • 12,419
  • 7
  • 54
  • 59
  • I think this is hard way to do it.. i need understand program.. acutally.. how its work?? If i changed file name htdocs.zip to my file name it should work right – user2559055 Jul 18 '13 at 21:46