Example usage for java.io BufferedOutputStream write

List of usage examples for java.io BufferedOutputStream write

Introduction

In this page you can find the example usage for java.io BufferedOutputStream write.

Prototype

@Override
public synchronized void write(byte b[], int off, int len) throws IOException 

Source Link

Document

Writes len bytes from the specified byte array starting at offset off to this buffered output stream.

Usage

From source file:com.glaf.core.util.ZipUtils.java

public static Map<String, byte[]> getZipBytesMap(ZipInputStream zipInputStream, List<String> includes) {
    Map<String, byte[]> zipMap = new java.util.HashMap<String, byte[]>();
    java.util.zip.ZipEntry zipEntry = null;
    ByteArrayOutputStream baos = null;
    BufferedOutputStream bos = null;
    byte tmpByte[] = null;
    try {//from  w ww  .  j  a v  a 2 s  .  com
        while ((zipEntry = zipInputStream.getNextEntry()) != null) {
            String name = zipEntry.getName();
            String ext = FileUtils.getFileExt(name);
            if (includes.contains(ext) || includes.contains(ext.toLowerCase())) {
                tmpByte = new byte[BUFFER];
                baos = new ByteArrayOutputStream();
                bos = new BufferedOutputStream(baos, BUFFER);
                int i = 0;
                while ((i = zipInputStream.read(tmpByte, 0, BUFFER)) != -1) {
                    bos.write(tmpByte, 0, i);
                }
                bos.flush();
                byte[] bytes = baos.toByteArray();
                IOUtils.closeStream(baos);
                IOUtils.closeStream(baos);
                zipMap.put(zipEntry.getName(), bytes);
            }
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        IOUtils.closeStream(baos);
        IOUtils.closeStream(baos);
    }
    return zipMap;
}

From source file:de.rub.syssec.saaf.analysis.steps.extract.ApkUnzipper.java

/**
 * Extracts the given apk into the given destination directory
 * /*from  w w  w.j a  va  2s. c  o m*/
 * @param archive
 *            - the file to extract
 * @param dest
 *            - the destination directory
 * @throws IOException
 */
public static void extractApk(File archive, File dest) throws IOException {

    @SuppressWarnings("resource") // Closing it later results in an error
    ZipFile zipFile = new ZipFile(archive);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    BufferedOutputStream bos = null;
    BufferedInputStream bis = null;

    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();

        String entryFileName = entry.getName();

        byte[] buffer = new byte[16384];
        int len;

        File dir = buildDirectoryHierarchyFor(entryFileName, dest);// destDir
        if (!dir.exists()) {
            dir.mkdirs();
        }

        if (!entry.isDirectory()) {
            if (entry.getSize() == 0) {
                LOGGER.warn("Found ZipEntry \'" + entry.getName() + "\' with size 0 in " + archive.getName()
                        + ". Looks corrupted.");
                continue;
            }

            try {
                bos = new BufferedOutputStream(new FileOutputStream(new File(dest, entryFileName)));// destDir,...

                bis = new BufferedInputStream(zipFile.getInputStream(entry));

                while ((len = bis.read(buffer)) > 0) {
                    bos.write(buffer, 0, len);
                }
                bos.flush();
            } catch (IOException ioe) {
                LOGGER.warn("Failed to extract entry \'" + entry.getName() + "\' from archive. Results for "
                        + archive.getName() + " may not be accurate");
            } finally {
                if (bos != null)
                    bos.close();
                if (bis != null)
                    bis.close();
                // if (zipFile != null) zipFile.close();
            }
        }

    }

}

From source file:com.piaoyou.util.FileUtil.java

