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.anthemengineering.mojo.infer.InferMojo.java

/**
 * Extracts a given infer.tar.xz file to the given directory.
 *
 * @param tarXzToExtract the file to extract
 * @param inferDownloadDir the directory to extract the file to
 *//*  w  ww  . j  a v a 2 s.co m*/
private static void extract(File tarXzToExtract, File inferDownloadDir) throws IOException {

    FileInputStream fin = null;
    BufferedInputStream in = null;
    XZCompressorInputStream xzIn = null;
    TarArchiveInputStream tarIn = null;

    try {
        fin = new FileInputStream(tarXzToExtract);
        in = new BufferedInputStream(fin);
        xzIn = new XZCompressorInputStream(in);
        tarIn = new TarArchiveInputStream(xzIn);

        TarArchiveEntry entry;
        while ((entry = tarIn.getNextTarEntry()) != null) {
            final File fileToWrite = new File(inferDownloadDir, entry.getName());

            if (entry.isDirectory()) {
                FileUtils.forceMkdir(fileToWrite);
            } else {
                BufferedOutputStream out = null;
                try {
                    out = new BufferedOutputStream(new FileOutputStream(fileToWrite));
                    final byte[] buffer = new byte[4096];
                    int n = 0;
                    while (-1 != (n = tarIn.read(buffer))) {
                        out.write(buffer, 0, n);
                    }
                } finally {
                    if (out != null) {
                        out.close();
                    }
                }
            }

            // assign file permissions
            final int mode = entry.getMode();

            fileToWrite.setReadable((mode & 0004) != 0, false);
            fileToWrite.setReadable((mode & 0400) != 0, true);
            fileToWrite.setWritable((mode & 0002) != 0, false);
            fileToWrite.setWritable((mode & 0200) != 0, true);
            fileToWrite.setExecutable((mode & 0001) != 0, false);
            fileToWrite.setExecutable((mode & 0100) != 0, true);
        }
    } finally {
        if (tarIn != null) {
            tarIn.close();
        }
    }
}

From source file:it.geosolutions.tools.compress.file.Extractor.java

public static void unZip(File inputZipFile, File outputDir) throws IOException, CompressorException {
    if (inputZipFile == null || outputDir == null) {
        throw new CompressorException("Unzip: with null parameters");
    }//from  w  w w .java  2s . co m

    if (!outputDir.exists()) {
        if (!outputDir.mkdirs()) {
            throw new CompressorException("Unzip: Unable to create directory structure: " + outputDir);
        }
    }

    final int BUFFER = Conf.getBufferSize();

    ZipInputStream zipInputStream = null;
    try {
        // Open Zip file for reading
        zipInputStream = new ZipInputStream(new FileInputStream(inputZipFile));
    } catch (FileNotFoundException fnf) {
        throw new CompressorException("Unzip: Unable to find the input zip file named: " + inputZipFile);
    }

    // extract file if not a directory
    BufferedInputStream bis = new BufferedInputStream(zipInputStream);
    // grab a zip file entry
    ZipEntry entry = null;
    while ((entry = zipInputStream.getNextEntry()) != null) {
        // Process each entry

        File currentFile = new File(outputDir, entry.getName());

        FileOutputStream fos = null;
        BufferedOutputStream dest = null;
        try {
            int currentByte;
            // establish buffer for writing file
            byte data[] = new byte[BUFFER];

            // write the current file to disk
            fos = new FileOutputStream(currentFile);
            dest = new BufferedOutputStream(fos, BUFFER);

            // read and write until last byte is encountered
            while ((currentByte = bis.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, currentByte);
            }
        } catch (IOException ioe) {

        } finally {
            try {
                if (dest != null) {
                    dest.flush();
                    dest.close();
                }
                if (fos != null)
                    fos.close();
            } catch (IOException ioe) {
                throw new CompressorException(
                        "Unzip: unable to close the zipInputStream: " + ioe.getLocalizedMessage());
            }
        }
    }
    try {
        if (zipInputStream != null)
            zipInputStream.close();
    } catch (IOException ioe) {
        throw new CompressorException(
                "Unzip: unable to close the zipInputStream: " + ioe.getLocalizedMessage());
    }
    try {
        if (bis != null)
            bis.close();
    } catch (IOException ioe) {
        throw new CompressorException(
                "Unzip: unable to close the Buffered zipInputStream: " + ioe.getLocalizedMessage());
    }
}

From source file:com.sldeditor.test.unit.tool.vector.VectorToolTest.java

/**
 * Extracts a zip entry (file entry).//  w  w w  . jav  a  2  s.  com
 *
 * @param zipIn the zip in
 * @param filePath the file path
 * @throws IOException Signals that an I/O exception has occurred.
 */
