首頁 > Java > java教程 > 主體

如何將JAR檔案內容提取到指定目錄?

Mary-Kate Olsen
發布: 2024-10-27 00:53:31
原創
435 人瀏覽過

How to Extract JAR File Contents into a Specified Directory?

將JAR 文件內容提取到指定目錄

問題:

您有一個JAR 文件,需要提取其內容到特定目錄。執行“jar -xf filename.jar”命令會回傳錯誤。

解決方案:

使用現有範例:

  • 請參閱本指南,了解如何從JAR和ZIP 檔案中提取Java 資源:https://examples.javacodegeeks.com/core-java/java-util-jar-extract-resources-from-jar-and-zip -archive-example/.

自訂程式碼:

或者,使用以下程式碼:

<code class="java">// JAR file to extract
java.util.jar.JarFile jar = new java.util.jar.JarFile(jarFile);

// Extract all entries
java.util.Enumeration enumEntries = jar.entries();
while (enumEntries.hasMoreElements()) {
    // Get the entry
    java.util.jar.JarEntry file = (java.util.jar.JarEntry) enumEntries.nextElement();

    // Create a file object
    java.io.File f = new java.io.File(destDir + java.io.File.separator + file.getName());

    // If the entry is a directory, create it
    if (file.isDirectory()) {
        f.mkdir();
        continue;
    }

    // Otherwise, extract the file
    java.io.InputStream is = jar.getInputStream(file); // Input stream
    java.io.FileOutputStream fos = new java.io.FileOutputStream(f); // Output stream

    // Write the file contents
    while (is.available() > 0) {
        fos.write(is.read());
    }

    // Close streams
    fos.close();
    is.close();
}

// Close the JAR file
jar.close();</code>
登入後複製

來源: http://www.devx .com/tips/Tip/22124

以上是如何將JAR檔案內容提取到指定目錄?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!