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

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

Introduction

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

Prototype

public String getGroupName() 

Source Link

Document

Get this entry's group name.

Usage

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);//from ww  w  .  j av  a 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:org.apache.ant.compress.resources.TarResource.java

protected void setEntry(ArchiveEntry e) {
    super.setEntry(e);
    if (e != null) {
        TarArchiveEntry te = (TarArchiveEntry) e;
        userName = te.getUserName();//from  w  ww. j  a  v a  2 s .c  o m
        groupName = te.getGroupName();
    }
}

From source file:org.apache.hadoop.fs.tar.TarFileSystem.java

@Override
public FileStatus[] listStatus(Path f) throws IOException {
    ArrayList<FileStatus> ret = new ArrayList<FileStatus>();
    Path abs = makeAbsolute(f);//from w  ww.  j a  va  2 s. com
    Path baseTar = getBaseTarPath(abs);
    String inFile = getFileInArchive(abs);
    FileStatus underlying = underlyingFS.getFileStatus(baseTar);

    // if subfile exists in the path, just return the status of that
    if (inFile != null) {
        ret.add(getFileStatus(abs));
    }

    else {
        FSDataInputStream in = underlyingFS.open(baseTar);
        byte[] buffer = new byte[512];

        for (long offset : index.getOffsetList()) {
            in.seek(offset - 512); // adjust for the header
            TarArchiveEntry entry = readHeaderEntry(in, buffer);

            // Construct a FileStatus object 
            FileStatus fstatus = new FileStatus(entry.getSize(), entry.isDirectory(),
                    (int) underlying.getReplication(), underlying.getBlockSize(), entry.getModTime().getTime(),
                    underlying.getAccessTime(), new FsPermission((short) entry.getMode()), entry.getUserName(),
                    entry.getGroupName(),
                    new Path(abs.toUri().toASCIIString() + TAR_INFILESEP + entry.getName()));
            ret.add(fstatus);
        }
    }

    // copy back
    FileStatus[] retArray = new FileStatus[ret.size()];
    ret.toArray(retArray);
    return retArray;
}

From source file:org.apache.hadoop.fs.tar.TarFileSystem.java

@Override
public FileStatus getFileStatus(Path f) throws IOException {
    FileStatus fstatus = null;/*from   w w  w .  j  av  a 2s .  co m*/
    Path abs = makeAbsolute(f);
    Path baseTar = getBaseTarPath(abs);
    String inFile = getFileInArchive(abs);

    FileStatus underlying = underlyingFS.getFileStatus(baseTar);

    if (inFile == null) {
        // return the status of the tar itself but make it a dir
        fstatus = new FileStatus(underlying.getLen(), true, underlying.getReplication(),
                underlying.getBlockSize(), underlying.getModificationTime(), underlying.getAccessTime(),
                underlying.getPermission(), underlying.getOwner(), underlying.getGroup(), abs);
    }

    else {
        long offset = index.getOffset(inFile);

        FSDataInputStream in = underlyingFS.open(baseTar);
        in.seek(offset - 512);
        TarArchiveEntry entry = readHeaderEntry(in);

        if (!entry.getName().equals(inFile)) {
            LOG.fatal("Index file is corrupt." + "Requested filename is present in index "
                    + "but absent in TAR.");
            throw new IOException("NBU-TAR: FATAL: entry file name " + "does not match requested file name");
        }

        // Construct a FileStatus object 
        fstatus = new FileStatus(entry.getSize(), entry.isDirectory(), (int) underlying.getReplication(),
                underlying.getBlockSize(), entry.getModTime().getTime(), underlying.getAccessTime(),
                new FsPermission((short) entry.getMode()), entry.getUserName(), entry.getGroupName(), abs);
    }
    return fstatus;
}

From source file:org.codehaus.mojo.unix.deb.DpkgDebTool.java

