/**
* Decomprime uno ZipInputStream in una directory destinazione
* @param input ZipInputStream da estrarre
* @param output directory destinazione
* @throws IOException
*/
public static void decompress(ZipInputStream input, File output) throws IOException
{
ZipEntry zipEntry;
while((zipEntry = input.getNextEntry()) != null)
{
boolean directory = zipEntry.isDirectory();
if(directory)
{
File dir = new File(output, zipEntry.getName());
if(!dir.exists())
dir.mkdir();
else if(dir.isDirectory())
throw new IOException("Output directory \"" + dir.getAbsolutePath() + "\" is a file");
}else
{
File decompressFile = new File(output, zipEntry.getName());
if(decompressFile.exists())
throw new IOException("Output file \"" + decompressFile.getAbsolutePath() + "\" already exists");
FileOutputStream fos = new FileOutputStream(decompressFile);
try{
byte[] readBuffer = new byte[4096];
int bytesIn = 0;
while ((bytesIn = input.read(readBuffer)) != -1) {
fos.write(readBuffer, 0, bytesIn);
}
}
finally{
fos.close();
}
}
}
}
Invocazione tipica del metodo,
input: file da estrarre, output: directory destinazione
/**
* Decomprime un file zip in una directory
* @param input file zip da estrarre
* @param output directory destinazione
* @throws IOException
*/
public static void decompress(File input, File output) throws IOException
{
ZipInputStream zis = new ZipInputStream(new FileInputStream(input));
try{
decompress(zis, output);
}finally
{
zis.close();
}
}
Nessun commento:
Posta un commento