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

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

Introduction

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

Prototype

public void write(int b) throws IOException 

Source Link

Document

Writes a byte to the current archive entry.

Usage

From source file:org.apache.nifi.cluster.flow.impl.DataFlowDaoImpl.java

private void writeTarEntry(final TarArchiveOutputStream tarOut, final String filename, final byte[] bytes)
        throws IOException {
    final TarArchiveEntry flowEntry = new TarArchiveEntry(filename);
    flowEntry.setSize(bytes.length);/*ww  w .j a  v  a 2 s  .c  o  m*/
    tarOut.putArchiveEntry(flowEntry);
    tarOut.write(bytes);
    tarOut.closeArchiveEntry();
}

From source file:org.apache.nifi.util.FlowFilePackagerV1.java

private void writeAttributesEntry(final Map<String, String> attributes, final TarArchiveOutputStream tout)
        throws IOException {
    final StringBuilder sb = new StringBuilder();
    sb.append(/*from w ww .  j  ava2s.c o m*/
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE properties\n  SYSTEM \"http://java.sun.com/dtd/properties.dtd\">\n");
    sb.append("<properties>");
    for (final Map.Entry<String, String> entry : attributes.entrySet()) {
        final String escapedKey = StringEscapeUtils.escapeXml11(entry.getKey());
        final String escapedValue = StringEscapeUtils.escapeXml11(entry.getValue());
        sb.append("\n  <entry key=\"").append(escapedKey).append("\">").append(escapedValue).append("</entry>");
    }
    sb.append("</properties>");

    final byte[] metaBytes = sb.toString().getBytes(StandardCharsets.UTF_8);
    final TarArchiveEntry attribEntry = new TarArchiveEntry(FILENAME_ATTRIBUTES);
    attribEntry.setMode(tarPermissions);
    attribEntry.setSize(metaBytes.length);
    tout.putArchiveEntry(attribEntry);
    tout.write(metaBytes);
    tout.closeArchiveEntry();
}

From source file:org.apache.tika.server.TarWriter.java

private static void tarStoreBuffer(TarArchiveOutputStream zip, String name, byte[] dataBuffer)
        throws IOException {
    TarArchiveEntry entry = new TarArchiveEntry(name);

    entry.setSize(dataBuffer.length);/* w  w w.jav a  2s . com*/

    zip.putArchiveEntry(entry);

    zip.write(dataBuffer);

    zip.closeArchiveEntry();
}

From source file:org.dcm4chee.storage.tar.TarContainerProvider.java

@Override
public void writeEntriesTo(StorageContext context, List<ContainerEntry> entries, OutputStream out)
        throws IOException {
    TarArchiveOutputStream tar = new TarArchiveOutputStream(out);
    String checksumEntry = container.getChecksumEntry();
    if (checksumEntry != null) {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ContainerEntry.writeChecksumsTo(entries, bout);
        TarArchiveEntry tarEntry = new TarArchiveEntry(checksumEntry);
        tarEntry.setSize(bout.size());//w w  w. ja va2  s  . c o m
        tar.putArchiveEntry(tarEntry);
        tar.write(bout.toByteArray());
        tar.closeArchiveEntry();
    }
    for (ContainerEntry entry : entries) {
        Path path = entry.getSourcePath();
        TarArchiveEntry tarEntry = new TarArchiveEntry(entry.getName());
        tarEntry.setModTime(Files.getLastModifiedTime(path).toMillis());
        tarEntry.setSize(Files.size(path));
        tar.putArchiveEntry(tarEntry);
        Files.copy(path, tar);
        tar.closeArchiveEntry();
    }
    tar.finish();
}

From source file:org.eclipse.che.api.vfs.TarArchiverTest.java

private void addFileEntry(TarArchiveOutputStream tarOut, String name) throws IOException {
    TarArchiveEntry entryA = new TarArchiveEntry(name);
    entryA.setSize(TEST_CONTENT_BYTES.length);
    tarOut.putArchiveEntry(entryA);//from www.ja v a2  s . com
    tarOut.write(TEST_CONTENT_BYTES);
    tarOut.closeArchiveEntry();
}

From source file:org.gradle.caching.internal.packaging.impl.TarBuildCacheEntryPacker.java

private void packMetadata(OriginWriter writeMetadata, TarArchiveOutputStream tarOutput) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    writeMetadata.execute(baos);/*  w w w  .  ja  va 2  s  .c  o m*/
    createTarEntry(METADATA_PATH, baos.size(), UnixPermissions.FILE_FLAG | UnixPermissions.DEFAULT_FILE_PERM,
            tarOutput);
    tarOutput.write(baos.toByteArray());
    tarOutput.closeArchiveEntry();
}

From source file:org.torproject.collector.bridgedescs.TarballBuilder.java

/** Writes the previously configured tarball with all contained files to the
 * given file, or fail if the file extension is not known. */
void build(File directory) throws IOException {
    File tarballFile = new File(directory, this.tarballFileName);
    TarArchiveOutputStream taos = null;
    if (this.tarballFileName.endsWith(".tar.gz")) {
        taos = new TarArchiveOutputStream(
                new GzipCompressorOutputStream(new BufferedOutputStream(new FileOutputStream(tarballFile))));
    } else if (this.tarballFileName.endsWith(".tar.bz2")) {
        taos = new TarArchiveOutputStream(
                new BZip2CompressorOutputStream(new BufferedOutputStream(new FileOutputStream(tarballFile))));
    } else if (this.tarballFileName.endsWith(".tar")) {
        taos = new TarArchiveOutputStream(new BufferedOutputStream(new FileOutputStream(tarballFile)));
    } else {//from ww w .  j  a va2 s  . com
        fail("Unknown file extension: " + this.tarballFileName);
    }
    for (Map.Entry<String, TarballFile> file : this.tarballFiles.entrySet()) {
        TarArchiveEntry tae = new TarArchiveEntry(file.getKey());
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        for (DescriptorBuilder descriptorBuilder : file.getValue().descriptorBuilders) {
            descriptorBuilder.build(baos);
        }
        tae.setSize(baos.size());
        tae.setModTime(file.getValue().modifiedMillis);
        taos.putArchiveEntry(tae);
        taos.write(baos.toByteArray());
        taos.closeArchiveEntry();
    }
    taos.close();
    tarballFile.setLastModified(this.modifiedMillis);
}

From source file:org.vafer.jdeb.ControlBuilder.java

private static void addControlEntry(final String pName, final String pContent,
        final TarArchiveOutputStream pOutput) throws IOException {
    final byte[] data = pContent.getBytes("UTF-8");

    final TarArchiveEntry entry = new TarArchiveEntry("./" + pName, true);
    entry.setSize(data.length);/* ww w. ja va 2  s  .co  m*/
    entry.setNames("root", "root");

    if (MAINTAINER_SCRIPTS.contains(pName)) {
        entry.setMode(PermMapper.toMode("755"));
    } else {
        entry.setMode(PermMapper.toMode("644"));
    }

    pOutput.putArchiveEntry(entry);
    pOutput.write(data);
    pOutput.closeArchiveEntry();
}

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  w  ww . j  a v  a2 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;
}