Example usage for org.apache.commons.compress.archivers.tar TarArchiveOutputStream TarArchiveOutputStream

List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveOutputStream TarArchiveOutputStream

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.tar TarArchiveOutputStream TarArchiveOutputStream.

Prototype

public TarArchiveOutputStream(OutputStream os) 

Source Link

Document

Constructor for TarInputStream.

Usage

From source file:org.xlcloud.commons.compress.CompressUtils.java

/**
 * creates tar.gz archive for a directory and write this to stream pased as
 * parameter//from   w  w  w .j a  v  a  2  s  .  c o  m
 * 
 * @param rootDir
 * @return
 */
public static OutputStream writeArchiveToStream(File rootDir, OutputStream stream) throws IOException {

    BufferedOutputStream bufferedOutputStream = null;
    TarArchiveOutputStream archiveStream = null;

    try {
        bufferedOutputStream = new BufferedOutputStream(stream);
        archiveStream = new TarArchiveOutputStream(new GZIPOutputStream(bufferedOutputStream));
        addFileToArchive(archiveStream, rootDir.getAbsoluteFile(), "");
        return archiveStream;
    } finally {
        IOUtils.closeQuietly(archiveStream);
        IOUtils.closeQuietly(bufferedOutputStream);
    }

}

From source file:plptool.gui.ProjectDriver.java

/**
 * Save current project state to the file specified by plpfile.
 *
 * @return PLP_OK on successful save, error code otherwise
 *///from   www.  ja v a  2 s .  c o  m
