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

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

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes the underlying OutputStream.

Usage

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

/**
 * Create a new Tar from a root directory
 * /*from  w w w.j a va2s.c  om*/
 * @param directory
 *            the base directory
 * @param filename
 *            the output filename
 * @param absolute
 *            store absolute filepath (from directory) or only filename
 * @return True if OK
 */
public static boolean createTarFromDirectory(String directory, String filename, boolean absolute) {
    File rootDir = new File(directory);
    File saveFile = new File(filename);
    // recursive call
    TarArchiveOutputStream taos;
    try {
        taos = new TarArchiveOutputStream(new FileOutputStream(saveFile));
    } catch (FileNotFoundException e) {
        return false;
    }
    taos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    try {
        recurseFiles(rootDir, rootDir, taos, absolute);
    } catch (IOException e2) {
        try {
            taos.close();
        } catch (IOException e) {
            // ignore
        }
        return false;
    }
    try {
        taos.finish();
    } catch (IOException e1) {
        // ignore
    }
    try {
        taos.flush();
    } catch (IOException e) {
        // ignore
    }
    try {
        taos.close();
    } catch (IOException e) {
        // ignore
    }
    return true;
}

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

/**
 * Create a new Tar from an array of Files (only name of files will be used)
 * /*w  w w .  j  av  a 2s.c  om*/
 * @param files
 *            array of files to add
 * @param filename
 *            the output filename
 * @return True if OK
 */
public static boolean createTarFromFiles(File[] files, String filename) {
    File saveFile = new File(filename);
    // recursive call
    TarArchiveOutputStream taos;
    try {
        taos = new TarArchiveOutputStream(new FileOutputStream(saveFile));
    } catch (FileNotFoundException e) {
        return false;
    }
    taos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    for (File file : files) {
        try {
            addFile(file, taos);
        } catch (IOException e) {
            try {
                taos.close();
            } catch (IOException e1) {
                // ignore
            }
            return false;
        }
    }
    try {
        taos.finish();
    } catch (IOException e1) {
        // ignore
    }
    try {
        taos.flush();
    } catch (IOException e) {
        // ignore
    }
    try {
        taos.close();
    } catch (IOException e) {
        // ignore
    }
    return true;
}

From source file:org.wso2.carbon.connector.util.FileCompressUtil.java

/**
 * Compress the files based on the archive type
 * /*from  www  .  j a  v  a  2 s .  co  m*/
 * 
 * @param files
 * @param file
 * @param archiveType
 * @throws IOException
 */
public void compressFiles(Collection files, File file, ArchiveType archiveType) throws IOException {
    log.info("Compressing " + files.size() + " to " + file.getAbsoluteFile());
    // Create the output stream for the output file
    FileOutputStream fos;
    switch (archiveType) {
    case TAR_GZIP:
        fos = new FileOutputStream(new File(file.getCanonicalPath() + ".tar" + ".gz"));
        // Wrap the output file stream in streams that will tar and gzip
        // everything
        TarArchiveOutputStream taos = new TarArchiveOutputStream(
                new GZIPOutputStream(new BufferedOutputStream(fos)));
        // TAR has an 8 gig file limit by default, this gets around that
        taos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR);
        // to get past the 8 gig limit; TAR originally didn't support
        // long file names, so enable the
        // support for it
        taos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        // Get to putting all the files in the compressed output file
        Iterator iterator = files.iterator();
        while (iterator.hasNext()) {
            File f = (File) iterator.next();
            addFilesToCompression(taos, f, ".", ArchiveType.TAR_GZIP);
            // do something to object here...
        }

        // Close everything up
        taos.close();
        fos.close();
        break;
    case ZIP:
        fos = new FileOutputStream(new File(file.getCanonicalPath() + ".zip"));
        // Wrap the output file stream in streams that will tar and zip
        // everything
        ZipArchiveOutputStream zaos = new ZipArchiveOutputStream(new BufferedOutputStream(fos));
        zaos.setEncoding("UTF-8");
        zaos.setCreateUnicodeExtraFields(ZipArchiveOutputStream.UnicodeExtraFieldPolicy.ALWAYS);

        // Get to putting all the files in the compressed output file
        Iterator iterator1 = files.iterator();
        while (iterator1.hasNext()) {
            File f = (File) iterator1.next();
            addFilesToCompression(zaos, f, ".", ArchiveType.ZIP);
            // do something to object here...
        }

        // Close everything up
        zaos.close();
        fos.close();
        break;
    }
}

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  w  w .  j  a  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//from   w  ww .j  a  v  a2s  .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  w  w  w  .ja  v a2 s  .  c om*/
    }

    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  ww  w  .j av a  2  s  .  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");
    }
}