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(byte[] b, int off, int len) throws IOException 

Source Link

Document

Reads from the current ZIP entry into an array of bytes.

Usage

From source file:org.n52.wps.io.IOUtils.java

public static List<File> unzipAll(File file) throws IOException {
    int bufferLength = 2048;
    byte buffer[] = new byte[bufferLength];
    List<File> foundFiles = new ArrayList<File>();
    ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(new FileInputStream(file)));
    ZipEntry entry;// www .  j  ava 2s.co m
    File tempDir = File.createTempFile("unzipped" + UUID.randomUUID(), "",
            new File(System.getProperty("java.io.tmpdir")));
    tempDir.delete();
    tempDir.mkdir();
    while ((entry = zipInputStream.getNextEntry()) != null) {
        int count;
        File entryFile = new File(tempDir, entry.getName());
        entryFile.createNewFile();
        FileOutputStream fos = new FileOutputStream(entryFile);
        BufferedOutputStream dest = new BufferedOutputStream(fos, bufferLength);
        while ((count = zipInputStream.read(buffer, 0, bufferLength)) != -1) {
            dest.write(buffer, 0, count);
        }
        dest.flush();
        dest.close();

        foundFiles.add(entryFile);

    }

    zipInputStream.close();

    deleteResources(file);

    return foundFiles;
}

From source file:com.sangupta.jerry.util.ZipUtils.java

/**
 * Read a given file from the ZIP file and store it in a temporary file. The
 * temporary file is set to be deleted on exit of application.
 * /*  w w  w. j a v a  2  s .c  om*/
 * @param zipFile
 *            the zip file from which the file needs to be read
 * 
 * @param fileName
 *            the name of the file that needs to be extracted
 * 
 * @return the {@link File} handle for the extracted file in the temp
 *         directory
 * 
 * @throws IllegalArgumentException
 *             if the zipFile is <code>null</code> or the fileName is
 *             <code>null</code> or empty.
 */
public static File readFileFromZip(File zipFile, String fileName) throws FileNotFoundException, IOException {
    if (zipFile == null) {
        throw new IllegalArgumentException("zip file to extract from cannot be null");
    }

    if (AssertUtils.isEmpty(fileName)) {
        throw new IllegalArgumentException("the filename to extract cannot be null/empty");
    }

    LOGGER.debug("Reading {} from {}", fileName, zipFile.getAbsolutePath());

    ZipInputStream stream = null;
    BufferedOutputStream outStream = null;
    File tempFile = null;

    try {
        byte[] buf = new byte[1024];
        stream = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry entry;
        while ((entry = stream.getNextEntry()) != null) {
            String entryName = entry.getName();
            if (entryName.equals(fileName)) {
                tempFile = File.createTempFile(FilenameUtils.getName(entryName),
                        FilenameUtils.getExtension(entryName));
                tempFile.deleteOnExit();

                outStream = new BufferedOutputStream(new FileOutputStream(tempFile));
                int readBytes;
                while ((readBytes = stream.read(buf, 0, 1024)) > -1) {
                    outStream.write(buf, 0, readBytes);
                }

                stream.close();
                outStream.close();

                return tempFile;
            }
        }
    } finally {
        IOUtils.closeQuietly(stream);
        IOUtils.closeQuietly(outStream);
    }

    return tempFile;
}

From source file:hu.sztaki.lpds.pgportal.services.dspace.DSpaceUtil.java

/**
 * Extracts any file from <code>inputFile</code> that begins with
 * 'bitstream' to <code>outputFile</code>.
 * Adapted from examples on/*from  w  w w. j  a  v a  2 s .c  o  m*/
 * http://java.sun.com/developer/technicalArticles/Programming/compression/
 *
 * @param inputFile  File to extract 'bitstream...' from
 * @param outputFile  File to extract 'bitstream...' to
 */
private static void unzipBitstream(InputStream is, File outputFile) throws IOException {
    final int BUFFER = 2048;
    BufferedOutputStream dest = null;
    ZipInputStream zis = new ZipInputStream(is);
    ZipEntry entry;
    try {

        while ((entry = zis.getNextEntry()) != null) {
            if (entry.getName().matches("bitstream.*")) {
                int count;
                byte data[] = new byte[BUFFER];

                // write the files to the disk
                FileOutputStream fos = new FileOutputStream(outputFile.getAbsoluteFile());
                dest = new BufferedOutputStream(fos, BUFFER);
                while ((count = zis.read(data, 0, BUFFER)) != -1) {
                    dest.write(data, 0, count);
                    //System.out.println("Writing: " + outputFile.getAbsoluteFile());
                }
                dest.flush();
                dest.close();
            }
        }
    } finally {
        zis.close();
        try {
            dest.close();
        } catch (Exception e) {
        }
    }
}

