Example usage for java.util.zip ZipInputStream getNextEntry

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

Introduction

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

Prototype

public ZipEntry getNextEntry() throws IOException 

Source Link

Document

Reads the next ZIP file entry and positions the stream at the beginning of the entry data.

Usage

From source file:de.suse.swamp.util.FileUtils.java

/**
 * Decompress the provided Stream to "targetPath"
 *///w  ww.j a  v a 2s  . c  o m
public static void uncompress(InputStream inputFile, String targetPath) throws Exception {
    ZipInputStream zin = new ZipInputStream(new BufferedInputStream(inputFile));
    BufferedOutputStream dest = null;
    ZipEntry entry;
    while ((entry = zin.getNextEntry()) != null) {
        int count;
        byte data[] = new byte[2048];
        // write the files to the disk
        if (entry.isDirectory()) {
            org.apache.commons.io.FileUtils.forceMkdir(new File(targetPath + "/" + entry.getName()));
        } else {
            FileOutputStream fos = new FileOutputStream(targetPath + "/" + entry.getName());
            dest = new BufferedOutputStream(fos, 2048);
            while ((count = zin.read(data, 0, 2048)) != -1) {
                dest.write(data, 0, count);
            }
            dest.flush();
            dest.close();
        }
    }
}

From source file:com.github.stagirs.lingvo.build.Xml2Plain.java

private static List<String> extract(File file) throws IOException {
    ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
    try {/*from   ww w.jav  a 2s. co m*/
        ZipEntry ze = zis.getNextEntry();
        if (ze == null) {
            throw new RuntimeException("can't unzip file");
        }
        return IOUtils.readLines(zis, Charset.forName("utf-8"));
    } finally {
        zis.close();
    }
}

From source file:Main.java

public static void unzip(InputStream fin, String targetPath, Context context) throws IOException {
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fin));
    try {//from   w ww  . j av a 2 s.  com
        ZipEntry zipEntry;
        int count;
        byte[] buffer = new byte[8192];
        while ((zipEntry = zis.getNextEntry()) != null) {
            File file = new File(targetPath, zipEntry.getName());
            File dir = zipEntry.isDirectory() ? file : file.getParentFile();
            if (!dir.isDirectory() && !dir.mkdirs()) {
                throw new FileNotFoundException("Failed to get directory: " + dir.getAbsolutePath());
            }
            if (zipEntry.isDirectory()) {
                continue;
            }
            FileOutputStream fout = new FileOutputStream(file);
            try {
                while ((count = zis.read(buffer)) != -1)
                    fout.write(buffer, 0, count);
            } finally {
                fout.close();
            }
            Log.d("TEST", "Unzipped " + file.getAbsolutePath());
        }
    } finally {
        zis.close();
    }
}

From source file:gabi.FileUploadServlet.java

private static void unzip(String zipFilePath, String destDir) {
    File dir = new File(destDir);
    // create output directory if it doesn't exist
    if (!dir.exists())
        dir.mkdirs();/*from   w ww. j a  v  a2 s  . com*/
    FileInputStream fis;
    //buffer for read and write data to file
    byte[] buffer = new byte[1024];
    try {
        fis = new FileInputStream(zipFilePath);
        ZipInputStream zis = new ZipInputStream(fis);
        ZipEntry ze = zis.getNextEntry();
        while (ze != null) {
            String fileName = ze.getName();
            File newFile = new File(destDir + File.separator + fileName);
            System.out.println("Unzipping to " + newFile.getAbsolutePath());
            //create directories for sub directories in zip
            new File(newFile.getParent()).mkdirs();
            FileOutputStream fos = new FileOutputStream(newFile);
            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
            fos.close();
            //close this ZipEntry
            zis.closeEntry();
            ze = zis.getNextEntry();
        }
        //close last ZipEntry
        zis.closeEntry();
        zis.close();
        fis.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:eionet.gdem.conversion.odf.OpenDocumentUtils.java

/**
 * Returns true, if inputstream is zip file
 * @param input InputStream//w  w  w .  j a  va 2s  .  c  om
 * @return True if InputStream is a zip file.
 */
public static boolean isSpreadsheetFile(InputStream input) {

    ZipInputStream zipStream = null;
    ZipEntry zipEntry = null;
    try {
        zipStream = new ZipInputStream(input);
        while (zipStream.available() == 1 && (zipEntry = zipStream.getNextEntry()) != null) {
            if (zipEntry != null) {
                if ("content.xml".equals(zipEntry.getName())) {
                    // content file found, it is OpenDocument.
                    return true;
                }
            }
        }
    } catch (IOException ioe) {
        return false;
    } finally {
        IOUtils.closeQuietly(zipStream);
    }
    return false;

}

From source file:Main.java

public static void unzip(File zipFile, File targetDirectory) throws IOException {
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile)));
    try {//  w  w w  .  j  av a 2  s.com
        ZipEntry ze;
        int count;
        byte[] buffer = new byte[8192];
        while ((ze = zis.getNextEntry()) != null) {
            File file = new File(targetDirectory, ze.getName());
            File dir = ze.isDirectory() ? file : file.getParentFile();
            if (!dir.isDirectory() && !dir.mkdirs())
                throw new FileNotFoundException("Failed to ensure directory: " + dir.getAbsolutePath());
            if (ze.isDirectory())
                continue;
            FileOutputStream fout = new FileOutputStream(file);
            try {
                while ((count = zis.read(buffer)) != -1)
                    fout.write(buffer, 0, count);
            } finally {
                fout.close();
            }
        }
    } finally {
        zis.close();
    }
}

