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

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

Introduction

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

Prototype

public void setUserName(String userName) 

Source Link

Document

Set this entry's user name.

Usage

From source file:com.moss.simpledeb.core.action.DigestAction.java

@Override
public void run(DebState state) throws Exception {

    final StringBuilder sb = new StringBuilder();
    final MessageDigest digest = java.security.MessageDigest.getInstance("MD5");

    for (ArchivePath path : state.contentPaths) {

        if (path.entry().isDirectory()) {
            continue;
        }/*from w w  w . j av  a 2 s  .  c om*/

        byte[] fileData;
        {
            InputStream in = path.read();
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024 * 10]; //10k buffer
            for (int numRead = in.read(buffer); numRead != -1; numRead = in.read(buffer)) {
                out.write(buffer, 0, numRead);
            }
            in.close();
            out.close();
            fileData = out.toByteArray();
        }

        digest.update(fileData);
        byte[] hash = digest.digest();
        digest.reset();

        sb.append(HexUtil.toHex(hash).toLowerCase());
        sb.append("  ");
        sb.append(path.entry().getName());
        sb.append("\n");
    }

    byte[] data = sb.toString().getBytes();

    TarArchiveEntry tarEntry = new TarArchiveEntry("md5sum");
    tarEntry.setGroupId(0);
    tarEntry.setGroupName("root");
    tarEntry.setIds(0, 0);
    tarEntry.setModTime(System.currentTimeMillis());
    tarEntry.setSize(data.length);
    tarEntry.setUserId(0);
    tarEntry.setUserName("root");

    state.addPath(DebComponent.CONTROL, new BytesArchivePath(tarEntry, data));
}

From source file:com.moss.simpledeb.core.action.ControlAction.java

@Override
public void run(DebState state) throws Exception {

    require("Package", packageName);
    require("Version", version);
    require("Architecture", architecture);
    require("Depends", depends);
    require("Maintainer", maintainer);
    require("Description", description);

    byte[] controlFileData;
    {/* ww  w  .  j  av  a2s. c o m*/
        StringBuilder sb = new StringBuilder();
        sb.append("Package: ").append(packageName).append("\n");
        sb.append("Version: ").append(version).append("\n");
        sb.append("Architecture: ").append(architecture).append("\n");
        sb.append("Depends: ").append(depends).append("\n");
        sb.append("Maintainer: ").append(maintainer).append("\n");
        sb.append("Description: ").append(description).append("\n");

        controlFileData = sb.toString().getBytes();
    }

    TarArchiveEntry tarEntry = new TarArchiveEntry("control");
    tarEntry.setGroupId(0);
    tarEntry.setGroupName("root");
    tarEntry.setIds(0, 0);
    tarEntry.setModTime(System.currentTimeMillis());
    tarEntry.setSize(controlFileData.length);
    tarEntry.setUserId(0);
    tarEntry.setUserName("root");

    state.addPath(DebComponent.CONTROL, new BytesArchivePath(tarEntry, controlFileData));
}

From source file:com.moss.simpledeb.core.action.CopyAction.java

