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

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

Introduction

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

Prototype

public TarArchiveEntry(byte[] headerBuf) 

Source Link

Document

Construct an entry from an archive's header bytes.

Usage

From source file:org.fabrician.maven.plugins.CompressUtils.java

/**
 * This attempts to mimic the filtering in the maven-assembly-plugin at a basic level.  It wasn't clear how to use the archivers directly 
 * in that plugin.// w  w  w .j  av  a2 s .  c o  m
 * Supports ${x} only.
 */
public static void copyFilteredDirToArchiveOutputStream(File baseDir, Properties replacements,
        ArchiveOutputStream out) throws IOException {
    File[] files = baseDir.listFiles();
    if (files != null) {
        for (File file : files) {
            if (file.isDirectory()) {
                continue;
            }
            String contents = transform(file, replacements);
            ByteArrayInputStream in = new ByteArrayInputStream(contents.getBytes());
            ArchiveEntry entry = null;
            if (out instanceof TarArchiveOutputStream) {
                entry = new TarArchiveEntry(file.getName());
                ((TarArchiveEntry) entry).setSize(contents.getBytes().length);
                ((TarArchiveEntry) entry).setModTime(file.lastModified());
            } else {
                entry = new ZipArchiveEntry(file.getName());
                ((ZipArchiveEntry) entry).setSize(contents.getBytes().length);
                ((ZipArchiveEntry) entry).setTime(file.lastModified());
            }
            out.putArchiveEntry(entry);
            IOUtils.copy(in, out);
            out.closeArchiveEntry();
        }
    }
}

From source file:org.fabrician.maven.plugins.CompressUtils.java

private static ArchiveEntry createArchiveEntry(ArchiveEntry entry, OutputStream out, String alternateBaseDir)
        throws IOException {
    String substitutedName = substituteAlternateBaseDir(entry, alternateBaseDir);
    if (out instanceof TarArchiveOutputStream) {
        TarArchiveEntry newEntry = new TarArchiveEntry(substitutedName);
        newEntry.setSize(entry.getSize());
        newEntry.setModTime(entry.getLastModifiedDate());

        if (entry instanceof TarArchiveEntry) {
            TarArchiveEntry old = (TarArchiveEntry) entry;
            newEntry.setSize(old.getSize());
            newEntry.setIds(old.getUserId(), old.getGroupId());
            newEntry.setNames(old.getUserName(), old.getGroupName());
        }// ww w .j  a  va  2 s.  c  om
        return newEntry;
    } else if (entry instanceof ZipArchiveEntry) {
        ZipArchiveEntry old = (ZipArchiveEntry) entry;
        ZipArchiveEntry zip = new ZipArchiveEntry(substitutedName);
        zip.setInternalAttributes(old.getInternalAttributes());
        zip.setExternalAttributes(old.getExternalAttributes());
        zip.setExtraFields(old.getExtraFields(true));
        return zip;
    } else {
        return new ZipArchiveEntry(substitutedName);
    }
}

From source file:org.gradle.caching.internal.tasks.CommonsTarPacker.java

@Override
public void pack(List<DataSource> inputs, DataTarget output) throws IOException {
    TarArchiveOutputStream tarOutput = new TarArchiveOutputStream(output.openOutput());
    for (DataSource input : inputs) {
        TarArchiveEntry entry = new TarArchiveEntry(input.getName());
        entry.setSize(input.getLength());
        tarOutput.putArchiveEntry(entry);
        PackerUtils.packEntry(input, tarOutput, buffer);
        tarOutput.closeArchiveEntry();/* ww  w.j a va  2s.  c  om*/
    }
    tarOutput.close();
}

From source file:org.haiku.haikudepotserver.pkg.job.AbstractPkgResourceExportArchiveJobRunner.java

/**
 * <p>Adds a little informational file into the tar-ball.</p>
 *//* w  ww  . j a v a 2s .c  om*/

private void appendArchiveInfo(State state) throws IOException {
    ArchiveInfo archiveInfo = new ArchiveInfo(
            DateTimeHelper.secondAccuracyDatePlusOneSecond(state.latestModifiedTimestamp),
            runtimeInformationService.getProjectVersion());

    byte[] payload = objectMapper.writeValueAsBytes(archiveInfo);
    TarArchiveEntry tarEntry = new TarArchiveEntry(getPathComponentTop() + "/info.json");
    tarEntry.setSize(payload.length);
    tarEntry.setModTime(roundTimeToSecondPlusOne(state.latestModifiedTimestamp));
    state.tarArchiveOutputStream.putArchiveEntry(tarEntry);
    state.tarArchiveOutputStream.write(payload);
    state.tarArchiveOutputStream.closeArchiveEntry();
}

From source file:org.haiku.haikudepotserver.pkg.job.PkgIconExportArchiveJobRunner.java