From source file:ZipHandler.java

/**
 * unzipps a zip file placed at <zipURL> to path <xmlURL>
 * @param zipURL// www.ja  v  a2  s.co m
 * @param xmlURL
 */
public static void unStructZip(String zipURL, String xmlURL) throws IOException {
    FileInputStream fis = new FileInputStream(new File(zipURL));
    ZipInputStream zis = new ZipInputStream(fis);
    FileOutputStream fos = new FileOutputStream(xmlURL);
    ZipEntry ze = zis.getNextEntry();
    writeInOutputStream(zis, fos);
    fos.flush();
    fis.close();
    fos.close();
    zis.close();
}

From source file:Main.java

private static File unzip(Context context, InputStream in) throws IOException {
    File tmpDb = null;//from ww w.  ja v a 2  s. co  m
    int dbFiles = 0;
    try {
        tmpDb = File.createTempFile("import", ".db");

        ZipInputStream zin = new ZipInputStream(in);
        ZipEntry sourceEntry;
        while (true) {

            sourceEntry = zin.getNextEntry();

            if (sourceEntry == null) {
                break;
            }

            if (sourceEntry.isDirectory()) {
                zin.closeEntry();
                continue;
            }

            FileOutputStream fOut;
            if (sourceEntry.getName().endsWith(".db")) {
                // Write database to tmp file
                fOut = new FileOutputStream(tmpDb);
                dbFiles++;
            } else {
                // Write all other files(images) to files dir in apps data
                int start = sourceEntry.getName().lastIndexOf("/") + 1;
                String name = sourceEntry.getName().substring(start);
                fOut = context.openFileOutput(name, Context.MODE_PRIVATE);
            }

            final OutputStream targetStream = fOut;
            try {
                int read;
                while (true) {
                    byte[] buffer = new byte[1024];
                    read = zin.read(buffer);
                    if (read == -1) {
                        break;
                    }
                    targetStream.write(buffer, 0, read);
                }
                targetStream.flush();
            } finally {
                safeCloseClosable(targetStream);
            }
            zin.closeEntry();
        }
    } finally {
        safeCloseClosable(in);
    }
    if (dbFiles != 1) {
        throw new IllegalStateException("Input file is not a valid backup");
    }
    return tmpDb;
}

From source file:ZipHandler.java

/**
 * unzipps a zip file placed at <zipURL> to path <xmlURL>
 * @param zipURL//from  w w  w  . j a  va  2s.  c  o m
 * @param xmlURL
 */
public static void unZip(String zipURL, String xmlURL) throws IOException {
    FileOutputStream fosBLOB = new FileOutputStream(new File("c:\\"));
    FileOutputStream fosCLOB = new FileOutputStream(new File("c:\\"));
    FileInputStream fis = new FileInputStream(new File(zipURL));
    ZipInputStream zis = new ZipInputStream(fis);
    FileOutputStream fos = new FileOutputStream(xmlURL);
    ZipEntry ze = zis.getNextEntry();
    ExtractZip(zis, fos, fosBLOB, fosCLOB, ze);
    ze = zis.getNextEntry();
    ExtractZip(zis, fos, fosBLOB, fosCLOB, ze);
    ze = zis.getNextEntry();
    ExtractZip(zis, fos, fosBLOB, fosCLOB, ze);
    fos.flush();
    fis.close();
    fos.close();
    fosCLOB.close();
    fosBLOB.close();
    zis.close();
}

From source file:com.cloudbees.plugins.deployer.CloudbeesDeployWarTest.java

public static void assertOnArchive(InputStream inputStream) throws IOException {
    List<String> fileNames = new ArrayList<String>();
    ZipInputStream zipInputStream = null;
    try {//from   ww w . java 2 s  . co  m
        zipInputStream = new ZipInputStream(inputStream);
        ZipEntry zipEntry = zipInputStream.getNextEntry();
        while (zipEntry != null) {
            fileNames.add(zipEntry.getName());
            zipEntry = zipInputStream.getNextEntry();
        }
    } finally {
        IOUtils.closeQuietly(zipInputStream);
    }
    assertTrue(fileNames.contains("META-INF/maven/org.olamy.puzzle.translate/translate-puzzle-webapp/pom.xml"));
    assertTrue(fileNames.contains("WEB-INF/lib/javax.inject-1.jar"));
}