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[]) throws IOException 

Source Link

Document

Reads up to b.length bytes of data from this input stream into an array of bytes.

Usage

From source file:org.dataconservancy.packaging.tool.model.impl.PackageStateBuilderImpl.java

@Override
public PackageState deserialize(InputStream stream) {
    ZipInputStream zipInputStream = new ZipInputStream(stream);

    PackageState state = new PackageState();

    ZipEntry entry = null;/*from w w  w.j av a  2 s .  c  o  m*/

    try {

        while ((entry = zipInputStream.getNextEntry()) != null) {
            //Deserialize package-tool-metadata file
            if (entry.getName().equals(PackageStateBuilderImpl.PACKAGE_TOOL_METADATA_FILE)) {
                //copy this entry into a byte array. This is to prevent BagItTxtReader from closing the whole
                // zipInputStream after it's done reading this one entry.
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int n = 0;
                while ((n = zipInputStream.read(buffer)) >= 0)
                    baos.write(buffer, 0, n);
                byte[] content = baos.toByteArray();

                BagItTxtReader reader = new BagItTxtReaderImpl("UTF-8", new ByteArrayInputStream(content));
                NameValueReader.NameValue nameValue;
                state.setCreationToolVersion(new ApplicationVersion());
                while (reader.hasNext()) {
                    nameValue = reader.next();
                    if (nameValue.getName().equals(PACKAGE_NAME)) {
                        state.setPackageName(nameValue.getValue());
                    } else if (nameValue.getName().equals(TOOL_BUILD_NUMBER)) {
                        state.getCreationToolVersion().setBuildNumber(nameValue.getValue());
                    } else if (nameValue.getName().equals(TOOL_BUILD_REVISION)) {
                        state.getCreationToolVersion().setBuildRevision(nameValue.getValue());
                    } else if (nameValue.getName().equals(TOOL_BUILD_TIMESTAMP)) {
                        state.getCreationToolVersion().setBuildTimeStamp(nameValue.getValue());
                    } else {
                        state.addPackageMetadata(nameValue.getName(), nameValue.getValue());
                    }
                }
            } //else if it's something else, parse that stream differently
        }
    } catch (IOException e) {
        throw new PackageToolException(PackagingToolReturnInfo.PKG_IO_EXCEPTION, e);
    }
    return state;
}

From source file:org.geoserver.wps.ppio.ShapeZipPPIO.java

@Override
public Object decode(InputStream input) throws Exception {
    // create the temp directory and register it as a temporary resource
    File tempDir = IOUtils.createTempDirectory("shpziptemp");

    // unzip to the temporary directory
    ZipInputStream zis = null;
    File shapeFile = null;/*from ww w. j  ava 2 s. co m*/
    try {
        zis = new ZipInputStream(input);
        ZipEntry entry = null;

        while ((entry = zis.getNextEntry()) != null) {
            String name = entry.getName();
            File file = new File(tempDir, entry.getName());
            if (entry.isDirectory()) {
                file.mkdir();
            } else {
                if (file.getName().toLowerCase().endsWith(".shp")) {
                    shapeFile = file;
                }

                int count;
                byte data[] = new byte[4096];
                // write the files to the disk
                FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream(file);
                    while ((count = zis.read(data)) != -1) {
                        fos.write(data, 0, count);
                    }
                    fos.flush();
                } finally {
                    if (fos != null) {
                        fos.close();
                    }
                }
            }
            zis.closeEntry();
        }
    } finally {
        if (zis != null) {
            zis.close();
        }
    }

    if (shapeFile == null) {
        FileUtils.deleteDirectory(tempDir);
        throw new IOException("Could not find any file with .shp extension in the zip file");
    } else {
        ShapefileDataStore store = new ShapefileDataStore(DataUtilities.fileToURL(shapeFile));
        resources.addResource(new ShapefileResource(store, tempDir));
        return store.getFeatureSource().getFeatures();
    }

}

From source file:com.taobao.android.tpatch.utils.JarSplitUtils.java

/**
 * jarclass/*from w w w  .  ja v  a 2 s .c  om*/
 * @param inJar
 * @param removeClasses
 */
