Last active
December 20, 2015 06:29
-
-
Save sulmansarwar/6086552 to your computer and use it in GitHub Desktop.
unzip the zipArchive. Takes the zipfile location and output directory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Takes the zipfile location and output directory and returns the reference to output folder | |
* @param zfile | |
* @param outDir | |
* @throws IOException | |
* | |
*/ | |
public File unzep(String zfile, String outDir) throws IOException | |
{ | |
ZipInputStream zis = null; | |
FileOutputStream fos = null; | |
try | |
{ | |
zis = new ZipInputStream(new FileInputStream(zfile)); | |
{ | |
byte[] buffer = new byte[4096]; | |
ZipEntry ze; | |
File directory = new File(outDir); | |
if(!directory.exists()) | |
{ | |
// | |
// If not, create a new one. | |
// | |
outDir = new File(outDir).mkdir(); | |
System.err.println("...Directory Created -"+outDir); | |
} | |
while ((ze = zis.getNextEntry()) != null) | |
{ | |
LOGGER.info("Extracting: "+ze +"["+outDir+"/"+ze.getName()+"]"); | |
fos = new FileOutputStream(outDir+"/"+ze.getName()); | |
int numBytes; | |
while ((numBytes = zis.read(buffer, 0, buffer.length)) != -1) | |
fos.write(buffer, 0, numBytes); | |
zis.closeEntry(); | |
} | |
} | |
} | |
catch(Exception ex) | |
{ | |
LOGGER.error("",ex); | |
} | |
finally{ | |
if(zis!=null) | |
{ | |
zis.close(); | |
} | |
if(fos!=null) | |
{ | |
fos.close(); | |
} | |
} | |
return outDir; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment