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

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

Introduction

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

Prototype

public void setSize(long size) 

Source Link

Document

Set this entry's file size.

Usage

From source file:org.ngrinder.common.util.CompressionUtils.java

/**
 * Add the given input stream into tar./*from  w  w w  .j a v  a  2  s.  c  om*/
 *
 * @param tarStream   TarArchive outputStream
 * @param inputStream input stream
 * @param path        relative path to append
 * @param size        size of stream
 * @param mode        mode for this entry
 * @throws IOException thrown when having IO problem.
 */
public static void addInputStreamToTar(TarArchiveOutputStream tarStream, InputStream inputStream, String path,
        long size, int mode) throws IOException {
    TarArchiveEntry entry = new TarArchiveEntry(path);
    entry.setSize(size);
    entry.setMode(mode);
    try {
        tarStream.putArchiveEntry(entry);
        IOUtils.copy(inputStream, tarStream);
    } catch (IOException e) {
        throw processException("Error while adding File to Tar file", e);
    } finally {
        tarStream.closeArchiveEntry();
    }
}

From source file:org.ngrinder.common.util.CompressionUtils.java

/**
 * Add a file into tar.//from   w  w  w  . j a v a2s .c o  m
 *
 * @param tarStream TarArchive outputStream
 * @param file      file
 * @param path      relative path to append
 * @param mode      mode for this entry
 * @throws IOException thrown when having IO problem.
 */
public static void addFileToTar(TarArchiveOutputStream tarStream, File file, String path, int mode)
        throws IOException {
    TarArchiveEntry entry = new TarArchiveEntry(path);
    entry.setSize(file.length());
    entry.setMode(mode);
    BufferedInputStream bis = null;
    try {
        tarStream.putArchiveEntry(entry);
        bis = new BufferedInputStream(new FileInputStream(file));
        IOUtils.copy(bis, tarStream);
    } catch (IOException e) {
        throw processException("Error while adding File to Tar file", e);
    } finally {
        IOUtils.closeQuietly(bis);
        tarStream.closeArchiveEntry();
    }
}

From source file:org.openbaton.marketplace.core.VNFPackageManagement.java

public ByteArrayOutputStream compose(String id) throws IOException, ArchiveException {

    VNFPackageMetadata vnfPackageMetadata = vnfPackageMetadataRepository.findFirstById(id);
    String vnfPackageName = vnfPackageMetadata.getName();
    VirtualNetworkFunctionDescriptor vnfd = vnfPackageMetadata.getVnfd();
    VNFPackage vnfPackage = vnfPackageMetadata.getVnfPackage();
    ImageMetadata imageMetadata = vnfPackageMetadata.getImageMetadata();
    NFVImage nfvImage = vnfPackageMetadata.getNfvImage();
    String vnfdJson = mapper.toJson(vnfd);

    HashMap<String, Object> imageConfigJson = new ObjectMapper().readValue(mapper.toJson(nfvImage),
            HashMap.class);
    imageConfigJson.put("minDisk", imageConfigJson.get("minDiskSpace"));
    Object minCPU = imageConfigJson.get("minCPU");
    if (minCPU != null) {
        imageConfigJson.put("minCPU", Integer.parseInt((String) minCPU));
    } else {//from   w  w w.  j a v  a2 s .  com
        imageConfigJson.put("minCPU", 0);
    }
    imageConfigJson.remove("minDiskSpace");
    imageConfigJson.remove("id");
    imageConfigJson.remove("hb_version");

    HashMap<String, String> imageMetadataJson = new ObjectMapper().readValue(mapper.toJson(imageMetadata),
            HashMap.class);
    imageMetadataJson.put("link", imageMetadata.getLink());
    imageMetadataJson.remove("id");
    imageMetadataJson.remove("hb_version");

    ByteArrayOutputStream tar_output = new ByteArrayOutputStream();
    ArchiveOutputStream my_tar_ball = new ArchiveStreamFactory()
            .createArchiveOutputStream(ArchiveStreamFactory.TAR, tar_output);

    //prepare Metadata.yaml
    File tar_input_file = File.createTempFile("Metadata", null);
    Map<String, Object> data = new HashMap<String, Object>();
    data.put("name", vnfPackageName);
    data.put("description", vnfPackageMetadata.getDescription());
    data.put("provider", vnfPackageMetadata.getProvider());
    data.put("requirements", vnfPackageMetadata.getRequirements());
    data.put("shared", vnfPackageMetadata.isShared());
    data.put("image", imageMetadataJson);
    data.put("image-config", imageConfigJson);
    data.put("scripts-link", vnfPackage.getScriptsLink());
    DumperOptions options = new DumperOptions();
    options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
    Yaml yaml = new Yaml(options);
    FileWriter writer = new FileWriter(tar_input_file);
    yaml.dump(data, writer);
    TarArchiveEntry tar_file = new TarArchiveEntry(tar_input_file, "Metadata.yaml");
    tar_file.setSize(tar_input_file.length());
    my_tar_ball.putArchiveEntry(tar_file);
    IOUtils.copy(new FileInputStream(tar_input_file), my_tar_ball);
    /* Close Archieve entry, write trailer information */
    my_tar_ball.closeArchiveEntry();

    //prepare VNFD
    tar_input_file = File.createTempFile("vnfd", null);
    tar_file = new TarArchiveEntry(tar_input_file, "vnfd.json");
    writer = new FileWriter(tar_input_file);
    writer.write(vnfdJson);
    writer.close();
    tar_file.setSize(tar_input_file.length());
    my_tar_ball.putArchiveEntry(tar_file);
    IOUtils.copy(new FileInputStream(tar_input_file), my_tar_ball);
    /* Close Archieve entry, write trailer information */
    my_tar_ball.closeArchiveEntry();

    //scripts
    for (Script script : vnfPackage.getScripts()) {
        tar_input_file = File.createTempFile("script", null);
        tar_file = new TarArchiveEntry(tar_input_file, "scripts/" + script.getName());
        FileOutputStream outputStream = new FileOutputStream(tar_input_file);
        outputStream.write(script.getPayload());
        outputStream.close();
        tar_file.setSize(tar_input_file.length());
        my_tar_ball.putArchiveEntry(tar_file);
        IOUtils.copy(new FileInputStream(tar_input_file), my_tar_ball);
        my_tar_ball.closeArchiveEntry();
    }

    //close tar
    my_tar_ball.finish();
    /* Close output stream, our files are zipped */
    tar_output.close();
    return tar_output;
}

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 w  w w. j  a  v  a 2s.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.savantbuild.io.tar.TarBuilder.java

