java decompress jar file

/**
* Unzip the JAR package
* @param fileName filename
* @param outputPath decompression output path
* @throws IOException IO exception
*/
private void decompress(String fileName, String outputPath) throws IOException{

if (!outputPath.endsWith(File.separator)) {

outputPath += File.separator;

}

JarFile jf = new JarFile(fileName);

for (Enumeration e = jf.entries(); e.hasMoreElements();)
{
JarEntry is = (JarEntry) e.nextElement ();
String outFileName = outputPath + je.getName();
File f = new File(outFileName);
System.out.println(f.getAbsolutePath());

//Create a directory and all parent directories for this path
makeSupDir(outFileName);

//If it is a directory, go directly to the next loop
if(f.isDirectory())
{
continue;
}

InputStream in = null;
OutputStream out = null;

try
{
in = jf.getInputStream(you);
out = new BufferedOutputStream(new FileOutputStream(f));
byte[] buffer = new byte[2048];
int nBytes = 0;
while ((nBytes = in.read(buffer)) > 0)
{
out.write(buffer, 0, nBytes);
}
} catch (IOException yes)
{
throw ioe;
} finally
{
try
{
if (null != out)
{
out.flush();
out.close();
}
} catch (IOException yes)
{
throw ioe;
}
                   finally
                   {
if (null != in)
{
in.close();
}
}
                   }
}
}

/**
* Loop to create parent directory
* @param outFileName
*/
private void makeSupDir(String outFileName) {
// match delimiter
Pattern p = Pattern.compile("[/\\" + File.separator + "]");
Matcher m = p.matcher(outFileName);
//Every time a matching delimiter is found, create a directory before the delimiter
while (m.find()) {
int index = m.start();
String subDir = outFileName.substring(0, index);
File subDirFile = new File(subDir);
if (!subDirFile.exists())
subDirFile.mkdir();
}
}

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327042576&siteId=291194637