private static List<UnixFsObject> process(InputStream is) throws IOException {
    TarArchiveInputStream tarInputStream = new TarArchiveInputStream(is);

    List<UnixFsObject> objects = new ArrayList<UnixFsObject>();

    TarArchiveEntry entry = (TarArchiveEntry) tarInputStream.getNextEntry();

    while (entry != null) {
        Option<UnixFileMode> mode = some(UnixFileMode.fromInt(entry.getMode()));
        FileAttributes attributes = new FileAttributes(some(entry.getUserName()), some(entry.getGroupName()),
                mode);/* ww w. j  av a  2  s.c o m*/
        RelativePath path = relativePath(entry.getName());
        LocalDateTime lastModified = LocalDateTime.fromDateFields(entry.getModTime());

        UnixFsObject object;

        if (entry.isDirectory()) {
            object = directory(path, lastModified, attributes);
        } else if (entry.isSymbolicLink()) {
            object = symlink(path, lastModified, some(entry.getUserName()), some(entry.getGroupName()),
                    entry.getLinkName());
        } else if (entry.isFile()) {
            object = regularFile(path, lastModified, entry.getSize(), attributes);
        } else {
            throw new IOException("Unsupported link type: name=" + entry.getName());
        }

        objects.add(object);

        entry = (TarArchiveEntry) tarInputStream.getNextEntry();
    }

    return objects;
}

From source file:org.eclipse.tycho.plugins.tar.TarGzArchiverTest.java

@Test
public void testCreateArchiveOwnerAndGroupPreserved() throws Exception {
    PosixFileAttributes attrs = getPosixFileAttributes(testOwnerAndGroupFile);
    archiver.createArchive();/*from   ww  w .  jav  a  2s .co m*/
    TarArchiveEntry testOwnerAndGroupNameEntry = getTarEntries().get("dir2/testOwnerAndGroupName");
    assertEquals(attrs.owner().getName(), testOwnerAndGroupNameEntry.getUserName());
    assertEquals(attrs.group().getName(), testOwnerAndGroupNameEntry.getGroupName());
}

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());
        }//from   w w  w  .j a v  a  2s.  co 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.savantbuild.io.tar.TarBuilderTest.java