public static void removeFilesFromJar(File inJar, List<String> removeClasses) throws IOException {
    if (null == removeClasses || removeClasses.isEmpty()) {
        return;
    }
    File outJar = new File(inJar.getParentFile(), inJar.getName() + ".tmp");
    File outParentFolder = outJar.getParentFile();
    if (!outParentFolder.exists()) {
        outParentFolder.mkdirs();
    }
    FileOutputStream fos = new FileOutputStream(outJar);
    ZipOutputStream jos = new ZipOutputStream(fos);
    final byte[] buffer = new byte[8192];
    FileInputStream fis = new FileInputStream(inJar);
    ZipInputStream zis = new ZipInputStream(fis);

    try {
        // loop on the entries of the jar file package and put them in the final jar
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            // do not take directories or anything inside a potential META-INF folder.
            if (entry.isDirectory() || !entry.getName().endsWith(".class")) {
                continue;
            }
            String name = entry.getName();
            String className = getClassName(name);
            if (removeClasses.contains(className)) {
                continue;
            }
            JarEntry newEntry;
            // Preserve the STORED method of the input entry.
            if (entry.getMethod() == JarEntry.STORED) {
                newEntry = new JarEntry(entry);
            } else {
                // Create a new entry so that the compressed len is recomputed.
                newEntry = new JarEntry(name);
            }
            // add the entry to the jar archive
            jos.putNextEntry(newEntry);

            // read the content of the entry from the input stream, and write it into the archive.
            int count;
            while ((count = zis.read(buffer)) != -1) {
                jos.write(buffer, 0, count);
            }
            // close the entries for this file
            jos.closeEntry();
            zis.closeEntry();
        }
    } finally {
        zis.close();
    }
    fis.close();
    jos.close();
    FileUtils.deleteQuietly(inJar);
    FileUtils.moveFile(outJar, inJar);
}

From source file:com.theaetetuslabs.apkmakertester.ApkMakerService.java

private boolean unpackZip(String pathToZip, String destPath) {
    new File(destPath).mkdirs();
    InputStream is;//from   w ww.  ja v  a 2 s . c  o m
    ZipInputStream zis;
    try {
        String filename;
        is = new FileInputStream(pathToZip);
        zis = new ZipInputStream(new BufferedInputStream(is));
        ZipEntry ze;
        byte[] buffer = new byte[1024];
        int count;

        while ((ze = zis.getNextEntry()) != null) {
            // zapis do souboru
            filename = ze.getName();

            // Need to create directories if not exists, or
            // it will generate an Exception...
            if (ze.isDirectory()) {
                File fmd = new File(destPath, filename);
                fmd.mkdirs();
                continue;
            }

            FileOutputStream fout = new FileOutputStream(new File(destPath, filename));

            // cteni zipu a zapis
            while ((count = zis.read(buffer)) != -1) {
                fout.write(buffer, 0, count);
            }

            fout.close();
            zis.closeEntry();
        }

        zis.close();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }

    return true;
}

From source file:org.agnitas.cms.web.CMTemplateAction.java

private byte[] getEntryData(ZipInputStream zipInputStream, ZipEntry entry) throws IOException {
    byte[] fileData = new byte[(int) entry.getSize()];
    byte[] buf = new byte[2048];
    int bytesRead = 0;
    int dataIndex = 0;
    while (bytesRead != -1) {
        bytesRead = zipInputStream.read(buf);
        for (int i = 0; i < bytesRead; i++) {
            if (dataIndex < fileData.length && i < buf.length) {
                fileData[dataIndex] = buf[i];
                dataIndex++;/*from w ww .  j a  v a  2s.  c o  m*/
            }
        }
    }
    return fileData;
}

From source file:org.ado.minesync.commons.ZipArchiver.java

public void unpackZip(FileInputStream inputStream, File outputDir) throws IOException {
    notNull(inputStream, "inputStream cannot be null");
    notNull(outputDir, "outputDir cannot be null");

    ALog.d(TAG, "unzip stream to [" + outputDir.getAbsolutePath() + "]");
    ZipInputStream zipInputStream = null;
    FileOutputStream fileOutputStream = null;
    try {/*from  ww  w .j av  a  2s . c om*/
        String filename;
        zipInputStream = new ZipInputStream(new BufferedInputStream(inputStream));
        ZipEntry zipEntry;
        byte[] buffer = new byte[BUFFER];
        int count;

        while ((zipEntry = zipInputStream.getNextEntry()) != null) {
            filename = zipEntry.getName();
            ALog.v(TAG, "creating file [" + filename + "].");

            if (isZipEntryInDirectory(zipEntry)) {
                File directory = new File(outputDir, getDirectoryName(filename));
                forceMkdir(directory);
            }

            File file = new File(outputDir, filename);
            fileOutputStream = new FileOutputStream(file);
            while ((count = zipInputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, count);
            }
            zipInputStream.closeEntry();

        }

    } finally {
        IOUtils.closeQuietly(fileOutputStream);
        IOUtils.closeQuietly(zipInputStream);
        IOUtils.closeQuietly(inputStream);
    }
}

From source file:org.tinymediamanager.core.movie.tasks.MovieSubtitleDownloadTask.java

