I am having a zip file into my project. When I am running my code through IDE, my extract(String file, String destination)
method works fine.
D:/Tools/JAVA/Lodable_Creation/build/classes/ib2.zip-->
String s1=getClass().getResource("Test.zip").getPath().toString();
extract(s1, "c:\\");
This is giving me Path s1 is--> D:\\Tools\\JAVA\\Lodable_Creation\\build
When I compile same code and run through Command prompt
file:/D:/Tools/JAVA/Lodable_Creation/dist/Lodable_Creation.jar!/Test.zip
s1 is-->D:\Tools\JAVA\Lodable_Creation\dist
And I am not getting output. Please help me.
UPDATE:-
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);
This is my actual code for extract method and on OK Button . This is extracting Test.zip file to the Destination folder. ie CYGWIN_HOME
如果您的文件路径实际上是URL(以"file://"
开头),则使用new ZipInputStream((new URL(file)).openStream())
否则使用new ZipInputStream(new FileInputStream(file))
就像您已经做过的一样。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.