private static void assertTarFileEquals(Path tarFile, String entry, Path original) throws IOException {
    InputStream is = Files.newInputStream(tarFile);
    if (tarFile.toString().endsWith(".gz")) {
        is = new GZIPInputStream(is);
    }/*  ww w . ja  va  2  s  .c  o  m*/

    try (TarArchiveInputStream tis = new TarArchiveInputStream(is)) {
        TarArchiveEntry tarArchiveEntry = tis.getNextTarEntry();
        while (tarArchiveEntry != null && !tarArchiveEntry.getName().equals(entry)) {
            tarArchiveEntry = tis.getNextTarEntry();
        }

        if (tarArchiveEntry == null) {
            fail("Tar [" + tarFile + "] is missing entry [" + entry + "]");
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int length;
        while ((length = tis.read(buf)) != -1) {
            baos.write(buf, 0, length);
        }

        assertEquals(Files.readAllBytes(original), baos.toByteArray());
        assertEquals(tarArchiveEntry.getSize(), Files.size(original));
        assertEquals(tarArchiveEntry.getUserName(), Files.getOwner(original).getName());
        assertEquals(tarArchiveEntry.getGroupName(),
                Files.readAttributes(original, PosixFileAttributes.class).group().getName());
    }
}

From source file:org.savantbuild.io.tar.TarBuilderTest.java

private static void assertTarContainsDirectory(Path tarFile, String entry, Integer mode, String userName,
        String groupName) throws IOException {
    InputStream is = Files.newInputStream(tarFile);
    if (tarFile.toString().endsWith(".gz")) {
        is = new GZIPInputStream(is);
    }//from   w  ww . j a v a 2 s  .  c om

    try (TarArchiveInputStream tis = new TarArchiveInputStream(is)) {
        TarArchiveEntry tarArchiveEntry = tis.getNextTarEntry();
        while (tarArchiveEntry != null && !tarArchiveEntry.getName().equals(entry)) {
            tarArchiveEntry = tis.getNextTarEntry();
        }

        if (tarArchiveEntry == null) {
            fail("Tar [" + tarFile + "] is missing entry [" + entry + "]");
        }

        assertTrue(tarArchiveEntry.isDirectory());
        if (mode != null) {
            assertEquals(tarArchiveEntry.getMode(), FileTools.toMode(mode));
        }
        if (userName != null) {
            assertEquals(tarArchiveEntry.getUserName(), userName);
        }
        if (groupName != null) {
            assertEquals(tarArchiveEntry.getGroupName(), groupName);
        }
    }
}

From source file:org.savantbuild.io.tar.TarTools.java

/**
 * Untars a TAR file. This also handles tar.gz files by checking the file extension. If the file extension ends in .gz
 * it will read the tarball through a GZIPInputStream.
 *
 * @param file     The TAR file./*  w w w .j ava 2s  .  co  m*/
 * @param to       The directory to untar to.
 * @param useGroup Determines if the group name in the archive is used.
 * @param useOwner Determines if the owner name in the archive is used.
 * @throws IOException If the untar fails.
 */
public static void untar(Path file, Path to, boolean useGroup, boolean useOwner) throws IOException {
    if (Files.notExists(to)) {
        Files.createDirectories(to);
    }

    InputStream is = Files.newInputStream(file);
    if (file.toString().endsWith(".gz")) {
        is = new GZIPInputStream(is);
    }

    try (TarArchiveInputStream tis = new TarArchiveInputStream(is)) {
        TarArchiveEntry entry;
        while ((entry = tis.getNextTarEntry()) != null) {
            Path entryPath = to.resolve(entry.getName());
            if (entry.isDirectory()) {
                // Skip directory entries that don't add any value
                if (entry.getMode() == 0 && entry.getGroupName() == null && entry.getUserName() == null) {
                    continue;
                }

                if (Files.notExists(entryPath)) {
                    Files.createDirectories(entryPath);
                }

                if (entry.getMode() != 0) {
                    Set<PosixFilePermission> permissions = FileTools.toPosixPermissions(entry.getMode());
                    Files.setPosixFilePermissions(entryPath, permissions);
                }

                if (useGroup && entry.getGroupName() != null && !entry.getGroupName().trim().isEmpty()) {
                    GroupPrincipal group = FileSystems.getDefault().getUserPrincipalLookupService()
                            .lookupPrincipalByGroupName(entry.getGroupName());
                    Files.getFileAttributeView(entryPath, PosixFileAttributeView.class).setGroup(group);
                }

                if (useOwner && entry.getUserName() != null && !entry.getUserName().trim().isEmpty()) {
                    UserPrincipal user = FileSystems.getDefault().getUserPrincipalLookupService()
                            .lookupPrincipalByName(entry.getUserName());
                    Files.getFileAttributeView(entryPath, PosixFileAttributeView.class).setOwner(user);
                }
            } else {
                if (Files.notExists(entryPath.getParent())) {
                    Files.createDirectories(entryPath.getParent());
                }

                if (Files.isRegularFile(entryPath)) {
                    if (Files.size(entryPath) == entry.getSize()) {
                        continue;
                    } else {
                        Files.delete(entryPath);
                    }
                }

                Files.createFile(entryPath);

                try (OutputStream os = Files.newOutputStream(entryPath)) {
                    byte[] ba = new byte[1024];
                    int read;
                    while ((read = tis.read(ba)) != -1) {
                        if (read > 0) {
                            os.write(ba, 0, read);
                        }
                    }
                }

                if (entry.getMode() != 0) {
                    Set<PosixFilePermission> permissions = FileTools.toPosixPermissions(entry.getMode());
                    Files.setPosixFilePermissions(entryPath, permissions);
                }

                if (useGroup && entry.getGroupName() != null && !entry.getGroupName().trim().isEmpty()) {
                    GroupPrincipal group = FileSystems.getDefault().getUserPrincipalLookupService()
                            .lookupPrincipalByGroupName(entry.getGroupName());
                    Files.getFileAttributeView(entryPath, PosixFileAttributeView.class).setGroup(group);
                }

                if (useOwner && entry.getUserName() != null && !entry.getUserName().trim().isEmpty()) {
                    UserPrincipal user = FileSystems.getDefault().getUserPrincipalLookupService()
                            .lookupPrincipalByName(entry.getUserName());
                    Files.getFileAttributeView(entryPath, PosixFileAttributeView.class).setOwner(user);
                }
            }
        }
    }
}