Example usage for java.util.zip ZipInputStream read

List of usage examples for java.util.zip ZipInputStream read

Introduction

In this page you can find the example usage for java.util.zip ZipInputStream read.

Prototype

public int read() throws IOException 

Source Link

Document

Reads a byte of uncompressed data.

Usage

From source file:MainClass.java

public static void main(String[] args) throws IOException {

    for (int i = 0; i < args.length; i++) {
        FileInputStream fin = new FileInputStream(args[i]);
        ZipInputStream zin = new ZipInputStream(fin);
        ZipEntry ze = null;// w ww.ja v  a2s .  c  o m
        while ((ze = zin.getNextEntry()) != null) {
            System.out.println("Unzipping " + ze.getName());
            FileOutputStream fout = new FileOutputStream(ze.getName());
            for (int c = zin.read(); c != -1; c = zin.read()) {
                fout.write(c);
            }
            zin.closeEntry();
            fout.close();
        }
        zin.close();
    }
}

From source file:Main.java

/**
 * Decompresses a given byte array that is a compressed folder.
 * //from   w  w  w .  jav  a  2 s  . c  o m
 * @param folderAsCompressedArray to decompress
 * @param unzippedLocation where the decompressed folder should be
 * @throws IOException e
 * @throws FileNotFoundException e
 */
public static void decompressFolderByteArray(byte[] folderAsCompressedArray, File unzippedLocation)
        throws IOException, FileNotFoundException {
    ZipInputStream zipFile = new ZipInputStream(new ByteArrayInputStream(folderAsCompressedArray));
    ZipEntry ze = null;
    final int minusOne = -1;
    while ((ze = zipFile.getNextEntry()) != null) {
        FileOutputStream fout = new FileOutputStream(
                new File(unzippedLocation, ze.getName()).getAbsolutePath());
        for (int c = zipFile.read(); c != minusOne; c = zipFile.read()) {
            fout.write(c);
        }
        zipFile.closeEntry();
        fout.close();
    }
    zipFile.close();
}

From source file:com.jlgranda.fede.ejb.url.reader.FacturaElectronicaURLReader.java

/**
 * Obtiene la lista de objetos factura para el sujeto en fede
 *
 * @param urls URLs hacia los archivo XML o ZIP a leer
 * @return una lista de instancias FacturaReader
 *//* ww w . j  ava 2  s  .  c  om*/
public static FacturaReader getFacturaElectronica(String url) throws Exception {
    FacturaReader facturaReader = null;

    if (url.endsWith(".xml")) {
        String xml = FacturaElectronicaURLReader.read(url);
        facturaReader = new FacturaReader(FacturaUtil.read(xml), xml, url);
    } else if (url.endsWith(".zip")) {
        URL url_ = new URL(url);
        InputStream is = url_.openStream();
        ZipInputStream zis = new ZipInputStream(is);
        try {

            ZipEntry entry = null;
            String tmp = null;
            ByteArrayOutputStream fout = null;
            while ((entry = zis.getNextEntry()) != null) {
                if (entry.getName().toLowerCase().endsWith(".xml")) {
                    fout = new ByteArrayOutputStream();
                    for (int c = zis.read(); c != -1; c = zis.read()) {
                        fout.write(c);
                    }

                    tmp = new String(fout.toByteArray(), Charset.defaultCharset());
                    facturaReader = new FacturaReader(FacturaUtil.read(tmp), tmp, url);
                    fout.close();
                }
                zis.closeEntry();
            }
            zis.close();

        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(zis);
        }
    }

    return facturaReader;
}

From source file:reconcile.hbase.mapreduce.ZipInputFormat.java

private static byte[] read(String entry, ZipInputStream zis, int numBytes) {
    byte[] data = new byte[numBytes];
    int n = 0;// w  w  w  .  j  av  a2s . c  o  m
    try {
        while (zis.available() == 1 && (n < numBytes)) {
            data[n] = (byte) zis.read();
            ++n;
        }
    } catch (IOException e) {
        LOG.error("failure reading zip entry(" + entry + ")");
        e.printStackTrace();
        return null;
    }
    LOG.info("Read bytes(" + n + ") from entry (" + entry + ")");
    LOG.debug("Read value(" + Bytes.toString(data) + ") from entry (" + entry + ")");

    return data;
}

