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

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

Introduction

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

Prototype

public void closeArchiveEntry() throws IOException 

Source Link

Document

Close an entry.

Usage

From source file:org.opentestsystem.delivery.testreg.transformer.TarBundler.java

/**
 * Bundles the inputs into a tar which is returned as a byte stream. The input is an array of byte arrays in which
 * the first element is a filename and the second element is the actual file. Subsequent files follow this same
 * pattern. If there is an uneven number of elements then the last file will be ignored.
 * //from ww w  .j  av a2s. c o m
 * @param inputs
 * @return
 */
@Transformer
public Message<File> bundleToTar(final String[] inputs, final @Header("dwBatchUuid") String dwBatchUuid,
        final @Header("fileSuffix") String fileSuffix, final @Header("recordsSent") int recordsSent,
        final @Header("tempPaths") List<Path> tempPaths,
        final @Header("dwConfigType") DwConfigType dwConfigType) {

    String debugPrefix = dwConfigType + " DW Config: ";

    long curTime = System.currentTimeMillis();

    File tmpTarFile;
    FileOutputStream tmpTarOutStream;
    FileInputStream tmpCsvInStream;
    FileInputStream tmpJsonInStream;

    try {
        Path tmpTarPath = Files.createTempFile(DwBatchHandler.DW_TAR_TMP_PREFIX,
                (dwConfigType == DwConfigType.SBAC ? DwBatchHandler.SBAC_DW_NAME : DwBatchHandler.LOCAL_DW_NAME)
                        + fileSuffix);
        tempPaths.add(tmpTarPath);
        tmpTarFile = tmpTarPath.toFile();

        LOGGER.debug(debugPrefix + "Created temp TAR file " + tmpTarFile.getAbsolutePath());

        tmpTarOutStream = new FileOutputStream(tmpTarFile);

        tmpCsvInStream = new FileInputStream(inputs[1]);
        tmpJsonInStream = new FileInputStream(inputs[4]);

        TarArchiveOutputStream tarOutput = new TarArchiveOutputStream(tmpTarOutStream);

        String csvFilename = inputs[0];
        String jsonFilename = inputs[3];

        // tar archive entry for the csv file
        TarArchiveEntry entry = new TarArchiveEntry(csvFilename);
        entry.setSize(Integer.valueOf(inputs[2]));
        tarOutput.putArchiveEntry(entry);

        byte[] buf = new byte[BUFFER_SIZE];
        int len;
        while ((len = tmpCsvInStream.read(buf)) > 0) {
            tarOutput.write(buf, 0, len);
        }

        tarOutput.closeArchiveEntry();

        // tar archive entry for the json file
        entry = new TarArchiveEntry(jsonFilename);
        entry.setSize(Integer.valueOf(inputs[5]));
        tarOutput.putArchiveEntry(entry);

        buf = new byte[BUFFER_SIZE];
        while ((len = tmpJsonInStream.read(buf)) > 0) {
            tarOutput.write(buf, 0, len);
        }

        tarOutput.closeArchiveEntry();

        tarOutput.close();
        tmpCsvInStream.close();
        tmpJsonInStream.close();
    } catch (IOException e) {
        throw new TarBundlerException(debugPrefix + "failure to tar output streams", e);
    }

    LOGGER.debug(debugPrefix + "Created TAR output in " + (System.currentTimeMillis() - curTime));

    return MessageBuilder.withPayload(tmpTarFile).setHeader("dwBatchUuid", dwBatchUuid)
            .setHeader("fileSuffix", fileSuffix).setHeader("recordsSent", recordsSent)
            .setHeader("tempPaths", tempPaths).setHeader("dwConfigType", dwConfigType).build();
}

From source file:org.slc.sli.bulk.extract.files.ExtractFile.java

