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:com.pinterest.deployservice.common.TarUtils.java

static void addInputStreamToTar(TarArchiveOutputStream taos, InputStream is, String path, long size, int mode)
        throws Exception {
    TarArchiveEntry entry = new TarArchiveEntry(path);
    entry.setSize(size);
    entry.setMode(mode);/*from w ww . j  a  v  a  2s. c o m*/
    try {
        taos.putArchiveEntry(entry);
        IOUtils.copy(is, taos);
    } finally {
        taos.closeArchiveEntry();
        Closeables.closeQuietly(is);
    }
}

From source file:com.openshift.client.utils.TarFileTestUtils.java

/**
 * Replaces the given file(-name), that might exist anywhere nested in the
 * given archive, by a new entry with the given content. The replacement is
 * faked by adding a new entry into the archive which will overwrite the
 * existing (older one) on extraction.// www. j  a va 2  s  .c  o m
 * 
 * @param name
 *            the name of the file to replace (no path required)
 * @param newContent
 *            the content of the replacement file
 * @param in
 * @return
 * @throws IOException
 * @throws ArchiveException
 * @throws CompressorException
 */
public static File fakeReplaceFile(String name, String newContent, InputStream in) throws IOException {
    Assert.notNull(name);
    Assert.notNull(in);

    File newArchive = FileUtils.createRandomTempFile(".tar.gz");
    newArchive.deleteOnExit();

    TarArchiveOutputStream newArchiveOut = new TarArchiveOutputStream(
            new GZIPOutputStream(new FileOutputStream(newArchive)));
    newArchiveOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

    TarArchiveInputStream archiveIn = new TarArchiveInputStream(new GZIPInputStream(in));
    String pathToReplace = null;
    try {
        // copy the existing entries
        for (ArchiveEntry nextEntry = null; (nextEntry = archiveIn.getNextEntry()) != null;) {
            if (nextEntry.getName().endsWith(name)) {
                pathToReplace = nextEntry.getName();
            }
            newArchiveOut.putArchiveEntry(nextEntry);
            IOUtils.copy(archiveIn, newArchiveOut);
            newArchiveOut.closeArchiveEntry();
        }

        if (pathToReplace == null) {
            throw new IllegalStateException("Could not find file " + name + " in the given archive.");
        }
        TarArchiveEntry newEntry = new TarArchiveEntry(pathToReplace);
        newEntry.setSize(newContent.length());
        newArchiveOut.putArchiveEntry(newEntry);
        IOUtils.copy(new ByteArrayInputStream(newContent.getBytes()), newArchiveOut);
        newArchiveOut.closeArchiveEntry();

        return newArchive;
    } finally {
        newArchiveOut.finish();
        newArchiveOut.flush();
        StreamUtils.close(archiveIn);
        StreamUtils.close(newArchiveOut);
    }
}

From source file:msec.org.TarUtil.java

private static void archiveFile(File file, TarArchiveOutputStream taos, String dir) throws Exception {

    TarArchiveEntry entry = new TarArchiveEntry(dir + file.getName());

    entry.setSize(file.length());

    taos.putArchiveEntry(entry);//from ww  w  .j ava2s  .  c  o m

    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
    int count;
    byte data[] = new byte[BUFFERSZ];
    while ((count = bis.read(data, 0, BUFFERSZ)) != -1) {
        taos.write(data, 0, count);
    }

    bis.close();

    taos.closeArchiveEntry();
}

From source file:com.vmware.admiral.closures.util.ClosureUtils.java

private static void putTarEntry(TarArchiveOutputStream tarOutputStream, TarArchiveEntry tarEntry,
        InputStream inStream, long size) throws IOException {
    tarEntry.setSize(size);
    tarOutputStream.putArchiveEntry(tarEntry);
    try (InputStream input = new BufferedInputStream(inStream)) {
        long byteRead = copy(input, tarOutputStream);
        logInfo("---- BYTES READ %s ", byteRead);
        tarOutputStream.closeArchiveEntry();
    }/* w ww  .java 2s  .  c om*/
}

From source file:jetbrains.exodus.util.CompressBackupUtil.java

/**
 * Adds the file to the tar archive represented by output stream. It's caller's responsibility to close output stream
 * properly./*  w w w  . j av  a  2 s . c  o  m*/
 *
 * @param out           target archive.
 * @param pathInArchive relative path in archive. It will lead the name of the file in the archive.
 * @param source        file to be added.
 * @param fileSize      size of the file (which is known in most cases).
 * @throws IOException in case of any issues with underlying store.
 */
public static void archiveFile(@NotNull final ArchiveOutputStream out, @NotNull final String pathInArchive,
        @NotNull final File source, final long fileSize) throws IOException {
    if (!source.isFile()) {
        throw new IllegalArgumentException("Provided source is not a file: " + source.getAbsolutePath());
    }
    //noinspection ChainOfInstanceofChecks
    if (out instanceof TarArchiveOutputStream) {
        final TarArchiveEntry entry = new TarArchiveEntry(pathInArchive + source.getName());
        entry.setSize(fileSize);
        entry.setModTime(source.lastModified());
        out.putArchiveEntry(entry);
    } else if (out instanceof ZipArchiveOutputStream) {
        final ZipArchiveEntry entry = new ZipArchiveEntry(pathInArchive + source.getName());
        entry.setSize(fileSize);
        entry.setTime(source.lastModified());
        out.putArchiveEntry(entry);
    } else {
        throw new IOException("Unknown archive output stream");
    }
    try (InputStream input = new FileInputStream(source)) {
        IOUtil.copyStreams(input, fileSize, out, IOUtil.BUFFER_ALLOCATOR);
    }
    out.closeArchiveEntry();
}