From source file:edu.clemson.cs.nestbed.common.util.ZipUtils.java

public static File unzip(byte[] zipData, File directory) throws IOException {
    ByteArrayInputStream bais = new ByteArrayInputStream(zipData);
    ZipInputStream zis = new ZipInputStream(bais);
    ZipEntry entry = zis.getNextEntry();
    File root = null;//from  w  w w .  j  a  v  a2  s  .c  o  m

    while (entry != null) {
        if (entry.isDirectory()) {
            File f = new File(directory, entry.getName());
            f.mkdir();

            if (root == null) {
                root = f;
            }
        } else {
            BufferedOutputStream out;
            out = new BufferedOutputStream(new FileOutputStream(new File(directory, entry.toString())),
                    BUFFER_SIZE);

            // ZipInputStream can only give us one byte at a time...
            for (int data = zis.read(); data != -1; data = zis.read()) {
                out.write(data);
            }
            out.close();
        }

        zis.closeEntry();
        entry = zis.getNextEntry();
    }
    zis.close();

    return root;
}

From source file:org.openremote.modeler.utils.ZipUtils.java

/**
 * Unzip a zip./* ww  w .ja  va 2  s. c  om*/
 * 
 * @param inputStream the input stream
 * @param targetDir the target dir
 * 
 * @return true, if success
 */
public static boolean unzip(InputStream inputStream, String targetDir) {
    ZipInputStream zipInputStream = new ZipInputStream(inputStream);
    ZipEntry zipEntry;
    FileOutputStream fileOutputStream = null;
    try {
        while ((zipEntry = zipInputStream.getNextEntry()) != null) {
            if (!zipEntry.isDirectory()) {
                targetDir = targetDir.endsWith("/") || targetDir.endsWith("\\") ? targetDir : targetDir + "/";
                File zippedFile = new File(targetDir + zipEntry.getName());
                FileUtilsExt.deleteQuietly(zippedFile);
                fileOutputStream = new FileOutputStream(zippedFile);
                int b;
                while ((b = zipInputStream.read()) != -1) {
                    fileOutputStream.write(b);
                }
                fileOutputStream.close();
            }
        }
    } catch (IOException e) {
        LOGGER.error("Can't unzip to " + targetDir, e);
        return false;
    } finally {
        try {
            zipInputStream.closeEntry();
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
        } catch (IOException e) {
            LOGGER.error("Error while closing stream.", e);
        }

    }
    return true;
}

From source file:functionalTests.vfsprovider.TestProActiveProvider.java

public static void extractZip(final ZipInputStream zipStream, final File dstFile) throws IOException {
    ZipEntry zipEntry;//ww w  . ja  v  a2  s .co m
    while ((zipEntry = zipStream.getNextEntry()) != null) {
        final File dstSubFile = new File(dstFile, zipEntry.getName());

        if (zipEntry.isDirectory()) {
            dstSubFile.mkdirs();
            if (!dstSubFile.exists() || !dstSubFile.isDirectory())
                throw new IOException("Could not create directory: " + dstSubFile);
        } else {
            final OutputStream os = new BufferedOutputStream(new FileOutputStream(dstSubFile));
            try {
                int data;
                while ((data = zipStream.read()) != -1)
                    os.write(data);
            } finally {
                os.close();
            }
        }
    }
}

From source file:org.openremote.controller.utils.ZipUtil.java

/**
 * Unzip a zip.//from   ww  w. j  a v  a  2 s  .  c o  m
 * 
 * @param inputStream the input stream
 * @param targetDir the target dir
 * 
 * @return true, if success
 */