public int save() {

    if (sim_mode) {
        smods = ioreg.createPreset();
        watcher = g_watcher.getEntries();
    }

    // commit changes of currently open source file
    if (g)
        updateAsm(open_asm, g_dev.getEditorText());
    //assemble();

    if (plpfile == null || plpfile.getName().equals("Unsaved Project"))
        return Msg.E("No PLP project file is open. Use Save As.", Constants.PLP_FILE_USE_SAVE_AS, null);

    ArrayList<PLPAsmSource> sourceList;
    String verilogHex = "";
    long[] objCode = null;
    PLPAsmSource temp;
    int i;

    try {

        File outFile = plpfile;

        meta = "PLP-5.0\n";

        if (asm != null && asm.isAssembled()) {
            objCode = asm.getObjectCode();
            if (arch.getID() == ArchRegistry.ISA_PLPMIPS) {
                Msg.D("Creating verilog hex code...", 2, this);
                verilogHex = plptool.mips.Formatter.writeVerilogHex(objCode);
            }
            if (objCode != null && objCode.length > 0)
                meta += "START=" + asm.getAddrTable()[0] + "\n";
            else
                meta += "START=0\n";
            meta += "DIRTY=0\n";
            dirty = false;
        } else {
            meta += "DIRTY=1\n";
            dirty = true;
        }

        meta += "ARCH=" + arch.getID() + "\n";

        meta += "\n";

        sourceList = asms;

        for (i = 0; i < sourceList.size(); i++) {
            temp = (PLPAsmSource) sourceList.get(i);
            meta += temp.getAsmFilePath() + "\n";
        }

        // Create plpfile (a tar archive)
        TarArchiveOutputStream tOut = new TarArchiveOutputStream(new FileOutputStream(outFile));

        Msg.D("Writing plp.metafile...", 2, this);
        TarArchiveEntry entry = new TarArchiveEntry("plp.metafile");
        entry.setSize(meta.length());
        tOut.putArchiveEntry(entry);
        byte[] data = meta.getBytes();
        tOut.write(data);
        tOut.flush();
        tOut.closeArchiveEntry();

        for (i = 0; i < sourceList.size(); i++) {
            PLPAsmSource asmFile = sourceList.get(i);
            Msg.D("Writing " + asmFile.getAsmFilePath() + "...", 2, this);
            entry = new TarArchiveEntry(asmFile.getAsmFilePath());

            // We are not expecting an .asm file with size greater than 4GiB
            // ... I hope...
            byte[] fileStr = asmFile.getAsmString().getBytes();
            entry.setSize(fileStr.length);
            tOut.putArchiveEntry(entry);
            tOut.write(fileStr);
            tOut.flush();
            tOut.closeArchiveEntry();
        }

        // Write simulation configuration
        Msg.D("Writing out simulation configuration...", 2, this);
        entry = new TarArchiveEntry("plp.simconfig");
        String str = "";

        str += "simRunnerDelay::" + Config.simRunnerDelay + "\n";
        str += "simAllowExecutionOfArbitraryMem::" + Config.simAllowExecutionOfArbitraryMem + "\n";
        str += "simBusReturnsZeroForUninitRegs::" + Config.simBusReturnsZeroForUninitRegs + "\n";
        str += "simDumpTraceOnFailedEvaluation::" + Config.simDumpTraceOnFailedEvaluation + "\n";

        if (watcher != null) {
            str += "WATCHER\n";

            for (i = 0; i < watcher.getRowCount(); i++) {
                str += watcher.getValueAt(i, 0) + "::";
                str += watcher.getValueAt(i, 1) + "\n";
            }

            str += "END\n";
        }

        Msg.D("-- saving mods info...", 2, this);

        if (ioreg != null && ioreg.getNumOfModsAttached() > 0)
            smods = ioreg.createPreset();

        if (smods != null && smods.size() > 0) {
            str += "MODS\n";

            for (i = 0; i < smods.size(); i++) {
                str += smods.getType(i) + "::"; //0
                str += "RESERVED_FIELD::"; //1
                str += smods.getAddress(i) + "::"; //2
                str += smods.getSize(i) + "::"; //3

                if (smods.getHasFrame(i)) {
                    str += "frame::"; //4
                    str += smods.getVisible(i) + "::"; //5
                    str += smods.getX(i) + "::"; //6
                    str += smods.getY(i) + "::"; //7
                    str += smods.getW(i) + "::"; //8
                    str += smods.getH(i); //9
                } else {
                    str += "noframe";
                }
                str += "\n";
            }
            str += "END\n";
        }

        str += "ISASPECIFIC\n";
        str += arch.saveArchSpecificSimStates();
        str += "END\n";

        entry.setSize(str.getBytes().length);
        tOut.putArchiveEntry(entry);
        tOut.write(str.getBytes());
        tOut.flush();
        tOut.closeArchiveEntry();

        if (asm != null && asm.isAssembled() && objCode != null) {
            // Write hex image
            Msg.D("Writing out verilog hex code...", 2, this);
            entry = new TarArchiveEntry("plp.hex");
            entry.setSize(verilogHex.length());
            tOut.putArchiveEntry(entry);
            data = new byte[verilogHex.length()];
            for (i = 0; i < verilogHex.length(); i++) {
                data[i] = (byte) verilogHex.charAt(i);
            }
            tOut.write(data);
            tOut.flush();
            tOut.closeArchiveEntry();

            // Write binary image, 4-byte big-endian packs
            Msg.D("Writing out binary image...", 2, this);
            entry = new TarArchiveEntry("plp.image");
            entry.setSize(objCode.length * 4);
            tOut.putArchiveEntry(entry);
            data = new byte[objCode.length * 4];
            for (i = 0; i < objCode.length; i++) {
                data[4 * i] = (byte) (objCode[i] >> 24);
                data[4 * i + 1] = (byte) (objCode[i] >> 16);
                data[4 * i + 2] = (byte) (objCode[i] >> 8);
                data[4 * i + 3] = (byte) (objCode[i]);
            }
            tOut.write(data);
            tOut.flush();
            tOut.closeArchiveEntry();

        } else if (binimage != null) {
            Msg.D("Writing out old (dirty) verilog hex code...", 2, this);
            entry = new TarArchiveEntry("plp.hex");
            entry.setSize(hexstring.length());
            tOut.putArchiveEntry(entry);
            tOut.write(hexstring.getBytes());
            tOut.flush();
            tOut.closeArchiveEntry();

            Msg.D("Writing out old (dirty) binary image...", 2, this);
            entry = new TarArchiveEntry("plp.image");
            entry.setSize(binimage.length);
            tOut.putArchiveEntry(entry);
            tOut.write(binimage);
            tOut.flush();
            tOut.closeArchiveEntry();
        }

        // Hook for project save
        CallbackRegistry.callback(CallbackRegistry.PROJECT_SAVE, tOut);

        Msg.D("Closing tar archive...", 2, this);
        tOut.close();
        Msg.D("Project save completed", 2, this);

        modified = false;
        if (g)
            refreshProjectView(false);
        Msg.I(plpfile.getAbsolutePath() + " written", null);

    } catch (Exception e) {
        Msg.trace(e);
        Msg.E("save: Unable to write to " + plpfile.getAbsolutePath() + ". "
                + "Do you have access to the specified location?", Constants.PLP_FILE_SAVE_ERROR, this);
        return Constants.PLP_FILE_SAVE_ERROR;
    }

    return Constants.PLP_OK;
}