From source file:org.semanticsoft.vaaclipsedemo.cassandra.app.CassandraActivator.java

static public void extractFolder(File destPath, InputStream inputStream) throws ZipException, IOException {
    int BUFFER = 2048;
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(inputStream));

    ZipEntry entry;//w ww. j a v a2 s  .c o m
    // Process each entry
    while ((entry = zis.getNextEntry()) != null) {
        // grab a zip file entry
        String currentEntry = entry.getName();
        File destCatalog = new File(destPath, currentEntry);
        // destFile = new File(newPath, destFile.getName());
        File destinationParent = destCatalog.getParentFile();

        // create the parent directory structure if needed
        destinationParent.mkdirs();

        if (!entry.isDirectory()) {
            int count;
            byte data[] = new byte[BUFFER];
            // write the files to the disk
            FileOutputStream fos = new FileOutputStream(destCatalog);
            BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
            while ((count = zis.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, count);
            }
            dest.flush();
            dest.close();
        }
    }
}

From source file:org.lunifera.examples.vaaclipse.demo1.e4.Demo1Activator.java

public static void extractFolder(File destPath, InputStream inputStream) throws ZipException, IOException {
    int BUFFER = 2048;
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(inputStream));

    ZipEntry entry;/*from   ww w . j  a  va  2 s  . co m*/
    // Process each entry
    while ((entry = zis.getNextEntry()) != null) {
        // grab a zip file entry
        String currentEntry = entry.getName();
        File destCatalog = new File(destPath, currentEntry);
        // destFile = new File(newPath, destFile.getName());
        File destinationParent = destCatalog.getParentFile();

        // create the parent directory structure if needed
        destinationParent.mkdirs();

        if (!entry.isDirectory()) {
            int count;
            byte data[] = new byte[BUFFER];
            // write the files to the disk
            FileOutputStream fos = new FileOutputStream(destCatalog);
            BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
            while ((count = zis.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, count);
            }
            dest.flush();
            dest.close();
        }
    }
}

From source file:org.dita2indesign.cmdline.Docx2Xml.java

/**
* @param zipInFile/*from  w ww.j  a v  a  2s.  c  o m*/
*            Zip file to be unzipped
* @param outputDir
*            Directory to which the zipped files will be unpacked.
* @throws Exception 
* @throws ZipException 
*/
public static void unzip(File zipInFile, File outputDir) throws Exception {

    Enumeration<? extends ZipEntry> entries;
    ZipFile zipFile = new ZipFile(zipInFile);
    ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipInFile));
    ZipEntry entry = (ZipEntry) zipInputStream.getNextEntry();

    File curOutDir = outputDir;
    while (entry != null) {

        if (entry.isDirectory()) {
            // This is not robust, just for demonstration purposes.
            curOutDir = new File(curOutDir, entry.getName());
            curOutDir.mkdirs();
            continue;
        }

        File outFile = new File(curOutDir, entry.getName());
        File tempDir = outFile.getParentFile();
        if (!tempDir.exists())
            tempDir.mkdirs();
        outFile.createNewFile();
        BufferedOutputStream outstream = new BufferedOutputStream(new FileOutputStream(outFile));

        int n;
        byte[] buf = new byte[1024];
        while ((n = zipInputStream.read(buf, 0, 1024)) > -1)
            outstream.write(buf, 0, n);
        outstream.flush();
        outstream.close();
        zipInputStream.closeEntry();
        entry = zipInputStream.getNextEntry();
    }
    zipInputStream.close();

    zipFile.close();
}

From source file:org.n52.wps.io.IOUtils.java

public static List<File> unzip(File file, String extension, File directory) throws IOException {
    int bufferLength = 2048;
    byte buffer[] = new byte[bufferLength];
    List<File> foundFiles = new ArrayList<File>();
    ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(new FileInputStream(file)));
    ZipEntry entry;/*from  w  w w. j  a  v a  2s . co  m*/
    File tempDir = directory;
    if (tempDir == null || !directory.isDirectory()) {
        tempDir = File.createTempFile("unzipped" + UUID.randomUUID(), "",
                new File(System.getProperty("java.io.tmpdir")));
        tempDir.delete();
        tempDir.mkdir();
    }
    while ((entry = zipInputStream.getNextEntry()) != null) {
        int count;
        File entryFile = new File(tempDir, entry.getName());
        entryFile.createNewFile();
        FileOutputStream fos = new FileOutputStream(entryFile);
        BufferedOutputStream dest = new BufferedOutputStream(fos, bufferLength);
        while ((count = zipInputStream.read(buffer, 0, bufferLength)) != -1) {
            dest.write(buffer, 0, count);
        }
        dest.flush();
        dest.close();

        if (entry.getName().endsWith("." + extension)) {
            foundFiles.add(entryFile);

        }
    }

    zipInputStream.close();

    deleteResources(file);

    return foundFiles;
}