public static boolean unzip(InputStream inputStream, String targetDir) {
    if (targetDir == null || "".equals(targetDir)) {
        throw new ExtractZipFileException("The resources path is null.");
    }
    File checkedTargetDir = new File(targetDir);
    if (!checkedTargetDir.exists()) {
        throw new ExtractZipFileException("The path " + targetDir + " doesn't exist.");
    }

    ZipInputStream zipInputStream = new ZipInputStream(inputStream);
    ZipEntry zipEntry;
    FileOutputStream fileOutputStream = null;
    File zippedFile = null;
    try {
        while ((zipEntry = zipInputStream.getNextEntry()) != null) {
            if (!zipEntry.isDirectory()) {
                targetDir = targetDir.endsWith("/") || targetDir.endsWith("\\") ? targetDir : targetDir + "/";
                zippedFile = new File(targetDir, zipEntry.getName());
                FileUtils.deleteQuietly(zippedFile);
                FileUtils.touch(zippedFile);
                fileOutputStream = new FileOutputStream(zippedFile);
                int b;
                while ((b = zipInputStream.read()) != -1) {
                    fileOutputStream.write(b);
                }
                fileOutputStream.close();
            }
        }
    } catch (IOException e) {
        logger.error("Can't unzip file to " + zippedFile.getPath(), e);
        return false;
    } finally {
        try {
            zipInputStream.closeEntry();
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
        } catch (IOException e) {
            logger.error("Error while closing stream.", e);
        }

    }
    return true;
}

From source file:Main.java

/**
 * kudos: http://www.jondev.net/articles/Unzipping_Files_with_Android_(Programmatically)
 * @param zipFile//from  w  ww . ja va2s .c o m
 * @param destinationDirectory
 */
public static void unZip(String zipFile, String destinationDirectory) {
    try {
        FileInputStream fin = new FileInputStream(zipFile);
        ZipInputStream zin = new ZipInputStream(fin);
        ZipEntry ze = null;
        while ((ze = zin.getNextEntry()) != null) {
            Log.v("Decompress", "Unzipping " + ze.getName());
            String destinationPath = destinationDirectory + File.separator + ze.getName();
            if (ze.isDirectory()) {
                dirChecker(destinationPath);
            } else {
                FileOutputStream fout;
                try {
                    File outputFile = new File(destinationPath);
                    if (!outputFile.getParentFile().exists()) {
                        dirChecker(outputFile.getParentFile().getPath());
                    }
                    fout = new FileOutputStream(destinationPath);
                    for (int c = zin.read(); c != -1; c = zin.read()) {
                        fout.write(c);
                    }
                    zin.closeEntry();
                    fout.close();
                } catch (Exception e) {
                    // ok for now.
                    Log.v("Decompress", "Error: " + e.getMessage());
                }
            }
        }
        zin.close();
    } catch (Exception e) {
        Log.e("Decompress", "unzip", e);
    }
}

From source file:org.lastmilehealth.collect.android.utilities.ZipUtils.java

/**private static void addFile(File fileObj, ZipOutputStream out) throws IOException {
//Log.d("~",  "Add file: "+fileObj.getName());
out.putNextEntry(new ZipEntry(fileObj.getName()));
out.closeEntry();/*from   w w  w . java 2 s. c  om*/
}*/

public static void unzip(String zipFile, String targetLocation) throws Exception {
    //delete target location to clear previously unzipped data
    try {
        org.apache.commons.io.FileUtils.deleteDirectory(new File(targetLocation));
    } catch (Exception e) {
    }
    createDirIfNotExist(targetLocation);

    FileInputStream fin = new FileInputStream(zipFile);
    ZipInputStream zin = new ZipInputStream(fin);
    ZipEntry ze = null;
    while ((ze = zin.getNextEntry()) != null) {
        //create dir if required while unzipping
        if (ze.isDirectory()) {
            createDirIfNotExist(ze.getName());
        } else {
            String path = targetLocation + "/" + ze.getName();
            Log.d(TAG, "unzipping: " + path);

            if (path.contains("/")) {
                org.apache.commons.io.FileUtils.forceMkdir(new File(path.substring(0, path.lastIndexOf("/"))));
            }

            FileOutputStream fout = new FileOutputStream(path);
            for (int c = zin.read(); c != -1; c = zin.read()) {
                fout.write(c);
            }
            zin.closeEntry();
            fout.close();
        }
    }
    zin.close();
}