/**
 * Builds the TAR file using the fileSets and Directories provided.
 *
 * @return The number of entries added to the TAR file including the directories.
 * @throws IOException If the build fails.
 *///from ww  w .j a  v  a  2s .  co m
public int build() throws IOException {
    if (Files.exists(file)) {
        Files.delete(file);
    }

    if (!Files.isDirectory(file.getParent())) {
        Files.createDirectories(file.getParent());
    }

    // Sort the file infos and add the directories
    Set<FileInfo> fileInfos = new TreeSet<>();
    for (FileSet fileSet : fileSets) {
        Set<Directory> dirs = fileSet.toDirectories();
        dirs.removeAll(directories);
        for (Directory dir : dirs) {
            directories.add(dir);
        }

        fileInfos.addAll(fileSet.toFileInfos());
    }

    int count = 0;
    OutputStream os = Files.newOutputStream(file);
    try (TarArchiveOutputStream tos = new TarArchiveOutputStream(compress ? new GZIPOutputStream(os) : os)) {
        tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

        for (Directory directory : directories) {
            String name = directory.name;
            TarArchiveEntry entry = new TarArchiveEntry(name.endsWith("/") ? name : name + "/");
            if (directory.lastModifiedTime != null) {
                entry.setModTime(directory.lastModifiedTime.toMillis());
            }
            if (directory.mode != null) {
                entry.setMode(FileTools.toMode(directory.mode));
            }
            if (storeGroupName && directory.groupName != null) {
                entry.setGroupName(directory.groupName);
            }
            if (storeUserName && directory.userName != null) {
                entry.setUserName(directory.userName);
            }
            tos.putArchiveEntry(entry);
            tos.closeArchiveEntry();
            count++;
        }

        for (FileInfo fileInfo : fileInfos) {
            TarArchiveEntry entry = new TarArchiveEntry(fileInfo.relative.toString());
            entry.setModTime(fileInfo.lastModifiedTime.toMillis());
            if (storeGroupName) {
                entry.setGroupName(fileInfo.groupName);
            }
            if (storeUserName) {
                entry.setUserName(fileInfo.userName);
            }
            entry.setSize(fileInfo.size);
            entry.setMode(fileInfo.toMode());
            tos.putArchiveEntry(entry);
            Files.copy(fileInfo.origin, tos);
            tos.closeArchiveEntry();
            count++;
        }
    }

    return count;
}

From source file:org.seadva.archive.impl.cloud.SdaArchiveStore.java

public void createTar(final File dir, final String tarFileName) {
    try {//w w  w  . j  av a  2  s.  com
        OutputStream tarOutput = new FileOutputStream(new File(tarFileName));
        ArchiveOutputStream tarArchive = new TarArchiveOutputStream(tarOutput);
        List<File> files = new ArrayList<File>();
        File[] filesList = dir.listFiles();
        if (filesList != null) {
            for (File file : filesList) {
                files.addAll(recurseDirectory(file));
            }
        }
        for (File file : files) {
            //                tarArchiveEntry = new TarArchiveEntry(file, file.getPath());
            TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(
                    file.toString().substring(dir.getAbsolutePath().length() + 1, file.toString().length()));
            tarArchiveEntry.setSize(file.length());
            tarArchive.putArchiveEntry(tarArchiveEntry);
            FileInputStream fileInputStream = new FileInputStream(file);
            IOUtils.copy(fileInputStream, tarArchive);
            fileInputStream.close();
            tarArchive.closeArchiveEntry();
        }
        tarArchive.finish();
        tarOutput.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (NullPointerException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

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;/*from   w w w  . j av  a  2s .  c o  m*/
    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 {
        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.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  w  w w .  j  a va2  s . c  om
        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.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);
    entry.setNames("root", "root");

    if (MAINTAINER_SCRIPTS.contains(pName)) {
        entry.setMode(PermMapper.toMode("755"));
    } else {//from  w ww.j av  a2s. com
        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/*w w w . j  a va 2s.  co 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;
}