![](/img/trans.png)
[英]Extract a particular folder from a jar and copy it to a desired destination on my system
[英]extract jar content and stored in destination folder
我如何提取所有存儲在任何jar文件中的所有文件夾,子文件夾,所有文件以及子文件夾中的所有文件和文件夾,然后將這些內容選入Java中的目標文件夾中,同時在Eclipse平台中進行編碼....... ..誰能建議我...我嘗試了許多與zipinput流相關的代碼..在枚舉中存儲在zipfile實體中並將其寫入目標文件夾。 但是沒有一個能正常工作......
您可以使用這個非常簡單的庫來打包/解壓縮jar文件
很簡單
import java.io.File;
import java.util.List;
import fr.stevecohen.jarmanager.JarUnpacker;
class Test {
JarUnpacker jarUnpacker = new JarUnpacker();
File myfile = new File("./myfile.jar");
File unpackDir = new File("./mydir");
List<File> unpacked_files = jarUnpacker.unpack(myfile.getAbsolutePath(), unpackDir.getAbsolutePath());
}
您還可以使用Maven依賴
<dependency>
<groupId>fr.stevecohen.jarmanager</groupId>
<artifactId>JarManager</artifactId>
<version>0.5.0</version>
</dependency>
您還需要我的存儲庫
<repository>
<id>repo-reapersoon</id>
<name>ReaperSoon's repo</name>
<url>http://repo-maven.stevecohen.fr</url>
</repository>
使用鏈接下面的鏈接檢查最新版本以使用最新依賴
如果發現一些錯誤,請使用我的公共問題跟蹤器
如果需要基於Eclipse的解決方案,請選擇
File menu > Import ... > General > Archive File
但是,如果您需要Java代碼解決方案,可以使用Guava :
public static void unpackZipFile(final File archive, final File targetDirectory)
throws ZipException, IOException {
ZipFile zipFile = new ZipFile(archive);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
final ZipEntry zipEntry = entries.nextElement();
if (zipEntry.isDirectory())
continue;
final File targetFile = new File(targetDirectory,
zipEntry.getName());
Files.createParentDirs(targetFile);
ByteStreams.copy(zipFile.getInputStream(zipEntry), Files
.newOutputStreamSupplier(targetFile).getOutput());
}
}
我編寫了一個幫助程序類來做到這一點:
http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/util/zip/ZipFiles.html
您可以在我的博客中閱讀有關最新版本及其獲取方式的更多信息:
http://puces-blog.blogspot.com/2011/03/news-from-software-smithy.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.