private static void archiveFile(TarArchiveOutputStream tarArchiveOutputStream, File fileToArchive)
        throws IOException {
    tarArchiveOutputStream//from  w  w  w .ja  v  a2  s. c  o  m
            .putArchiveEntry(tarArchiveOutputStream.createArchiveEntry(fileToArchive, fileToArchive.getName()));
    FileUtils.copyFile(fileToArchive, tarArchiveOutputStream);
    tarArchiveOutputStream.closeArchiveEntry();
    fileToArchive.delete();
}

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 w w  w. ja v  a  2 s  .  co m
        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 .  j a 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:org.vafer.jdeb.DataBuilder.java

/**
 * Build the data archive of the deb from the provided DataProducers
 *
 * @param producers/*from   ww w .  j a v a2  s. c o m*/
 * @param output
 * @param checksums
 * @param compression the compression method used for the data file
 * @return
 * @throws java.security.NoSuchAlgorithmException
 * @throws java.io.IOException
 * @throws org.apache.commons.compress.compressors.CompressorException
 */
BigInteger buildData(Collection<DataProducer> producers, File output, final StringBuilder checksums,
        Compression compression) throws NoSuchAlgorithmException, IOException, CompressorException {

    final File dir = output.getParentFile();
    if (dir != null && (!dir.exists() || !dir.isDirectory())) {
        throw new IOException("Cannot write data file at '" + output.getAbsolutePath() + "'");
    }

    final TarArchiveOutputStream tarOutputStream = new TarArchiveOutputStream(
            compression.toCompressedOutputStream(new FileOutputStream(output)));
    tarOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

    final MessageDigest digest = MessageDigest.getInstance("MD5");

    final Total dataSize = new Total();

    final List<String> addedDirectories = new ArrayList<String>();
    final DataConsumer receiver = new DataConsumer() {
        public void onEachDir(String dirname, String linkname, String user, int uid, String group, int gid,
                int mode, long size) throws IOException {
            dirname = fixPath(dirname);

            createParentDirectories(dirname, user, uid, group, gid);

            // The directory passed in explicitly by the caller also gets the passed-in mode.  (Unlike
            // the parent directories for now.  See related comments at "int mode =" in
            // createParentDirectories, including about a possible bug.)
            createDirectory(dirname, user, uid, group, gid, mode, 0);

            console.info("dir: " + dirname);
        }

        public void onEachFile(InputStream inputStream, String filename, String linkname, String user, int uid,
                String group, int gid, int mode, long size) throws IOException {
            filename = fixPath(filename);

            createParentDirectories(filename, user, uid, group, gid);

            final TarArchiveEntry entry = new TarArchiveEntry(filename, true);

            entry.setUserName(user);
            entry.setUserId(uid);
            entry.setGroupName(group);
            entry.setGroupId(gid);
            entry.setMode(mode);
            entry.setSize(size);

            tarOutputStream.putArchiveEntry(entry);

            dataSize.add(size);
            digest.reset();

            Utils.copy(inputStream, new DigestOutputStream(tarOutputStream, digest));

            final String md5 = Utils.toHex(digest.digest());

            tarOutputStream.closeArchiveEntry();

            console.info("file:" + entry.getName() + " size:" + entry.getSize() + " mode:" + entry.getMode()
                    + " linkname:" + entry.getLinkName() + " username:" + entry.getUserName() + " userid:"
                    + entry.getUserId() + " groupname:" + entry.getGroupName() + " groupid:"
                    + entry.getGroupId() + " modtime:" + entry.getModTime() + " md5: " + md5);

            // append to file md5 list
            checksums.append(md5).append(" ").append(entry.getName()).append('\n');
        }

        public void onEachLink(String path, String linkName, boolean symlink, String user, int uid,
                String group, int gid, int mode) throws IOException {
            path = fixPath(path);

            createParentDirectories(path, user, uid, group, gid);

            final TarArchiveEntry entry = new TarArchiveEntry(path,
                    symlink ? TarArchiveEntry.LF_SYMLINK : TarArchiveEntry.LF_LINK);
            entry.setLinkName(linkName);

            entry.setUserName(user);
            entry.setUserId(uid);
            entry.setGroupName(group);
            entry.setGroupId(gid);
            entry.setMode(mode);

            tarOutputStream.putArchiveEntry(entry);
            tarOutputStream.closeArchiveEntry();

            console.info("link:" + entry.getName() + " mode:" + entry.getMode() + " linkname:"
                    + entry.getLinkName() + " username:" + entry.getUserName() + " userid:" + entry.getUserId()
                    + " groupname:" + entry.getGroupName() + " groupid:" + entry.getGroupId());
        }

        private void createDirectory(String directory, String user, int uid, String group, int gid, int mode,
                long size) throws IOException {
            // All dirs should end with "/" when created, or the test DebAndTaskTestCase.testTarFileSet() thinks its a file
            // and so thinks it has the wrong permission.
            // This consistency also helps when checking if a directory already exists in addedDirectories.

            if (!directory.endsWith("/")) {
                directory += "/";
            }

            if (!addedDirectories.contains(directory)) {
                TarArchiveEntry entry = new TarArchiveEntry(directory, true);
                entry.setUserName(user);
                entry.setUserId(uid);
                entry.setGroupName(group);
                entry.setGroupId(gid);
                entry.setMode(mode);
                entry.setSize(size);

                tarOutputStream.putArchiveEntry(entry);
                tarOutputStream.closeArchiveEntry();
                addedDirectories.add(directory); // so addedDirectories consistently have "/" for finding duplicates.
            }
        }

        private void createParentDirectories(String filename, String user, int uid, String group, int gid)
                throws IOException {
            String dirname = fixPath(new File(filename).getParent());

            // Debian packages must have parent directories created
            // before sub-directories or files can be installed.
            // For example, if an entry of ./usr/lib/foo/bar existed
            // in a .deb package, but the ./usr/lib/foo directory didn't
            // exist, the package installation would fail.  The .deb must
            // then have an entry for ./usr/lib/foo and then ./usr/lib/foo/bar

            if (dirname == null) {
                return;
            }

            // The loop below will create entries for all parent directories
            // to ensure that .deb packages will install correctly.
            String[] pathParts = dirname.split("/");
            String parentDir = "./";
            for (int i = 1; i < pathParts.length; i++) {
                parentDir += pathParts[i] + "/";
                // Make it so the dirs can be traversed by users.
                // We could instead try something more granular, like setting the directory
                // permission to 'rx' for each of the 3 user/group/other read permissions
                // found on the file being added (ie, only if "other" has read
                // permission on the main node, then add o+rx permission on all the containing
                // directories, same w/ user & group), and then also we'd have to
                // check the parentDirs collection of those already added to
                // see if those permissions need to be similarly updated.  (Note, it hasn't
                // been demonstrated, but there might be a bug if a user specifically
                // requests a directory with certain permissions,
                // that has already been auto-created because it was a parent, and if so, go set
                // the user-requested mode on that directory instead of this automatic one.)
                // But for now, keeping it simple by making every dir a+rx.   Examples are:
                // drw-r----- fs/fs   # what you get with setMode(mode)
                // drwxr-xr-x fs/fs   # Usable. Too loose?
                int mode = TarArchiveEntry.DEFAULT_DIR_MODE;

                createDirectory(parentDir, user, uid, group, gid, mode, 0);
            }
        }
    };

    try {
        for (DataProducer data : producers) {
            data.produce(receiver);
        }
    } finally {
        tarOutputStream.close();
    }

    console.info("Total size: " + dataSize);

    return dataSize.count;
}

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

/**
 * Recursive traversal to add files//  w  ww  .  j  a v  a 2  s. 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   ww  w . jav a  2s  .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");
    }/*  www.j  av a 2  s . c o 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:org.xlcloud.commons.compress.CompressUtils.java

/**
 * Creates tar.gz entry for file recursively
 * //from w  w  w  . ja  va 2s . co  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
 *///from w ww . ja  v  a2s. com
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;
}