@Override
public void run(final DebState state) throws Exception {

    File target = new File(targetDir);
    LinkedList<File> pathsNeeded = new LinkedList<File>();

    File f = target;/*from www .  j  a va  2s  .  co m*/
    while (f != null) {
        pathsNeeded.addFirst(f);
        f = f.getParentFile();
    }

    for (int i = 0; i < assumedTargetPathLevel; i++) {
        pathsNeeded.removeFirst();
    }

    for (File e : pathsNeeded) {
        String p = "./" + e.getPath();

        if (!p.endsWith("/")) {
            p = p + "/";
        }

        TarArchiveEntry tarEntry = new TarArchiveEntry(p);
        tarEntry.setGroupId(0);
        tarEntry.setGroupName("root");
        tarEntry.setIds(0, 0);
        tarEntry.setModTime(System.currentTimeMillis());
        tarEntry.setSize(0);
        tarEntry.setUserId(0);
        tarEntry.setUserName("root");
        tarEntry.setMode(Integer.parseInt("755", 8));

        ArchivePath path = new DirArchivePath(tarEntry);
        state.addPath(component, path);
    }

    files.visit(new FileVisitor() {
        public void file(File file) {
            try {
                copyFile(file, state);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    });
}

From source file:com.moss.simpledeb.core.action.LaunchScriptAction.java

@Override
public void run(DebState state) throws Exception {

    {// w w  w. j  a v a  2s.  c om
        File target = new File(targetFile).getParentFile();
        LinkedList<File> pathsNeeded = new LinkedList<File>();

        File f = target;
        while (f != null) {
            pathsNeeded.addFirst(f);
            f = f.getParentFile();
        }

        for (int i = 0; i < pathLevel; i++) {
            pathsNeeded.removeFirst();
        }

        for (File e : pathsNeeded) {
            String p = "./" + e.getPath();

            if (!p.endsWith("/")) {
                p = p + "/";
            }

            TarArchiveEntry tarEntry = new TarArchiveEntry(p);
            tarEntry.setGroupId(0);
            tarEntry.setGroupName("root");
            tarEntry.setIds(0, 0);
            tarEntry.setModTime(System.currentTimeMillis());
            tarEntry.setSize(0);
            tarEntry.setUserId(0);
            tarEntry.setUserName("root");
            tarEntry.setMode(Integer.parseInt("755", 8));

            ArchivePath path = new DirArchivePath(tarEntry);
            state.addPath(DebComponent.CONTENT, path);
        }
    }

    String cp;
    {
        StringBuffer sb = new StringBuffer();
        for (String path : state.classpath) {
            if (sb.length() == 0) {
                sb.append(path);
            } else {
                sb.append(":");
                sb.append(path);
            }
        }
        cp = sb.toString();
    }

    StringBuilder sb = new StringBuilder();
    sb.append("#!/bin/bash\n");
    sb.append("CP=\"");
    sb.append(cp);
    sb.append("\"\n");
    sb.append("/usr/bin/java -cp $CP ");
    sb.append(className);
    sb.append(" $@\n");

    byte[] data = sb.toString().getBytes();

    String entryName = "./" + targetFile;

    TarArchiveEntry tarEntry = new TarArchiveEntry(entryName);
    tarEntry.setGroupId(0);
    tarEntry.setGroupName("root");
    tarEntry.setIds(0, 0);
    tarEntry.setModTime(System.currentTimeMillis());
    tarEntry.setSize(data.length);
    tarEntry.setUserId(0);
    tarEntry.setUserName("root");
    tarEntry.setMode(Integer.parseInt("755", 8));

    ArchivePath path = new BytesArchivePath(tarEntry, data);
    state.addPath(DebComponent.CONTENT, path);
}

From source file:com.moss.simpledeb.core.action.CopyAction.java

private void copyFile(File file, DebState state) throws Exception {

    String entryName = "./" + targetDir + "/" + file.getName();

    if (!file.isDirectory()) {

        TarArchiveEntry tarEntry = new TarArchiveEntry(entryName);
        tarEntry.setGroupId(0);/*from  w  ww. j ava 2 s  .  c om*/
        tarEntry.setGroupName("root");
        tarEntry.setIds(0, 0);
        tarEntry.setModTime(System.currentTimeMillis());
        tarEntry.setSize(file.length());
        tarEntry.setUserId(0);
        tarEntry.setUserName("root");
        tarEntry.setMode(Integer.parseInt(fileMode, 8));

        ArchivePath path = new FileArchivePath(tarEntry, file);
        state.addPath(component, path);

        if (appendToClasspath) {
            state.classpath.add("/" + targetDir + "/" + file.getName());
        }
    } else {
        entryName = entryName + "/";

        TarArchiveEntry tarEntry = new TarArchiveEntry(entryName);
        tarEntry.setGroupId(0);
        tarEntry.setGroupName("root");
        tarEntry.setIds(0, 0);
        tarEntry.setModTime(System.currentTimeMillis());
        tarEntry.setSize(0);
        tarEntry.setUserId(0);
        tarEntry.setUserName("root");
        tarEntry.setMode(Integer.parseInt(dirMode, 8));

        ArchivePath path = new DirArchivePath(tarEntry);
        state.addPath(component, path);
    }
}

From source file:com.mobilesorcery.sdk.builder.linux.deb.DebBuilder.java

/**
 * Adds the files in the file list in a tar+gz
 *
 * @param o Output file/*ww  w.jav  a 2s .co  m*/
 *
 * @throws IOException If error occurs during writing
 * @throws FileNotFoundException If the output file could not be opened.
 */
private void doAddFilesToTarGZip(File o) throws IOException, FileNotFoundException

{
    FileOutputStream os = new FileOutputStream(o);
    GzipCompressorOutputStream gzos = new GzipCompressorOutputStream(os);
    TarArchiveOutputStream tos = new TarArchiveOutputStream(gzos);

    // Add files
    for (SimpleEntry<File, SimpleEntry<String, Integer>> fileEntry : m_fileList) {
        File file = fileEntry.getKey();
        String name = fileEntry.getValue().getKey();
        int mode = fileEntry.getValue().getValue();
        TarArchiveEntry e = new TarArchiveEntry(file, name);

        // Add to tar, user/group id 0 is always root
        e.setMode(mode);
        e.setUserId(0);
        e.setUserName("root");
        e.setGroupId(0);
        e.setGroupName("root");
        tos.putArchiveEntry(e);

        // Write bytes
        if (file.isFile())
            BuilderUtil.getInstance().copyFileToOutputStream(tos, file);
        tos.closeArchiveEntry();
    }

    // Done
    tos.close();
    gzos.close();
    os.close();
}

From source file:org.apache.ant.compress.taskdefs.Tar.java

public Tar() {
    setFactory(new TarStreamFactory() {
        public ArchiveOutputStream getArchiveStream(OutputStream stream, String encoding) throws IOException {
            TarArchiveOutputStream o = (TarArchiveOutputStream) super.getArchiveStream(stream, encoding);
            if (format.equals(Format.OLDGNU)) {
                o.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
            } else if (format.equals(Format.GNU)) {
                o.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
                o.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR);
            } else if (format.equals(Format.STAR)) {
                o.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
                o.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR);
            } else if (format.equals(Format.PAX)) {
                o.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
                o.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
                o.setAddPaxHeadersForNonAsciiNames(true);
            }/* www  .j  a  va 2  s.  c om*/
            return o;
        }
    });
    setEntryBuilder(new ArchiveBase.EntryBuilder() {
        public ArchiveEntry buildEntry(ArchiveBase.ResourceWithFlags r) {
            boolean isDir = r.getResource().isDirectory();
            String name = r.getName();
            if (isDir && !name.endsWith("/")) {
                name += "/";
            } else if (!isDir && name.endsWith("/")) {
                name = name.substring(0, name.length() - 1);
            }
            TarArchiveEntry ent = new TarArchiveEntry(name, getPreserveLeadingSlashes());
            ent.setModTime(round(r.getResource().getLastModified(), 1000));
            ent.setSize(isDir ? 0 : r.getResource().getSize());

            if (!isDir && r.getCollectionFlags().hasModeBeenSet()) {
                ent.setMode(r.getCollectionFlags().getMode());
            } else if (isDir && r.getCollectionFlags().hasDirModeBeenSet()) {
                ent.setMode(r.getCollectionFlags().getDirMode());
            } else if (r.getResourceFlags().hasModeBeenSet()) {
                ent.setMode(r.getResourceFlags().getMode());
            } else {
                ent.setMode(isDir ? ArchiveFileSet.DEFAULT_DIR_MODE : ArchiveFileSet.DEFAULT_FILE_MODE);
            }

            if (r.getResourceFlags().hasUserIdBeenSet()) {
                ent.setUserId(r.getResourceFlags().getUserId());
            } else if (r.getCollectionFlags().hasUserIdBeenSet()) {
                ent.setUserId(r.getCollectionFlags().getUserId());
            }

            if (r.getResourceFlags().hasGroupIdBeenSet()) {
                ent.setGroupId(r.getResourceFlags().getGroupId());
            } else if (r.getCollectionFlags().hasGroupIdBeenSet()) {
                ent.setGroupId(r.getCollectionFlags().getGroupId());
            }

            if (r.getResourceFlags().hasUserNameBeenSet()) {
                ent.setUserName(r.getResourceFlags().getUserName());
            } else if (r.getCollectionFlags().hasUserNameBeenSet()) {
                ent.setUserName(r.getCollectionFlags().getUserName());
            }

            if (r.getResourceFlags().hasGroupNameBeenSet()) {
                ent.setGroupName(r.getResourceFlags().getGroupName());
            } else if (r.getCollectionFlags().hasGroupNameBeenSet()) {
                ent.setGroupName(r.getCollectionFlags().getGroupName());
            }

            return ent;
        }
    });
    setFileSetBuilder(new ArchiveBase.FileSetBuilder() {
        public ArchiveFileSet buildFileSet(Resource dest) {
            ArchiveFileSet afs = new TarFileSet();
            afs.setSrcResource(dest);
            return afs;
        }
    });
}