private void append(State state, String pkgName, Number size, String mediaTypeCode, byte[] payload,
        Date modifyTimestamp) throws IOException {

    String filename = String.join("/", PATH_COMPONENT_TOP, pkgName,
            PkgIcon.deriveFilename(mediaTypeCode, null == size ? null : size.intValue()));

    TarArchiveEntry tarEntry = new TarArchiveEntry(filename);
    tarEntry.setSize(payload.length);//from w ww.  j a  va2s.c o m
    tarEntry.setModTime(roundTimeToSecond(modifyTimestamp));
    state.tarArchiveOutputStream.putArchiveEntry(tarEntry);
    state.tarArchiveOutputStream.write(payload);
    state.tarArchiveOutputStream.closeArchiveEntry();

    if (modifyTimestamp.after(state.latestModifiedTimestamp)) {
        state.latestModifiedTimestamp = modifyTimestamp;
    }
}

From source file:org.haiku.haikudepotserver.pkg.job.PkgScreenshotExportArchiveJobRunner.java

private void append(State state, String pkgName, byte[] payload, Date modifyTimestamp, Integer ordering)
        throws IOException {

    String filename = String.join("/", PATH_COMPONENT_TOP, pkgName, ordering.toString() + ".png");

    TarArchiveEntry tarEntry = new TarArchiveEntry(filename);
    tarEntry.setSize(payload.length);/*  w  ww .j  a v a 2 s. c  om*/
    tarEntry.setModTime(roundTimeToSecond(modifyTimestamp));
    state.tarArchiveOutputStream.putArchiveEntry(tarEntry);
    state.tarArchiveOutputStream.write(payload);
    state.tarArchiveOutputStream.closeArchiveEntry();

    if (modifyTimestamp.after(state.latestModifiedTimestamp)) {
        state.latestModifiedTimestamp = modifyTimestamp;
    }
}

From source file:org.icgc.dcc.download.client.io.ArchiveOutputStream.java

@SneakyThrows
private static void addArchiveEntry(TarArchiveOutputStream os, String filename, long fileSize) {
    TarArchiveEntry entry = new TarArchiveEntry(filename);
    entry.setSize(fileSize);/* ww w.j av  a2s .c  o  m*/
    os.putArchiveEntry(entry);
}

From source file:org.icgc.dcc.portal.manifest.ManifestArchive.java

public void addManifest(@NonNull String fileName, @NonNull ByteArrayOutputStream fileContents)
        throws IOException {
    val tarEntry = new TarArchiveEntry(fileName);

    tarEntry.setSize(fileContents.size());
    tar.putArchiveEntry(tarEntry);/*from w ww. j a  va  2  s . co m*/

    fileContents.writeTo(tar);
    tar.closeArchiveEntry();
}

From source file:org.jbpm.process.workitem.archive.ArchiveWorkItemHandler.java

public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
    String archive = (String) workItem.getParameter("Archive");
    List<File> files = (List<File>) workItem.getParameter("Files");

    try {/* w w w. j  a  va 2  s .  com*/
        OutputStream outputStream = new FileOutputStream(new File(archive));
        ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream("tar", outputStream);

        if (files != null) {
            for (File file : files) {
                final TarArchiveEntry entry = new TarArchiveEntry("testdata/test1.xml");
                entry.setModTime(0);
                entry.setSize(file.length());
                entry.setUserId(0);
                entry.setGroupId(0);
                entry.setMode(0100000);
                os.putArchiveEntry(entry);
                IOUtils.copy(new FileInputStream(file), os);
            }
        }
        os.closeArchiveEntry();
        os.close();
        manager.completeWorkItem(workItem.getId(), null);
    } catch (Throwable t) {
        t.printStackTrace();
        manager.abortWorkItem(workItem.getId());
    }
}

From source file:org.jclouds.docker.compute.features.internal.Archives.java

public static File tar(File baseDir, String archivePath) throws IOException {
    // Check that the directory is a directory, and get its contents
    checkArgument(baseDir.isDirectory(), "%s is not a directory", baseDir);
    File[] files = baseDir.listFiles();
    File tarFile = new File(archivePath);

    String token = getLast(Splitter.on("/").split(archivePath.substring(0, archivePath.lastIndexOf("/"))));

    byte[] buf = new byte[1024];
    int len;/*from  ww w.j ava 2 s.  co m*/
    TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(tarFile));
    tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    for (File file : files) {
        TarArchiveEntry tarEntry = new TarArchiveEntry(file);
        tarEntry.setName("/" + getLast(Splitter.on(token).split(file.toString())));
        tos.putArchiveEntry(tarEntry);
        if (!file.isDirectory()) {
            FileInputStream fin = new FileInputStream(file);
            BufferedInputStream in = new BufferedInputStream(fin);
            while ((len = in.read(buf)) != -1) {
                tos.write(buf, 0, len);
            }
            in.close();
        }
        tos.closeArchiveEntry();
    }
    tos.close();
    return tarFile;
}