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

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

Introduction

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

Prototype

public TarArchiveEntry(byte[] headerBuf) 

Source Link

Document

Construct an entry from an archive's header bytes.

Usage

From source file:org.trustedanalytics.servicebroker.gearpump.service.file.FileHelper.java

public static byte[] prepareTarGzFile(byte[] tarFileContent) throws IOException {
    ByteArrayOutputStream byteOutput = null;
    GzipCompressorOutputStream gzOutput = null;
    TarArchiveOutputStream tarOutput = null;
    try {/*from  www.  ja  v a  2 s .c  o  m*/
        byteOutput = new ByteArrayOutputStream();
        gzOutput = new GzipCompressorOutputStream(byteOutput);
        tarOutput = new TarArchiveOutputStream(gzOutput);
        TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(FILE_NAME);
        tarArchiveEntry.setSize(tarFileContent.length);
        addArchiveEntry(tarOutput, tarArchiveEntry, tarFileContent);
    } finally {
        gzOutput.close();
        byteOutput.close();
    }

    return byteOutput.toByteArray();
}

From source file:org.waarp.common.tar.TarUtility.java

/**
 * Recursive traversal to add files//w  ww. j a v  a2s  .co  m
 * 
 * @param root
 * @param file
 * @param taos
 * @param absolute
 * @throws IOException
 */
private static void recurseFiles(File root, File file, TarArchiveOutputStream taos, boolean absolute)
        throws IOException {
    if (file.isDirectory()) {
        // recursive call
        File[] files = file.listFiles();
        for (File file2 : files) {
            recurseFiles(root, file2, taos, absolute);
        }
    } else if ((!file.getName().endsWith(".tar")) && (!file.getName().endsWith(".TAR"))) {
        String filename = null;
        if (absolute) {
            filename = file.getAbsolutePath().substring(root.getAbsolutePath().length());
        } else {
            filename = file.getName();
        }
        TarArchiveEntry tae = new TarArchiveEntry(filename);
        tae.setSize(file.length());
        taos.putArchiveEntry(tae);
        FileInputStream fis = new FileInputStream(file);
        IOUtils.copy(fis, taos);
        taos.closeArchiveEntry();
    }
}

From source file:org.waarp.common.tar.TarUtility.java

/**
 * Recursive traversal to add files/*from w  ww .j  av  a2  s  . co  m*/
 * 
 * @param file
 * @param taos
 * @throws IOException
 */
private static void addFile(File file, TarArchiveOutputStream taos) throws IOException {
    String filename = null;
    filename = file.getName();
    TarArchiveEntry tae = new TarArchiveEntry(filename);
    tae.setSize(file.length());
    taos.putArchiveEntry(tae);
    FileInputStream fis = new FileInputStream(file);
    IOUtils.copy(fis, taos);
    taos.closeArchiveEntry();
}

From source file:org.xenmaster.web.SetupHook.java

protected void writePluginsToTarball(TarArchiveOutputStream tos) throws IOException {
    File f = new File("store/xapi/plugins");
    if (!f.exists() || !f.isDirectory()) {
        throw new IOException("Plugin directory is not present");
    }//from w w w  .  ja v a  2  s. co m

    for (File plugin : f.listFiles()) {
        TarArchiveEntry tae = new TarArchiveEntry(plugin);
        tae.setName(plugin.getName());
        tae.setUserId(0);
        tae.setMode(0755);
        tos.putArchiveEntry(tae);
        IOUtils.copy(new FileInputStream(plugin), tos);
        tos.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
 *///  w  w  w .ja  va2 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: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 .  ja v a2s . co  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");
    }/*ww  w  .j  a v  a 2s  .com*/
    // 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");
    }
}

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

private static void tarFile(File file, String name, TarArchiveOutputStream tar, Consumer<String> cs)
        throws IOException {
    if (cs != null) {
        cs.accept("Packaging " + file.getName());
    }//  w w w .ja  v  a  2 s  .co m
    TarArchiveEntry entry = new TarArchiveEntry(name);
    entry.setSize(file.length());
    tar.putArchiveEntry(entry);
    FileInputStream fis = new FileInputStream(file);
    IOUtils.copy(fis, tar);
    fis.close();
    tar.closeArchiveEntry();
}