2
我在我的项目中有一个zip文件。当我通过IDE运行我的代码时,我的extract(String file, String destination)
方法正常工作。如何从jar文件中提取zip文件
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
当我编译相同的代码,并通过运行命令提示符
file:/D:/Tools/JAVA/Lodable_Creation/dist/Lodable_Creation.jar!/Test.zip
s1 is-->D:\Tools\JAVA\Lodable_Creation\dist
而且我没有得到输出。请帮帮我。
更新: -
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文件解压缩到目标文件夹。即CYGWIN_HOME
我猜测“Test.zip”没有找到,可能是因为它不在你的类路径中的正确位置。 – 2012-02-27 12:22:55
虽然看起来更深我不知道有什么作品。它似乎不像“Test.zip”上的getResource可以工作。我认为你需要发布你的真实代码。 – 2012-02-27 12:29:03
'extract()'的代码是...? – 2012-02-27 12:30:12