Как извлечь zip-файл из jar-файла

У меня есть zip-файл в моем проекте. Когда я запускаю свой код через IDE, мой метод extract(String file, String destination) работает нормально.

 D:/Tools/JAVA/Lodable_Creation/build/classes/ib2.zip-->
 String s1=getClass().getResource("Test.zip").getPath().toString();
  extract(s1, "c:\\");

Это дает мне Путь s1 is--> D:\Tools\JAVA\Lodable_Creation\build

Когда я компилирую тот же код и запускаю командную строку

file:/D:/Tools/JAVA/Lodable_Creation/dist/Lodable_Creation.jar!/Test.zip
s1 is-->D:\Tools\JAVA\Lodable_Creation\dist

И у меня не выходит. Помогите мне, пожалуйста.

ОБНОВЛЕНИЕ:-

public static void extract(String file, String destination) throws IOException {
    ZipInputStream in = null;
    OutputStream out = null;
    try {
      // Open the ZIP file
      in = new ZipInputStream(new FileInputStream(file));
      // Get the first entry
      ZipEntry entry = null;
      while ((entry = in.getNextEntry()) != null) {
        String outFilename = entry.getName();
        // Open the output file
        if (entry.isDirectory()) {
          new File(destination, outFilename).mkdirs();
        } else {
          out = new FileOutputStream(new File(destination,outFilename));
          // Transfer bytes from the ZIP file to the output file
          byte[] buf = new byte[1024];
          int len;
          while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
          }
          out.close();
        }
      }
    } finally {
      // Close the stream
      if (in != null) {
        in.close();
      }
      if (out != null) {
        out.close();
      }
    }
  }

On Ok Click button

Map map = System.getenv();
 Set keys = map.keySet();
 String newString  = (String) map.get("CYGWIN_HOME");
 System.out.println(" " + newString);
 String  destination= newString.replace(";", "");
 System.out.println(" " + destination);
 String S =getClass().getResource("Test.zip").getPath().toString();
 File jarFile = new File(S);
 String file=jarFile.toString();
 extract(file,destination);

Это мой фактический код для метода извлечения и кнопки OK. Это извлечение файла Test.zip в папку назначения. то есть CYGWIN_HOME


person Code Hungry    schedule 27.02.2012    source источник
comment
Я предполагаю, что Test.zip не найден, вероятно, потому, что он не находится в нужном месте в вашем пути к классам.   -  person Hot Licks    schedule 27.02.2012
comment
Хотя, глядя глубже, мне интересно, как что-то работает. Не похоже, чтобы getResource в Test.zip когда-либо работал. Я думаю, вам нужно опубликовать свой РЕАЛЬНЫЙ код.   -  person Hot Licks    schedule 27.02.2012
comment
А код extract() есть...?   -  person JB Nizet    schedule 27.02.2012


Ответы (1)


Если ваш путь к файлу на самом деле является URL-адресом (начинается с "file://"), используйте new ZipInputStream((new URL(file)).openStream()), в противном случае используйте new ZipInputStream(new FileInputStream(file)), как вы уже делали.

person martijno    schedule 15.08.2012