Example usage for org.apache.commons.io FileUtils openOutputStream

List of usage examples for org.apache.commons.io FileUtils openOutputStream

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils openOutputStream.

Prototype

public static FileOutputStream openOutputStream(File file) throws IOException 

Source Link

Document

Opens a FileOutputStream for the specified file, checking and creating the parent directory if it does not exist.

Usage

From source file:org.dataconservancy.dcs.index.dcpsolr.support.SolrBootstrap.java

/**
 * Install solr if it is not already installed.
 * // ww  w.ja v  a2 s  . co  m
 * @param solrhome
 *        Solr home directory
 * @return String containing the solr home directory.
 * @throws IOException
 */
public static String createIfNecessary(String solrhome) throws IOException {

    File confdir = new File(solrhome, "default" + File.separator + "conf");

    if (confdir.exists()) {
        return solrhome;
    }

    // Cannot list resources so have keep names here
    String[] filenames = new String[] { "elevate.xml", "mapping-ISOLatin1Accent.txt", "protwords.txt",
            "solrconfig.xml", "spellings.txt", "stopwords.txt", "synonyms.txt", "schema.xml", "scripts.conf" };

    for (String name : filenames) {

        OutputStream os = FileUtils.openOutputStream(new File(confdir, name));
        InputStream is = SolrBootstrap.class.getResourceAsStream(CONF_PATH + name);
        IOUtils.copy(is, os);
        is.close();
        os.close();
    }

    OutputStream os = FileUtils.openOutputStream(new File(solrhome, "solr.xml"));
    InputStream is = SolrBootstrap.class.getResourceAsStream(SOLR_PATH + "solr.xml");

    copy(toInputStream(IOUtils.toString(is).replace("${solr.solr.home}", solrhome)), os);
    is.close();
    os.close();

    return solrhome;
}

From source file:org.dataconservancy.dcs.ingest.client.impl.DualManagerDepositTest.java

@BeforeClass
public static void createSampleFile() throws IOException {
    final String fileName = "file.1.0.png";
    baseDir = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());

    File file = new File(baseDir, fileName);
    sampleFilePath = file.getAbsolutePath();
    sampleFilePath = sampleFilePath.replace('\\', '/');

    OutputStream out = FileUtils.openOutputStream(file);
    InputStream in = DualManagerDepositTest.class.getResourceAsStream("/" + fileName);
    IOUtils.copy(in, out);// w ww  .  j ava2  s .com
    out.close();
}

From source file:org.dataconservancy.dcs.ingest.file.impl.FileSystemContentStager.java

public StagedFile add(InputStream stream, Map<String, String> metadata) {

    if (baseDir == null) {
        throw new NullPointerException("Base directory not set!");
    }//  ww  w.j  a  v  a 2 s. c o  m

    FilePathSource path = pathAlgorithm.getPath(stream, metadata);

    String initialPath = null;

    if (path.getPathName() != null) {
        initialPath = path.getPathName();

        File initialFile = new File(baseDir, initialPath);
        if (initialFile.exists() && pathAlgorithm.isContentAddressable()) {
            return new FsStagedFile(path, createSip(path.getPathKey()));
        }
    } else {
        initialPath = getTempPath();
    }

    try {
        File initialFile = new File(baseDir, initialPath);

        FileOutputStream out = FileUtils.openOutputStream(initialFile);
        InputStream source = path.getInputStream();
        IOUtils.copy(source, out);
        out.close();
        source.close();

        if (!path.getPathName().equals(initialPath)) {
            File dest = new File(baseDir, path.getPathName());

            if (dest.exists() && pathAlgorithm.isContentAddressable()) {
                initialFile.delete();
                return new FsStagedFile(path, createSip(path.getPathKey()));
            } else {
                FileUtils.moveFile(initialFile, dest);
            }
        }
    } catch (IOException e) {
        log.error("Error storing file: " + e.getMessage());
        throw new RuntimeException("Could not store content", e);
    }

    return new FsStagedFile(path, createSip(path.getPathKey()));
}

From source file:org.dataconservancy.dcs.ingest.file.impl.FileSystemContentStager.java

private OutputStream openDeletionFile() throws IOException {
    if (deletionFile.exists()) {
        return new FileOutputStream(deletionFile, true);
    } else {//from  w  w  w .j  a v  a2  s.  c  o m
        return FileUtils.openOutputStream(deletionFile);
    }
}

From source file:org.dataconservancy.dcs.ingest.services.ExternalContentStagerTest.java