public static void copyFile(File org, File des, boolean overwrite) throws IOException {

    if (!org.exists()) {
        throw new FileNotFoundException("Cannot find the source file: " + org.getAbsolutePath());
    }//  w  ww. ja  v a  2 s .  c om
    if (!org.canRead()) {
        throw new IOException("Cannot read the source file: " + org.getAbsolutePath());
    }

    if (overwrite == false) {
        if (des.exists()) {
            return;
        }
    } else {
        if (des.exists()) {
            if (!des.canWrite()) {
                throw new IOException("Cannot write the destination file: " + des.getAbsolutePath());
            }
        } else {
            if (!des.createNewFile()) {
                throw new IOException("Cannot write the destination file: " + des.getAbsolutePath());
            }
        }
    }

    BufferedInputStream inputStream = null;
    BufferedOutputStream outputStream = null;
    byte[] block = new byte[1024];
    try {
        inputStream = new BufferedInputStream(new FileInputStream(org));
        outputStream = new BufferedOutputStream(new FileOutputStream(des));
        while (true) {
            int readLength = inputStream.read(block);
            if (readLength == -1)
                break;// end of file
            outputStream.write(block, 0, readLength);
        }
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException ex) {
                log.error("Cannot close stream", ex);
            }
        }
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException ex) {
                log.error("Cannot close stream", ex);
            }
        }
    }
}

From source file:io.github.blindio.prospero.core.browserdrivers.phantomjs.AbstractUnarchiver.java

protected void extract(ArchiveInputStream arcInStream) throws IOException {
    ArchiveEntry entry = null;// ww w.j  a va 2 s  . c  om

    /** Read the tar entries using the getNextEntry method **/

    while ((entry = (ArchiveEntry) arcInStream.getNextEntry()) != null) {

        System.out.println("Extracting: " + entry.getName());

        /** If the entry is a directory, create the directory. **/

        if (entry.isDirectory()) {

            File f = new File(getDestDirectory() + SystemUtils.FILE_SEPARATOR + entry.getName());
            f.mkdirs();
        }
        /**
         * If the entry is a file,write the decompressed file to the disk
         * and close destination stream.
         **/
        else {
            int count;
            byte data[] = new byte[BUFFER];

            FileOutputStream fos = new FileOutputStream(
                    getDestDirectory() + SystemUtils.FILE_SEPARATOR + entry.getName());
            BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
            while ((count = arcInStream.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, count);
            }
            dest.close();
        }
    }

    /** Close the input stream **/

    arcInStream.close();
    System.out.println("untar completed successfully!!");
}

From source file:com.celements.photo.utilities.Unzip.java

private ByteArrayOutputStream findAndExtractFile(String filename, ZipInputStream zipIn) throws IOException {
    ByteArrayOutputStream out = null;

    for (ZipEntry entry = zipIn.getNextEntry(); zipIn.available() > 0; entry = zipIn.getNextEntry()) {
        if (!entry.isDirectory() && entry.getName().equals(filename)) {
            // read the data and write it to the OutputStream
            int count;
            byte[] data = new byte[BUFFER];
            out = new ByteArrayOutputStream();
            BufferedOutputStream byteOut = new BufferedOutputStream(out, BUFFER);
            while ((count = zipIn.read(data, 0, BUFFER)) != -1) {
                byteOut.write(data, 0, count);
            }/*from w  ww.  j  av a 2 s.  c  om*/
            byteOut.flush();
            break;
        }
    }

    zipIn.close();
    return out;
}

From source file:com.piaoyou.util.FileUtil.java

public static void copyFile(String srcFilename, String destFilename, boolean overwrite) throws IOException {

    File srcFile = new File(srcFilename);
    if (!srcFile.exists()) {
        throw new FileNotFoundException("Cannot find the source file: " + srcFile.getAbsolutePath());
    }/*from   w ww  . j  a va 2 s.c  om*/
    if (!srcFile.canRead()) {
        throw new IOException("Cannot read the source file: " + srcFile.getAbsolutePath());
    }

    File destFile = new File(destFilename);
    if (overwrite == false) {
        if (destFile.exists()) {
            return;
        }
    } else {
        if (destFile.exists()) {
            if (!destFile.canWrite()) {
                throw new IOException("Cannot write the destination file: " + destFile.getAbsolutePath());
            }
        } else {
            if (!destFile.createNewFile()) {
                throw new IOException("Cannot write the destination file: " + destFile.getAbsolutePath());
            }
        }
    }

    BufferedInputStream inputStream = null;
    BufferedOutputStream outputStream = null;
    byte[] block = new byte[1024];
    try {
        inputStream = new BufferedInputStream(new FileInputStream(srcFile));
        outputStream = new BufferedOutputStream(new FileOutputStream(destFile));
        while (true) {
            int readLength = inputStream.read(block);
            if (readLength == -1)
                break;// end of file
            outputStream.write(block, 0, readLength);
        }
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException ex) {
                log.error("Cannot close stream", ex);
            }
        }
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException ex) {
                log.error("Cannot close stream", ex);
            }
        }
    }
}

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

