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

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

Introduction

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

Prototype

public void setNames(String userName, String groupName) 

Source Link

Document

Convenience method to set this entry's group and user names.

Usage

From source file:cpcc.vvrte.services.VirtualVehicleMigratorTest.java

private static void appendEntryToStream(String entryName, byte[] content, ArchiveOutputStream os)
        throws IOException {
    TarArchiveEntry entry = new TarArchiveEntry(entryName);
    entry.setModTime(new Date());
    entry.setSize(content.length);//ww  w  . ja  va  2s .c  o m
    entry.setIds(0, 0);
    entry.setNames("vvrte", "cpcc");

    os.putArchiveEntry(entry);
    os.write(content);
    os.closeArchiveEntry();
}

From source file:com.ibm.util.merge.storage.TarArchive.java

@Override
public String writeFile(String entryName, String content, String userName, String groupName)
        throws IOException, MergeException {
    String chksum = super.writeFile(entryName, content, userName, groupName);
    TarArchiveOutputStream outputStream = (TarArchiveOutputStream) this.getOutputStream();
    TarArchiveEntry entry = new TarArchiveEntry(entryName);
    entry.setSize(content.getBytes().length);
    entry.setNames(userName, groupName);
    outputStream.putArchiveEntry(entry);
    outputStream.write(content.getBytes());
    outputStream.flush();/* w w  w  .j a va  2  s . c om*/
    outputStream.closeArchiveEntry();
    return chksum;
}

From source file:cpcc.vvrte.services.TarArchiveDemo.java

@Test
public void shouldWriteTarFile() throws IOException, ArchiveException {
    byte[] c1 = "content1\n".getBytes("UTF-8");
    byte[] c2 = "content2 text\n".getBytes("UTF-8");

    Date t1 = new Date(1000L * (System.currentTimeMillis() / 1000L));
    Date t2 = new Date(t1.getTime() - 30000);

    FileOutputStream fos = new FileOutputStream("bugger1.tar");

    ArchiveStreamFactory factory = new ArchiveStreamFactory("UTF-8");
    ArchiveOutputStream outStream = factory.createArchiveOutputStream("tar", fos);

    TarArchiveEntry archiveEntry1 = new TarArchiveEntry("entry1");
    archiveEntry1.setModTime(t1);// w  ww. j ava  2s . c o  m
    archiveEntry1.setSize(c1.length);
    archiveEntry1.setIds(STORAGE_ID_ONE, CHUNK_ID_ONE);
    archiveEntry1.setNames(USER_NAME_ONE, GROUP_NAME_ONE);

    outStream.putArchiveEntry(archiveEntry1);
    outStream.write(c1);
    outStream.closeArchiveEntry();

    TarArchiveEntry archiveEntry2 = new TarArchiveEntry("data/entry2");
    archiveEntry2.setModTime(t2);
    archiveEntry2.setSize(c2.length);
    archiveEntry2.setIds(STORAGE_ID_TWO, CHUNK_ID_TWO);
    archiveEntry2.setNames(USER_NAME_TWO, GROUP_NAME_TWO);

    outStream.putArchiveEntry(archiveEntry2);
    outStream.write(c2);
    outStream.closeArchiveEntry();

    outStream.close();

    FileInputStream fis = new FileInputStream("bugger1.tar");
    ArchiveInputStream inStream = factory.createArchiveInputStream("tar", fis);

    TarArchiveEntry entry1 = (TarArchiveEntry) inStream.getNextEntry();
    assertThat(entry1.getModTime()).isEqualTo(t1);
    assertThat(entry1.getSize()).isEqualTo(c1.length);
    assertThat(entry1.getLongUserId()).isEqualTo(STORAGE_ID_ONE);
    assertThat(entry1.getLongGroupId()).isEqualTo(CHUNK_ID_ONE);
    assertThat(entry1.getUserName()).isEqualTo(USER_NAME_ONE);
    assertThat(entry1.getGroupName()).isEqualTo(GROUP_NAME_ONE);
    ByteArrayOutputStream b1 = new ByteArrayOutputStream();
    IOUtils.copy(inStream, b1);
    b1.close();
    assertThat(b1.toByteArray().length).isEqualTo(c1.length);
    assertThat(b1.toByteArray()).isEqualTo(c1);

    TarArchiveEntry entry2 = (TarArchiveEntry) inStream.getNextEntry();
    assertThat(entry2.getModTime()).isEqualTo(t2);
    assertThat(entry2.getSize()).isEqualTo(c2.length);
    assertThat(entry2.getLongUserId()).isEqualTo(STORAGE_ID_TWO);
    assertThat(entry2.getLongGroupId()).isEqualTo(CHUNK_ID_TWO);
    assertThat(entry2.getUserName()).isEqualTo(USER_NAME_TWO);
    assertThat(entry2.getGroupName()).isEqualTo(GROUP_NAME_TWO);
    ByteArrayOutputStream b2 = new ByteArrayOutputStream();
    IOUtils.copy(inStream, b2);
    b2.close();
    assertThat(b2.toByteArray().length).isEqualTo(c2.length);
    assertThat(b2.toByteArray()).isEqualTo(c2);

    TarArchiveEntry entry3 = (TarArchiveEntry) inStream.getNextEntry();
    assertThat(entry3).isNull();

    inStream.close();
}

