2012-02-27 11 views
2

私のプロジェクトにはzipファイルがあります。 IDEでコードを実行しているときに、私のextract(String file, String destination)メソッドが正常に動作します。私はjarファイルからzipファイルを解凍する方法

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

をコマンドプロンプトを通じて同じコードをコンパイルして実行すると

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を与えていると私は出力を得ていないのです。私を助けてください。

UPDATE: -

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ファイルをDestinationフォルダに展開します。すなわちCYGWIN_HOME

+0

「Test.zip」が見つかりません。おそらくクラスパスの適切な場所にないためです。 –

+0

もっと深く見ていても、何がどういう仕組みかと思います。 "Test.zip"上のgetResourceのようには動作しないようです。私はあなたの本当のコードを投稿する必要があると思います。 –

+0

そして 'extract()'のコードは...ですか? –

答えて

0

ファイル・パスは、実際にはURL("file://"で始まる)をすでに行うようにそうnew ZipInputStream(new FileInputStream(file))を使用new ZipInputStream((new URL(file)).openStream())を使用している場合。

関連する問題