From source file:psef.handler.ProjectGetHandler.java

/**
 * Convert the root into a tarball/* w  ww .ja  v a 2  s .  c om*/
 * @return file pointing to the temporary tar.gz file
 */
private File tarRoot() throws IOException {
    TarArchiveOutputStream tOut = null;
    GzipCompressorOutputStream gzOut = null;
    BufferedOutputStream bOut = null;
    FileOutputStream fOut = null;
    System.out.println("Creating tarball");
    try {
        File parent = root.getParentFile();
        File dst = new File(parent, root.getName() + ".tar.gz");
        if (dst.exists()) {
            dst.delete();
            dst.createNewFile();
        }
        String tarGzPath = dst.getAbsolutePath();
        fOut = new FileOutputStream(new File(tarGzPath));
        bOut = new BufferedOutputStream(fOut);
        gzOut = new GzipCompressorOutputStream(bOut);
        tOut = new TarArchiveOutputStream(gzOut);
        addFilesToTarGz(tOut, root.getAbsolutePath(), "");
        return dst;
    } finally {
        if (tOut != null) {
            tOut.finish();
            tOut.close();
        }
        if (gzOut != null)
            gzOut.close();
        if (bOut != null)
            bOut.close();
        if (fOut != null)
            fOut.close();
    }
}

From source file:uk.ac.ebi.intact.dataexchange.psimi.exporter.archive.Compressor.java

private void tar(File outputFile, List<File> filesToCompress) throws IOException {
    OutputStream os = new FileOutputStream(outputFile);
    TarArchiveOutputStream tarOutput = new TarArchiveOutputStream(os);

    for (File fileToCompress : filesToCompress) {
        TarArchiveEntry entry = new TarArchiveEntry(fileToCompress.getName());
        entry.setSize(fileToCompress.length());
        tarOutput.putArchiveEntry(entry);
        IOUtils.copy(new FileInputStream(fileToCompress), tarOutput);
        tarOutput.closeArchiveEntry();/*from ww w .j a  v  a 2s.c o  m*/
    }

    tarOutput.finish();
    tarOutput.close();
    os.close();
}

From source file:utybo.branchingstorytree.swing.utils.BSTPackager.java

public static void toPackage(File bstFile, OutputStream out, HashMap<String, String> meta, Consumer<String> cs)
        throws IOException {
    TarArchiveOutputStream tar = new TarArchiveOutputStream(new GZIPOutputStream(out));

    if (cs != null) {
        cs.accept("Packaging the main BST file");
    }//from w  ww  .  j  a  v a 2s . c  o  m
    // Write the main BST file
    tarFile(bstFile, tar);

    // Write the resources folder
    if (cs != null) {
        cs.accept("Packaging resources");
    }
    tarFolder(new File(bstFile.getParentFile(), "resources"), "resources", tar, cs);

    // Write the meta file
    if (cs != null) {
        cs.accept("Writing meta information");
    }
    meta.put("mainFile", bstFile.getName());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    OutputStreamWriter osw = new OutputStreamWriter(baos, "UTF-8");
    new Gson().toJson(meta, osw);
    osw.flush();
    osw.close();
    TarArchiveEntry tae = new TarArchiveEntry("bstmeta.json");
    tae.setSize(baos.size());
    InputStream bais = baos.toInputStream();
    tar.putArchiveEntry(tae);
    IOUtils.copy(bais, tar);
    tar.closeArchiveEntry();
    tar.close();

    if (cs != null) {
        cs.accept("Done");
    }
}