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

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

Introduction

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

Prototype

public void putArchiveEntry(ArchiveEntry archiveEntry) throws IOException 

Source Link

Document

Put an entry on the output stream.

Usage

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

/**
 * Creates tar.gz entry for file recursively
 * //from  www . ja  va 2  s. c  o m
 * @param archiveStream
 * @param absoluteFile
 * @param string
 * @throws IOException
 */
private static void addFileToArchive(TarArchiveOutputStream archiveStream, File file, String base)
        throws IOException {
    String entryName = base + file.getName();
    TarArchiveEntry tarEntry = new TarArchiveEntry(file, entryName);
    archiveStream.putArchiveEntry(tarEntry);
    if (file.isFile()) {
        FileInputStream fInputStream = null;
        try {
            fInputStream = new FileInputStream(file);
            IOUtils.copy(fInputStream, archiveStream);
            archiveStream.closeArchiveEntry();
        } finally {
            IOUtils.closeQuietly(fInputStream);
        }

    } else {
        archiveStream.closeArchiveEntry();
        File[] children = file.listFiles();
        if (children != null) {
            for (File child : children) {
                addFileToArchive(archiveStream, child, entryName + "/");
            }
        }
    }
}

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  ww .jav a 2s.  co  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

/**
 * Add files to a tar.gz archive recursively
 * @param tOut the tar archive output stream
 * @param path the absolute path to the file/dir to add
 * @param base the base path inside the tar.gz archive
 * @throws IOException //ww  w.ja  va 2 s . c  o m
 */
private void addFilesToTarGz(TarArchiveOutputStream tOut, String path, String base) throws IOException {
    File f = new File(path);
    String entryName = base + f.getName();
    TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);
    tOut.putArchiveEntry(tarEntry);

    if (f.isFile()) {
        IOUtils.copy(new FileInputStream(f), tOut);
        tOut.closeArchiveEntry();
    } else {
        tOut.closeArchiveEntry();
        File[] children = f.listFiles();
        if (children != null) {
            for (File child : children) {
                addFilesToTarGz(tOut, child.getAbsolutePath(), entryName + "/");
            }
        }
    }
}

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   w  w  w  .jav a  2 s . 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");
    }//from   w w w.ja  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");
    }
}

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  ww . j  av  a  2s . c o  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();
}