From source file:org.codehaus.plexus.archiver.tar.TarArchiver.java

/**
 * tar a file//from   www . java2s. c  o  m
 *
 * @param entry the file to tar
 * @param tOut  the output stream
 * @param vPath the path name of the file to tar
 * @throws IOException on error
 */
protected void tarFile(ArchiveEntry entry, TarArchiveOutputStream tOut, String vPath)
        throws ArchiverException, IOException {

    // don't add "" to the archive
    if (vPath.length() <= 0) {
        return;
    }

    if (entry.getResource().isDirectory() && !vPath.endsWith("/")) {
        vPath += "/";
    }

    if (vPath.startsWith("/") && !options.getPreserveLeadingSlashes()) {
        int l = vPath.length();
        if (l <= 1) {
            // we would end up adding "" to the archive
            return;
        }
        vPath = vPath.substring(1, l);
    }

    int pathLength = vPath.length();
    InputStream fIn = null;

    try {
        TarArchiveEntry te;
        if (!longFileMode.isGnuMode()
                && pathLength >= org.apache.commons.compress.archivers.tar.TarConstants.NAMELEN) {
            int maxPosixPathLen = org.apache.commons.compress.archivers.tar.TarConstants.NAMELEN
                    + org.apache.commons.compress.archivers.tar.TarConstants.PREFIXLEN;
            if (longFileMode.isPosixMode()) {
            } else if (longFileMode.isPosixWarnMode()) {
                if (pathLength > maxPosixPathLen) {
                    getLogger().warn("Entry: " + vPath + " longer than " + maxPosixPathLen + " characters.");
                    if (!longWarningGiven) {
                        getLogger().warn("Resulting tar file can only be processed "
                                + "successfully by GNU compatible tar commands");
                        longWarningGiven = true;
                    }
                }
            } else if (longFileMode.isOmitMode()) {
                getLogger().info("Omitting: " + vPath);
                return;
            } else if (longFileMode.isWarnMode()) {
                getLogger().warn("Entry: " + vPath + " longer than "
                        + org.apache.commons.compress.archivers.tar.TarConstants.NAMELEN + " characters.");
                if (!longWarningGiven) {
                    getLogger().warn("Resulting tar file can only be processed "
                            + "successfully by GNU compatible tar commands");
                    longWarningGiven = true;
                }
            } else if (longFileMode.isFailMode()) {
                throw new ArchiverException("Entry: " + vPath + " longer than "
                        + org.apache.commons.compress.archivers.tar.TarConstants.NAMELEN + " characters.");
            } else {
                throw new IllegalStateException("Non gnu mode should never get here?");
            }
        }

        if (entry.getType() == ArchiveEntry.SYMLINK) {
            final SymlinkDestinationSupplier plexusIoSymlinkResource = (SymlinkDestinationSupplier) entry
                    .getResource();
            te = new TarArchiveEntry(vPath, TarArchiveEntry.LF_SYMLINK);
            te.setLinkName(plexusIoSymlinkResource.getSymlinkDestination());
        } else {
            te = new TarArchiveEntry(vPath);
        }

        long teLastModified = entry.getResource().getLastModified();
        te.setModTime(teLastModified == PlexusIoResource.UNKNOWN_MODIFICATION_DATE ? System.currentTimeMillis()
                : teLastModified);

        if (entry.getType() == ArchiveEntry.SYMLINK) {
            te.setSize(0);

        } else if (!entry.getResource().isDirectory()) {
            final long size = entry.getResource().getSize();
            te.setSize(size == PlexusIoResource.UNKNOWN_RESOURCE_SIZE ? 0 : size);
        }
        te.setMode(entry.getMode());

        PlexusIoResourceAttributes attributes = entry.getResourceAttributes();

        te.setUserName((attributes != null && attributes.getUserName() != null) ? attributes.getUserName()
                : options.getUserName());
        te.setGroupName((attributes != null && attributes.getGroupName() != null) ? attributes.getGroupName()
                : options.getGroup());

        final int userId = (attributes != null && attributes.getUserId() != null) ? attributes.getUserId()
                : options.getUid();
        if (userId >= 0) {
            te.setUserId(userId);
        }

        final int groupId = (attributes != null && attributes.getGroupId() != null) ? attributes.getGroupId()
                : options.getGid();
        if (groupId >= 0) {
            te.setGroupId(groupId);
        }

        tOut.putArchiveEntry(te);

        try {
            if (entry.getResource().isFile() && !(entry.getType() == ArchiveEntry.SYMLINK)) {
                fIn = entry.getInputStream();

                Streams.copyFullyDontCloseOutput(fIn, tOut, "xAR");
            }

        } catch (Throwable e) {
            getLogger().warn("When creating tar entry", e);
        } finally {
            tOut.closeArchiveEntry();
        }
    } finally {
        IOUtil.close(fIn);
    }
}

