Search in sources :

Example 1 with PackUnpackException

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());
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) PackUnpackException(SMExceptions.PackUnpackException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) PackUnpackException(SMExceptions.PackUnpackException) SMException(SMExceptions.SMException)

Aggregations

PackUnpackException (SMExceptions.PackUnpackException)1 SMException (SMExceptions.SMException)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1