2012-05-07 20 views
2

現在実行中のjarファイルから2つのjarファイルを抽出しようとしていますが、サイズが104kbと1.7mであっても常に2kbになります。現在実行中のjarファイルからjarファイルを抽出する

public static boolean extractFromJar(String fileName, String dest) { 
    if (Configuration.getRunningJarPath() == null) { 
     return false; 
    } 
    File file = new File(dest + fileName); 
    if (file.exists()) { 
     return false; 
    } 

    if (file.isDirectory()) { 
     file.mkdir(); 
     return false; 
    } 
    try { 
     JarFile jar = new JarFile(Configuration.getRunningJarPath()); 
     Enumeration<JarEntry> e = jar.entries(); 
     while (e.hasMoreElements()) { 
      JarEntry je = e.nextElement(); 
      InputStream in = new BufferedInputStream(jar.getInputStream(je)); 
      OutputStream out = new BufferedOutputStream(
        new FileOutputStream(file)); 
      copyInputStream(in, out); 
     } 
     return true; 
    } catch (Exception e) { 
     Methods.debug(e); 
     return false; 
    } 
} 

private final static void copyInputStream(InputStream in, OutputStream out) 
     throws IOException { 
    while (in.available() > 0) { 
     out.write(in.read()); 
    } 
    out.flush(); 
    out.close(); 
    in.close(); 
} 

答えて

2

これは(InputStream.availableに頼って、その後、より良い動作するはず)方法:

private final static void copyInputStream(InputStream in, OutputStream out) 
     throws IOException { 
    byte[] buff = new byte[4096]; 
    int n; 
    while ((n = in.read(buff)) > 0) { 
     out.write(buff, 0, n); 
    } 
    out.flush(); 
    out.close(); 
    in.close(); 
} 
0

私はjarの抽出についてはわかりませんが、すべてのjarファイルは実際にはzipファイルなので、解凍してみることができます。

あなたがここにJavaでunzipingについてfindoutすることができます How to unzip files recursively in Java?

1

available()方法はそれとして、データを読み込むことが信頼できるものではありませんそのドキュメントのとおり、単なる見積もりに過ぎません。
read()メソッドに依存する必要があります。

byte[] contentBytes = new byte[ 4096 ]; 
int bytesRead = -1; 
while ((bytesRead = inputStream.read(contentBytes)) > 0) 
{ 
    out.write(contentBytes, 0, bytesRead); 
} // while available 

あなたはavailable()に問題がhereであるかについての議論を経ることができます。

関連する問題