提取jar内容并存储在目标文件夹中
问题描述:
如何提取所有文件夹,子文件夹,子文件夹内存储的任何jar文件和opy这些内容到java中的目标文件夹中的文件夹和文件夹编码在eclipse平台......... 任何人都可以建议我... 我尝试了许多与zipinput流有关的代码..在枚举中存储zipfile实体并将其写入detination文件夹。但没有它的正常工作......提取jar内容并存储在目标文件夹中
答
如果你想要一个基于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
答
您可以使用这个非常简单的库来打包/解压缩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());
}
您还可以使用行家依赖
<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>
检查的最后一个版本的链接钟嗷嗷利用最后的依赖
请使用我的public issue tracker,如果你发现了一些错误
让我们了解您到目前为止编码。 – adarshr 2011-04-15 08:45:06