/**
 * @param tmp//from   www . j a  v  a2s .  co  m
 * @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:jeffaschenk.tomcat.instance.generator.builders.TomcatInstanceBuilderHelper.java

/**
 * Extracts a zip entry (file entry)//www.jav  a  2s  . co  m
 *
 * @param zipIn
 * @param filePath
 * @throws IOException
 */
protected static void extractFile(GenerationLogger GENERATION_LOGGER, ZipInputStream zipIn, String filePath)
        throws IOException {
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
    byte[] bytesIn = new byte[4096];
    int read = 0;
    while ((read = zipIn.read(bytesIn)) != -1) {
        bos.write(bytesIn, 0, read);
    }
    bos.close();
    GENERATION_LOGGER.debug("Extracted zip Entry: " + filePath);
}

From source file:com.foreignreader.util.WordNetExtractor.java

@Override
protected Void doInBackground(InputStream... streams) {
    assert streams.length == 1;

    try {/*from   w  w  w.j av a 2 s .  c om*/

        BufferedInputStream in = new BufferedInputStream(streams[0]);
        GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);

        TarArchiveInputStream tar = new TarArchiveInputStream(gzIn);

        TarArchiveEntry tarEntry;

        File dest = LongTranslationHelper.getWordNetDict().getParentFile();

        while ((tarEntry = tar.getNextTarEntry()) != null) {
            File destPath = new File(dest, tarEntry.getName());
            if (tarEntry.isDirectory()) {
                destPath.mkdirs();
            } else {
                destPath.createNewFile();
                byte[] btoRead = new byte[1024 * 10];

                BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath));
                int len = 0;

                while ((len = tar.read(btoRead)) != -1) {
                    bout.write(btoRead, 0, len);
                }

                bout.close();

            }
        }

        tar.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:io.wcm.maven.plugins.nodejs.installation.TarUnArchiver.java

/**
 * Unarchives the arvive into the pBaseDir
 * @param baseDir/*  w  ww.  j a  va 2s  .c  om*/
 * @throws MojoExecutionException
 */
public void unarchive(String baseDir) throws MojoExecutionException {
    try {
        FileInputStream fis = new FileInputStream(archive);

        // TarArchiveInputStream can be constructed with a normal FileInputStream if
        // we ever need to extract regular '.tar' files.
        final TarArchiveInputStream tarIn = new TarArchiveInputStream(new GzipCompressorInputStream(fis));

        TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
        while (tarEntry != null) {
            // Create a file for this tarEntry
            final File destPath = new File(baseDir + File.separator + tarEntry.getName());
            if (tarEntry.isDirectory()) {
                destPath.mkdirs();
            } else {
                destPath.createNewFile();
                destPath.setExecutable(true);
                //byte [] btoRead = new byte[(int)tarEntry.getSize()];
                byte[] btoRead = new byte[8024];
                final BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath));
                int len = 0;

                while ((len = tarIn.read(btoRead)) != -1) {
                    bout.write(btoRead, 0, len);
                }

                bout.close();
            }
            tarEntry = tarIn.getNextTarEntry();
        }
        tarIn.close();
    } catch (IOException e) {
        throw new MojoExecutionException("Could not extract archive: '" + archive.getAbsolutePath() + "'", e);
    }

}