private static void initFiles() throws IOException {
    baseDir = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());

    File file = new File(baseDir, "test.txt");
    sampleFilePath = file.getAbsolutePath();

    OutputStream out = FileUtils.openOutputStream(file);
    InputStream in = IOUtils.toInputStream("content!");
    IOUtils.copy(in, out);/*  w  w w.  j a  v  a2s  .  c om*/
    out.close();
}

From source file:org.dataconservancy.dcs.integration.main.FileRoundTripIT.java

private static void initFiles() throws IOException {
    final String fileName = "file.1.0.png";
    baseDir = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());

    File file = new File(baseDir, fileName);
    sampleFilePath = file.getAbsolutePath();
    sampleFilePath = sampleFilePath.replace('\\', '/');
    OutputStream out = FileUtils.openOutputStream(file);
    InputStream in = FileRoundTripIT.class.getResourceAsStream("/" + fileName);
    IOUtils.copy(in, out);/*www .j  av  a 2 s .c o m*/
    in.close();
    out.close();
}

From source file:org.dataconservancy.dcs.integration.main.ManualDepositIT.java

private static File createReferenceSipFile(boolean extant) throws IOException {
    Dcp sip = new Dcp();

    DcsDeliverableUnit du = new DcsDeliverableUnit();
    du.setId("example:/du");
    du.setTitle("title");

    DcsManifestation man = new DcsManifestation();
    man.setId("example:/man");
    man.setDeliverableUnit("example:/du");

    DcsManifestationFile dmf = new DcsManifestationFile();
    dmf.setRef(new DcsFileRef("example:/file"));
    man.addManifestationFile(dmf);/*w  w  w  .  j  a v  a2 s .c  o m*/

    DcsFile file = new DcsFile();
    file.setId("example:/file");
    file.setSource("file://" + sampleFilePath);
    file.setName("file.png");
    file.setExtant(extant);

    sip.addDeliverableUnit(du);
    sip.addManifestation(man);
    sip.addFile(file);

    String sipFileName = "reference_" + extant + ".xml";
    File sipFile = new File(baseDir, sipFileName);

    OutputStream out = FileUtils.openOutputStream(sipFile);
    new DcsXstreamStaxModelBuilder().buildSip(sip, out);
    new DcsXstreamStaxModelBuilder().buildSip(sip, new FileOutputStream("/tmp/dcp.xml"));

    out.close();

    log.info(String.format("Created example sip file %s", sipFile.getAbsoluteFile()));

    return sipFile;

}

From source file:org.dataconservancy.dcs.integration.main.ManualDepositIT.java

private static void initFiles() throws IOException {
    final String fileName = "file.1.0.png";
    baseDir = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());

    File file = new File(baseDir, fileName);
    sampleFilePath = file.getAbsolutePath();

    OutputStream out = FileUtils.openOutputStream(file);
    InputStream in = FileRoundTripIT.class.getResourceAsStream("/" + fileName);
    IOUtils.copy(in, out);//from  w  w  w  . ja v a  2 s .  co m
    out.close();
}

From source file:org.datavec.api.util.ArchiveUtils.java

/**
 * Extracts files to the specified destination
 * @param file the file to extract to/* w w  w  . j  a  va 2s .co  m*/
 * @param dest the destination directory
 * @throws java.io.IOException
 */