From source file:org.eclipse.packagedrone.utils.deb.build.DebianPackageWriter.java

private static void applyInfo(final TarArchiveEntry entry, final EntryInformation entryInformation) {
    if (entryInformation == null) {
        return;//from ww w. j a  va  2  s  .co m
    }

    if (entryInformation.getUser() != null) {
        entry.setUserName(entryInformation.getUser());
    }
    if (entryInformation.getGroup() != null) {
        entry.setGroupName(entryInformation.getGroup());
    }
    entry.setMode(entryInformation.getMode());
}

From source file:org.eclipse.packagedrone.utils.deb.build.DebianPackageWriter.java

private void addControlContent(final TarArchiveOutputStream out, final String name,
        final ContentProvider content, final int mode) throws IOException {
    if (content == null || !content.hasContent()) {
        return;/*from   w w w .  j a  v  a  2 s.  c  o m*/
    }

    final TarArchiveEntry entry = new TarArchiveEntry(name);
    if (mode >= 0) {
        entry.setMode(mode);
    }

    entry.setUserName("root");
    entry.setGroupName("root");
    entry.setSize(content.getSize());
    out.putArchiveEntry(entry);
    try (InputStream stream = content.createInputStream()) {
        IOUtils.copy(stream, out);
    }
    out.closeArchiveEntry();
}