@Override
protected void doInBackground() {
    // let the DownloadTask handle the whole download
    super.doInBackground();

    MediaFile mf = new MediaFile(file);

    if (mf.getType() != MediaFileType.SUBTITLE) {
        String basename = FilenameUtils.getBaseName(videoFilePath.toString()) + "." + languageTag;

        // try to decompress
        try {//from   w  w w .ja v  a2  s. c om
            byte[] buffer = new byte[1024];

            ZipInputStream is = new ZipInputStream(new FileInputStream(file.toFile()));

            // get the zipped file list entry
            ZipEntry ze = is.getNextEntry();

            while (ze != null) {
                String zipEntryFilename = ze.getName();
                String extension = FilenameUtils.getExtension(zipEntryFilename);

                // check is that is a valid file type
                if (!Globals.settings.getSubtitleFileType().contains("." + extension)
                        && !"idx".equals(extension)) {
                    ze = is.getNextEntry();
                    continue;
                }

                File destination = new File(file.getParent().toFile(), basename + "." + extension);
                FileOutputStream os = new FileOutputStream(destination);

                int len;
                while ((len = is.read(buffer)) > 0) {
                    os.write(buffer, 0, len);
                }

                os.close();
                mf = new MediaFile(destination.toPath());

                // only take the first subtitle
                break;
            }
            is.closeEntry();
            is.close();

            Utils.deleteFileSafely(file);
        } catch (Exception e) {
        }
    }

    mf.gatherMediaInformation();
    movie.removeFromMediaFiles(mf); // remove old (possibly same) file
    movie.addToMediaFiles(mf); // add file, but maybe with other MI values
    movie.saveToDb();
}

From source file:pt.webdetails.cpk.utils.ZipUtil.java

public void unzip(File zipFile, File destinationFolder) {
    byte[] buffer = new byte[1024];
    setFileInputStream(zipFile);//from w  w w .  j  av  a2 s  .  co m
    ZipInputStream zis = new ZipInputStream(fis);
    try {

        ZipEntry entry = zis.getNextEntry();
        while (entry != null) {
            String filename = entry.getName();
            File newFile = null;

            if (entry.isDirectory()) {
                newFile = new File(
                        destinationFolder.getAbsolutePath() + File.separator + filename + File.separator);
                newFile.mkdirs();
                newFile.mkdir();
            } else {
                newFile = new File(destinationFolder.getAbsolutePath() + File.separator + filename);
                newFile.createNewFile();
                FileOutputStream fos = new FileOutputStream(newFile);
                int len = 0;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }

                fos.close();
            }

            entry = zis.getNextEntry();
        }

        zis.closeEntry();
        zis.close();

    } catch (IOException ex) {
        Logger.getLogger(ZipUtil.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:org.peercast.core.PeerCastService.java

private void assetInstall() throws IOException {
    File dataDir = getFilesDir();

    ZipInputStream zipIs = new ZipInputStream(getAssets().open("peca.zip"));
    try {/*from  ww  w. j  a  va 2 s.co m*/
        ZipEntry ze;
        while ((ze = zipIs.getNextEntry()) != null) {
            File file = new File(dataDir, ze.getName());
            if (file.exists())
                continue;

            if (ze.isDirectory()) {
                file.mkdirs();
                continue;
            }
            file.getParentFile().mkdirs();
            Log.d(TAG, "Install resource -> " + file.getAbsolutePath());
            FileOutputStream fout = new FileOutputStream(file);
            byte[] buffer = new byte[1024];
            int length = 0;
            while ((length = zipIs.read(buffer)) > 0) {
                fout.write(buffer, 0, length);
            }
            zipIs.closeEntry();
            fout.close();
        }
    } finally {
        zipIs.close();
    }
}

From source file:de.static_interface.sinklibrary.Updater.java

/**
 * Part of Zip-File-Extractor, modified by Gravity for use with Bukkit
 */// w ww  . j  av a  2 s  .c  o m
private void unzip(File zipFile) {
    byte[] buffer = new byte[1024];

    try {
        File outputFolder = zipFile.getParentFile();
        if (!outputFolder.exists()) {
            outputFolder.mkdir();
        }

        ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {

            String fileName = ze.getName();
            File newFile = new File(outputFolder + File.separator + fileName);
            if (!newFile.exists() && newFile.getName().toLowerCase().endsWith(".jar")) {
                ze = zis.getNextEntry();
                continue;
            }

            FileOutputStream fos = new FileOutputStream(newFile);

            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }

            fos.close();
            ze = zis.getNextEntry();
        }

        zis.closeEntry();
        zis.close();

        zipFile.delete();

    } catch (IOException ex) {
        SinkLibrary.getInstance().getLogger()
                .warning("The auto-updater tried to unzip a new update file, but was unsuccessful.");
        result = Updater.UpdateResult.FAIL_DOWNLOAD;
        ex.printStackTrace();
    }
}