From source file:cpcc.vvrte.services.VirtualVehicleMigratorImpl.java

/**
 * @param virtualVehicle the virtual vehicle.
 * @param os the output stream to write to.
 * @param chunkNumber the chunk number.//www.  ja  va2 s . co  m
 * @throws IOException thrown in case of errors.
 */
private void writeVirtualVehicleSourceCode(VirtualVehicle virtualVehicle, ArchiveOutputStream os,
        int chunkNumber) throws IOException {
    if (virtualVehicle.getCode() == null) {
        return;
    }

    byte[] source = virtualVehicle.getCode().getBytes("UTF-8");

    TarArchiveEntry entry = new TarArchiveEntry(DATA_VV_SOURCE_JS);
    entry.setModTime(new Date());
    entry.setSize(source.length);
    entry.setIds(0, chunkNumber);
    entry.setNames("vvrte", "cpcc");

    os.putArchiveEntry(entry);
    os.write(source);
    os.closeArchiveEntry();
}

From source file:cpcc.vvrte.services.VirtualVehicleMigratorImpl.java

/**
 * @param virtualVehicle the virtual vehicle.
 * @param os the output stream to write to.
 * @param chunkNumber the chunk number./*w w  w .  jav  a2  s.  c  om*/
 * @throws IOException thrown in case of errors.
 */
private void writeVirtualVehicleProperties(VirtualVehicle virtualVehicle, ArchiveOutputStream os,
        int chunkNumber, boolean lastChunk) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Properties virtualVehicleProps = fillVirtualVehicleProps(virtualVehicle, lastChunk);
    virtualVehicleProps.store(baos, "Virtual Vehicle Properties");
    baos.close();

    byte[] propBytes = baos.toByteArray();

    TarArchiveEntry entry = new TarArchiveEntry(DATA_VV_PROPERTIES);
    entry.setModTime(new Date());
    entry.setSize(propBytes.length);
    entry.setIds(0, chunkNumber);
    entry.setNames("vvrte", "cpcc");

    os.putArchiveEntry(entry);
    os.write(propBytes);
    os.closeArchiveEntry();
}

From source file:cpcc.vvrte.services.VirtualVehicleMigratorImpl.java

/**
 * @param virtualVehicle the virtual vehicle.
 * @param os the output stream to write to.
 * @param chunkNumber the chunk number./*w  ww . jav a 2s. c o m*/
 * @throws IOException thrown in case of errors.
 */
private void writeVirtualVehicleContinuation(VirtualVehicle virtualVehicle, ArchiveOutputStream os,
        int chunkNumber) throws IOException {
    byte[] continuation = virtualVehicle.getContinuation();

    if (continuation == null) {
        return;
    }

    TarArchiveEntry entry = new TarArchiveEntry(DATA_VV_CONTINUATION_JS);
    entry.setModTime(new Date());
    entry.setSize(continuation.length);
    entry.setIds(0, chunkNumber);
    entry.setNames("vvrte", "cpcc");

    os.putArchiveEntry(entry);
    os.write(continuation);
    os.closeArchiveEntry();
}

From source file:cpcc.vvrte.services.VirtualVehicleMigratorImpl.java

/**
 * @param os the output stream to write to.
 * @throws IOException thrown in case of errors.
 *//*from   ww w .  ja v a2  s. co  m*/
private void writeVirtualVehicleStorageChunk(VirtualVehicle virtualVehicle, ArchiveOutputStream os,
        int chunkNumber, List<VirtualVehicleStorage> storageChunk) throws IOException {
    for (VirtualVehicleStorage se : storageChunk) {
        logger.debug("Writing storage entry '" + se.getName() + "'");

        byte[] content = se.getContentAsByteArray();
        TarArchiveEntry entry = new TarArchiveEntry("storage/" + se.getName());
        entry.setModTime(se.getModificationTime());
        entry.setSize(content.length);
        entry.setIds(se.getId(), chunkNumber);
        entry.setNames("vvrte", "cpcc");

        os.putArchiveEntry(entry);
        os.write(content);
        os.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());
        }/*w  w  w . j  a va2s  . c  o  m*/
        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.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);/*  w  ww.  j a v  a2 s  .  c om*/
    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();
}