Skip to content

Instantly share code, notes, and snippets.

@sulmansarwar
Last active December 20, 2015 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sulmansarwar/6086552 to your computer and use it in GitHub Desktop.
Save sulmansarwar/6086552 to your computer and use it in GitHub Desktop.
unzip the zipArchive. Takes the zipfile location and output directory.
/**
* 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