From source file:net.ftb.util.FileUtils.java

/**
 * Extracts given zip to given location//from   w  w  w  . j  a  va2s . c o  m
 * @param zipLocation - the location of the zip to be extracted
 * @param outputLocation - location to extract to
 */
public static void extractZipTo(String zipLocation, String outputLocation) {
    ZipInputStream zipinputstream = null;
    try {
        byte[] buf = new byte[1024];
        zipinputstream = new ZipInputStream(new FileInputStream(zipLocation));
        ZipEntry zipentry = zipinputstream.getNextEntry();
        while (zipentry != null) {
            String entryName = zipentry.getName();
            int n;
            if (!zipentry.isDirectory() && !entryName.equalsIgnoreCase("minecraft")
                    && !entryName.equalsIgnoreCase(".minecraft") && !entryName.equalsIgnoreCase("instMods")) {
                new File(outputLocation + File.separator + entryName).getParentFile().mkdirs();
                FileOutputStream fileoutputstream = new FileOutputStream(
                        outputLocation + File.separator + entryName);
                while ((n = zipinputstream.read(buf, 0, 1024)) > -1) {
                    fileoutputstream.write(buf, 0, n);
                }
                fileoutputstream.close();
            }
            zipinputstream.closeEntry();
            zipentry = zipinputstream.getNextEntry();
        }
    } catch (Exception e) {
        Logger.logError("Error while extracting zip", e);
        backupExtract(zipLocation, outputLocation);
    } finally {
        try {
            zipinputstream.close();
        } catch (IOException e) {
        }
    }
}

From source file:de.uka.ilkd.key.dl.gui.initialdialog.gui.ToolInstaller.java

/**
 * @param tmp// w  w  w .  j  a va 2  s . c om
 * @param
 * @throws FileNotFoundException
 * @throws IOException
 */
private static void unzip(File file, File dir, PropertySetter ps, JProgressBar bar)
        throws FileNotFoundException, IOException {
    final int BUFFER = 2048;
    BufferedOutputStream dest = null;
    FileInputStream fis = new FileInputStream(file);
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
    ZipEntry entry;
    int value = 0;
    while ((entry = zis.getNextEntry()) != null) {
        bar.setValue(value++);
        int count;
        byte data[] = new byte[BUFFER];
        // write the files to the disk
        String outputFile = dir.getAbsolutePath() + File.separator + entry.getName();
        if (entry.isDirectory()) {
            new File(outputFile).mkdirs();
        } else {
            FileOutputStream fos = new FileOutputStream(outputFile);
            dest = new BufferedOutputStream(fos, BUFFER);
            while ((count = zis.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, count);
            }
            dest.flush();
            dest.close();
            File oFile = new File(outputFile);
            if (OSInfosDefault.INSTANCE.getOs() == OperatingSystem.OSX) {
                // FIXME: we need to make everything executable as somehow
                // the executable bit is not preserved in
                // OSX
                oFile.setExecutable(true);
            }
            if (ps.filterFilename(oFile)) {
                ps.setProperty(outputFile);
            }
        }
    }
    zis.close();
    file.delete();
}

From source file:org.exoplatform.services.cms.impl.Utils.java

/**
 * This function is used to get the version history data which is kept inside the xml files
 * @param versionHistorySourceStream//from   ww w.jav  a  2 s.com
 * @return a map saving version history data with format: [file name, version history data]
 * @throws IOException
 */
private static Map<String, byte[]> getVersionHistoryData(InputStream versionHistorySourceStream)
        throws IOException {
    Map<String, byte[]> mapVersionHistoryData = new HashMap<String, byte[]>();
    ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(versionHistorySourceStream));
    byte[] data = new byte[1024];
    ZipEntry entry = zipInputStream.getNextEntry();
    while (entry != null) {
        //get binary data inside the zip entry
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int available = -1;
        while ((available = zipInputStream.read(data, 0, 1024)) > -1) {
            out.write(data, 0, available);
        }

        //save data into map
        mapVersionHistoryData.put(entry.getName(), out.toByteArray());

        //go to next entry
        out.close();
        zipInputStream.closeEntry();
        entry = zipInputStream.getNextEntry();
    }

    zipInputStream.close();
    return mapVersionHistoryData;
}