public static void unzipFileTo(String file, String dest) throws IOException {
    File target = new File(file);
    if (!target.exists())
        throw new IllegalArgumentException("Archive doesnt exist");
    FileInputStream fin = new FileInputStream(target);
    int BUFFER = 2048;
    byte data[] = new byte[BUFFER];

    if (file.endsWith(".zip") || file.endsWith(".jar")) {
        //getFromOrigin the zip file content
        ZipInputStream zis = new ZipInputStream(fin);
        //getFromOrigin the zipped file list entry
        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {
            String fileName = ze.getName();

            File newFile = new File(dest + File.separator + fileName);

            if (ze.isDirectory()) {
                newFile.mkdirs();
                zis.closeEntry();
                ze = zis.getNextEntry();
                continue;
            }

            log.info("file unzip : " + newFile.getAbsoluteFile());

            //create all non exists folders
            //else you will hit FileNotFoundException for compressed folder

            FileOutputStream fos = new FileOutputStream(newFile);

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

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

        zis.close();

    }

    else if (file.endsWith(".tar")) {

        BufferedInputStream in = new BufferedInputStream(fin);
        TarArchiveInputStream tarIn = new TarArchiveInputStream(in);

        TarArchiveEntry entry = null;

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

        while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {

            log.info("Extracting: " + entry.getName());

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

            if (entry.isDirectory()) {

                File f = new File(dest + 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;

                FileOutputStream fos = new FileOutputStream(dest + File.separator + entry.getName());
                BufferedOutputStream destStream = new BufferedOutputStream(fos, BUFFER);
                while ((count = tarIn.read(data, 0, BUFFER)) != -1) {
                    destStream.write(data, 0, count);
                }

                destStream.flush();
                ;

                IOUtils.closeQuietly(destStream);
            }
        }

        /** Close the input stream **/

        tarIn.close();
    }

    else if (file.endsWith(".tar.gz") || file.endsWith(".tgz")) {

        BufferedInputStream in = new BufferedInputStream(fin);
        GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
        TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn);

        TarArchiveEntry entry = null;

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

        while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {

            log.info("Extracting: " + entry.getName());

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

            if (entry.isDirectory()) {

                File f = new File(dest + 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;

                FileOutputStream fos = new FileOutputStream(dest + File.separator + entry.getName());
                BufferedOutputStream destStream = new BufferedOutputStream(fos, BUFFER);
                while ((count = tarIn.read(data, 0, BUFFER)) != -1) {
                    destStream.write(data, 0, count);
                }

                destStream.flush();

                IOUtils.closeQuietly(destStream);
            }
        }

        /** Close the input stream **/

        tarIn.close();
    }

    else if (file.endsWith(".gz")) {
        GZIPInputStream is2 = new GZIPInputStream(fin);
        File extracted = new File(target.getParent(), target.getName().replace(".gz", ""));
        if (extracted.exists())
            extracted.delete();
        extracted.createNewFile();
        OutputStream fos = FileUtils.openOutputStream(extracted);
        IOUtils.copyLarge(is2, fos);
        is2.close();
        fos.flush();
        fos.close();
    }

}

From source file:org.deeplearning4j.util.ArchiveUtils.java

/**
 * Extracts files to the specified destination
 * @param file the file to extract to/*from  www  .  j a  va2  s .c  o m*/
 * @param dest the destination directory
 * @throws IOException
 */
public static void unzipFileTo(String file, String dest) throws IOException {
    File target = new File(file);
    if (!target.exists())
        throw new IllegalArgumentException("Archive doesnt exist");
    FileInputStream fin = new FileInputStream(target);
    int BUFFER = 2048;
    byte data[] = new byte[BUFFER];

    if (file.endsWith(".zip")) {
        //getFromOrigin the zip file content
        ZipInputStream zis = new ZipInputStream(fin);
        //getFromOrigin the zipped file list entry
        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {

            String fileName = ze.getName();
            File newFile = new File(dest + File.separator + fileName);

            log.info("file unzip : " + newFile.getAbsoluteFile());

            //createComplex all non exists folders
            //else you will hit FileNotFoundException for compressed folder
            new File(newFile.getParent()).mkdirs();

            FileOutputStream fos = new FileOutputStream(newFile);

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

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

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

    }

    else if (file.endsWith(".tar.gz") || file.endsWith(".tgz")) {

        BufferedInputStream in = new BufferedInputStream(fin);
        GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
        TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn);

        TarArchiveEntry entry = null;

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

        while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {

            log.info("Extracting: " + entry.getName());

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

            if (entry.isDirectory()) {

                File f = new File(dest + 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;

                FileOutputStream fos = new FileOutputStream(dest + File.separator + entry.getName());
                BufferedOutputStream destStream = new BufferedOutputStream(fos, BUFFER);
                while ((count = tarIn.read(data, 0, BUFFER)) != -1) {
                    destStream.write(data, 0, count);
                }

                destStream.flush();

                IOUtils.closeQuietly(destStream);
            }
        }

        /** Close the input stream **/

        tarIn.close();
    }

    else if (file.endsWith(".gz")) {
        GZIPInputStream is2 = new GZIPInputStream(fin);
        File extracted = new File(target.getParent(), target.getName().replace(".gz", ""));
        if (extracted.exists())
            extracted.delete();
        extracted.createNewFile();
        OutputStream fos = FileUtils.openOutputStream(extracted);
        IOUtils.copyLarge(is2, fos);
        is2.close();
        fos.flush();
        fos.close();
    }

    target.delete();

}