use of SMExceptions.PackUnpackException in project SmartCity-Market by TechnionYP5777.
the class Packing method unpack.
public static void unpack(ZipFile zfile, String unpackPath) throws SMException {
if (zfile == null || unpackPath == null || unpackPath.isEmpty()) {
log.fatal("unpacking failed due to invalid parameter.");
throw new PackUnpackException();
}
File unpackDir;
try {
unpackDir = new File(unpackPath);
} catch (Exception e) {
log.error(e + "");
throw new PackUnpackException();
}
if (!unpackDir.isDirectory()) {
log.error("Unpacking failed: Unpack path must be a directory");
throw new PackUnpackException();
}
try {
BufferedOutputStream dest = null;
BufferedInputStream is = null;
ZipEntry entry;
for (Enumeration<? extends ZipEntry> e = zfile.entries(); e.hasMoreElements(); ) {
entry = e.nextElement();
log.debug("Extracting: " + entry);
is = new BufferedInputStream(zfile.getInputStream(entry));
int count;
byte[] data = new byte[bufferSize];
FileOutputStream fos = new FileOutputStream(unpackDir.getAbsolutePath() + '/' + entry.getName());
dest = new BufferedOutputStream(fos, bufferSize);
while ((count = is.read(data, 0, bufferSize)) != -1) dest.write(data, 0, count);
dest.flush();
dest.close();
is.close();
}
} catch (Exception e) {
log.error(e + "");
log.error("Failed extracting zipfile " + zfile + " into " + unpackDir.getAbsolutePath());
}
}
Aggregations