From source file:gobblin.util.io.StreamUtils.java

/**
 * Helper method for {@link #tar(FileSystem, FileSystem, Path, Path)} that adds a file entry to a given
 * {@link TarArchiveOutputStream} and copies the contents of the file to the new entry.
 *///from w ww  . ja  va2 s. c om
private static void fileToTarArchiveOutputStream(FileStatus fileStatus, FSDataInputStream fsDataInputStream,
        Path destFile, TarArchiveOutputStream tarArchiveOutputStream) throws IOException {
    TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(formatPathToFile(destFile));
    tarArchiveEntry.setSize(fileStatus.getLen());
    tarArchiveEntry.setModTime(System.currentTimeMillis());
    tarArchiveOutputStream.putArchiveEntry(tarArchiveEntry);

    try {
        IOUtils.copy(fsDataInputStream, tarArchiveOutputStream);
    } finally {
        tarArchiveOutputStream.closeArchiveEntry();
    }
}

From source file:com.github.wolfposd.jdpkg.deb.DpkgDeb.java

public static void fileEntryToDestination(File source, ArchiveOutputStream archive, boolean atRoot)
        throws IOException {

    TarArchiveEntry entry;
    if (atRoot) {
        entry = new TarArchiveEntry(source.getName());
    } else {/*from   w w w. j  av  a 2s . co m*/
        entry = new TarArchiveEntry(source.getPath().replace(BuildFile, ""));
    }
    entry.setSize(source.length());
    entry.setMode(TarArchiveEntry.DEFAULT_FILE_MODE);

    archive.putArchiveEntry(entry);

    BufferedInputStream input = new BufferedInputStream(new FileInputStream(source));
    IOUtils.copy(input, archive);

    input.close();
    archive.closeArchiveEntry();
}

From source file:com.facebook.buck.artifact_cache.ArtifactUploader.java

/** Archive and compress 'pathsToIncludeInArchive' into 'out', using tar+zstandard. */
@VisibleForTesting//from w ww  . j a  va 2  s  . c  om
static void compress(ProjectFilesystem projectFilesystem, Collection<Path> pathsToIncludeInArchive, Path out)
        throws IOException {
    try (OutputStream o = new BufferedOutputStream(Files.newOutputStream(out));
            OutputStream z = new ZstdCompressorOutputStream(o);
            TarArchiveOutputStream archive = new TarArchiveOutputStream(z)) {
        archive.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
        for (Path path : pathsToIncludeInArchive) {
            boolean isRegularFile = !projectFilesystem.isDirectory(path);

            // Add a file entry.
            TarArchiveEntry e = new TarArchiveEntry(path.toString() + (isRegularFile ? "" : "/"));
            e.setMode((int) projectFilesystem.getPosixFileMode(path));
            e.setModTime(ZipConstants.getFakeTime());

            if (isRegularFile) {
                e.setSize(projectFilesystem.getFileSize(path));
                archive.putArchiveEntry(e);
                try (InputStream input = projectFilesystem.newFileInputStream(path)) {
                    ByteStreams.copy(input, archive);
                }
            } else {
                archive.putArchiveEntry(e);
            }
            archive.closeArchiveEntry();
        }
        archive.finish();
    }
}

From source file:ezbake.protect.ezca.EzCABootstrap.java

protected static void addTarArchiveEntry(final TarArchiveOutputStream tao, String name, byte[] data) {
    TarArchiveEntry tae = new TarArchiveEntry(name);
    try {//from  w  w w .  j av a2 s  . co  m
        tae.setSize(data.length);
        tao.putArchiveEntry(tae);
        tao.write(data);
        tao.closeArchiveEntry();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:deployer.TestUtils.java

public static ByteBuffer createSampleAppTarBall(ArtifactType type) throws IOException, ArchiveException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream(8096);
    CompressorOutputStream gzs = new GzipCompressorOutputStream(bos);
    ArchiveOutputStream aos = new TarArchiveOutputStream(gzs);
    {// w  ww .  j a va  2 s .  c o m
        TarArchiveEntry nextEntry = new TarArchiveEntry(CONFIG_DIRECTORY + "/app.conf");
        byte[] sampleConf = SAMPLE_CONF_DATA.getBytes();
        nextEntry.setSize(sampleConf.length);
        aos.putArchiveEntry(nextEntry);
        IOUtils.write(sampleConf, aos);
        aos.closeArchiveEntry();
    }
    if (type != ArtifactType.WebApp) {
        TarArchiveEntry nextEntry = new TarArchiveEntry("bin/myApplication.jar");
        byte[] jarData = SAMPLE_JAR_DATA.getBytes();
        nextEntry.setSize(jarData.length);
        aos.putArchiveEntry(nextEntry);
        IOUtils.write(jarData, aos);
        aos.closeArchiveEntry();
    } else {
        TarArchiveEntry nextEntry = new TarArchiveEntry("deployments/ROOT.war");
        byte[] jarData = SAMPLE_JAR_DATA.getBytes();
        nextEntry.setSize(jarData.length);
        aos.putArchiveEntry(nextEntry);
        IOUtils.write(jarData, aos);
        aos.closeArchiveEntry();
    }
    aos.finish();
    gzs.close();
    bos.flush();
    return ByteBuffer.wrap(bos.toByteArray());
}