private static void extractFile(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();
}

From source file:autohit.common.Utils.java

/**
 *  Copy files.  I can't believe I have to write this.  It will always
 *  overwrite an existing file./*ww w. j a v  a  2 s.  c om*/
 * @param source source file path
 * @param dest destination file path
 * @return string containing errors or messages
 */
public static String copy(String source, String dest) {

    String overwrite = " ";

    BufferedOutputStream bos = null;
    try {

        File destf = new File(dest);
        if (destf.exists()) {
            destf.delete();
            overwrite = " [overwrite] ";
        }

        byte[] buf = new byte[1024];
        int sbuf;

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(source));
        bos = new BufferedOutputStream(new FileOutputStream(dest));

        sbuf = bis.read(buf, 0, 1024);
        while (sbuf > 0) {
            bos.write(buf, 0, sbuf);
            sbuf = bis.read(buf, 0, 1024);
        }
    } catch (Exception e) {
    }
    try {
        bos.close();
    } catch (Exception e) {
        return "Copy failed for=" + source + Constants.CRUDE_SEPERATOR;
    }
    return "Copied file=" + source + overwrite + Constants.CRUDE_SEPERATOR;
}

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

public static Map<String, byte[]> getZipBytesMap(ZipInputStream zipInputStream) {
    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 {/*  w ww .  j av a 2s  .  c  o  m*/
        while ((zipEntry = zipInputStream.getNextEntry()) != null) {
            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:Main.java

public static void unZip(String path) {
    int count = -1;
    int index = -1;

    String savepath = "";

    savepath = path.substring(0, path.lastIndexOf("."));
    try {//from  w w  w  .ja va  2s .  c om
        BufferedOutputStream bos = null;
        ZipEntry entry = null;
        FileInputStream fis = new FileInputStream(path);
        ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));

        while ((entry = zis.getNextEntry()) != null) {
            byte data[] = new byte[buffer];

            String temp = entry.getName();
            index = temp.lastIndexOf("/");
            if (index > -1)
                temp = temp.substring(index + 1);
            String tempDir = savepath + "/zip/";
            File dir = new File(tempDir);
            dir.mkdirs();
            temp = tempDir + temp;
            File f = new File(temp);
            f.createNewFile();

            FileOutputStream fos = new FileOutputStream(f);
            bos = new BufferedOutputStream(fos, buffer);

            while ((count = zis.read(data, 0, buffer)) != -1) {
                bos.write(data, 0, count);
            }

            bos.flush();
            bos.close();
        }

        zis.close();

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

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.
 * //from  w  w  w .  j a va 2  s  .  co  m
 * @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:com.sun.socialsite.util.Utilities.java

public static void copyInputToOutput(InputStream input, OutputStream output) throws IOException {
    BufferedInputStream in = new BufferedInputStream(input);
    BufferedOutputStream out = new BufferedOutputStream(output);
    byte buffer[] = new byte[8192];
    for (int count = 0; count != -1;) {
        count = in.read(buffer, 0, 8192);
        if (count != -1)
            out.write(buffer, 0, count);
    }/*from  w  ww.j  a  va2s  . c om*/

    try {
        in.close();
        out.close();
    } catch (IOException ex) {
        throw new IOException("Closing file streams, " + ex.getMessage());
    }
}

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 2s.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:com.chinamobile.bcbsp.fault.tools.Zip.java

/**
 * Uncompress *.zip files.//from   www.ja va  2 s  .c om
 * @param fileName
 *        : file to be uncompress
 */
@SuppressWarnings("unchecked")
public static void decompress(String fileName) {
    File sourceFile = new File(fileName);
    String filePath = sourceFile.getParent() + "/";
    try {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        ZipFile zipFile = new ZipFile(fileName);
        Enumeration en = zipFile.entries();
        byte[] data = new byte[BUFFER];
        while (en.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) en.nextElement();
            if (entry.isDirectory()) {
                new File(filePath + entry.getName()).mkdirs();
                continue;
            }
            bis = new BufferedInputStream(zipFile.getInputStream(entry));
            File file = new File(filePath + entry.getName());
            File parent = file.getParentFile();
            if (parent != null && (!parent.exists())) {
                parent.mkdirs();
            }
            bos = new BufferedOutputStream(new FileOutputStream(file));
            int count;
            while ((count = bis.read(data, 0, BUFFER)) != -1) {
                bos.write(data, 0, count);
            }
            bis.close();
            bos.close();
        }
        zipFile.close();
    } catch (IOException e) {
        //LOG.error("[compress]", e);
        throw new RuntimeException("[compress]", e);
    }
}