List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry setUserId
public void setUserId(int userId)
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 ww w .ja va 2 s . co m*/ 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; {/* w w w. ja v a2s. c om*/ 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.LaunchScriptAction.java
@Override public void run(DebState state) throws Exception { {//from w ww.j av a2 s . 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
@Override public void run(final DebState state) throws Exception { File target = new File(targetDir); LinkedList<File> pathsNeeded = new LinkedList<File>(); File f = target;/*from w w w . ja v a2 s . c o 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.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);//ww w . jav a2s . com 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//from w w w .j ava 2s . c o 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); }//from w ww.jav a 2s. c o m 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 w w w . j a va 2s . 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.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 {//from w w w . j a va 2 s. c o m 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.vafer.jdeb.DataBuilder.java
/** * Build the data archive of the deb from the provided DataProducers * * @param producers/*from w w w. j a v a2s.com*/ * @param output * @param checksums * @param compression the compression method used for the data file * @return * @throws java.security.NoSuchAlgorithmException * @throws java.io.IOException * @throws org.apache.commons.compress.compressors.CompressorException */ BigInteger buildData(Collection<DataProducer> producers, File output, final StringBuilder checksums, Compression compression) throws NoSuchAlgorithmException, IOException, CompressorException { final File dir = output.getParentFile(); if (dir != null && (!dir.exists() || !dir.isDirectory())) { throw new IOException("Cannot write data file at '" + output.getAbsolutePath() + "'"); } final TarArchiveOutputStream tarOutputStream = new TarArchiveOutputStream( compression.toCompressedOutputStream(new FileOutputStream(output))); tarOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); final MessageDigest digest = MessageDigest.getInstance("MD5"); final Total dataSize = new Total(); final List<String> addedDirectories = new ArrayList<String>(); final DataConsumer receiver = new DataConsumer() { public void onEachDir(String dirname, String linkname, String user, int uid, String group, int gid, int mode, long size) throws IOException { dirname = fixPath(dirname); createParentDirectories(dirname, user, uid, group, gid); // The directory passed in explicitly by the caller also gets the passed-in mode. (Unlike // the parent directories for now. See related comments at "int mode =" in // createParentDirectories, including about a possible bug.) createDirectory(dirname, user, uid, group, gid, mode, 0); console.info("dir: " + dirname); } public void onEachFile(InputStream inputStream, String filename, String linkname, String user, int uid, String group, int gid, int mode, long size) throws IOException { filename = fixPath(filename); createParentDirectories(filename, user, uid, group, gid); final TarArchiveEntry entry = new TarArchiveEntry(filename, true); entry.setUserName(user); entry.setUserId(uid); entry.setGroupName(group); entry.setGroupId(gid); entry.setMode(mode); entry.setSize(size); tarOutputStream.putArchiveEntry(entry); dataSize.add(size); digest.reset(); Utils.copy(inputStream, new DigestOutputStream(tarOutputStream, digest)); final String md5 = Utils.toHex(digest.digest()); tarOutputStream.closeArchiveEntry(); console.info("file:" + entry.getName() + " size:" + entry.getSize() + " mode:" + entry.getMode() + " linkname:" + entry.getLinkName() + " username:" + entry.getUserName() + " userid:" + entry.getUserId() + " groupname:" + entry.getGroupName() + " groupid:" + entry.getGroupId() + " modtime:" + entry.getModTime() + " md5: " + md5); // append to file md5 list checksums.append(md5).append(" ").append(entry.getName()).append('\n'); } public void onEachLink(String path, String linkName, boolean symlink, String user, int uid, String group, int gid, int mode) throws IOException { path = fixPath(path); createParentDirectories(path, user, uid, group, gid); final TarArchiveEntry entry = new TarArchiveEntry(path, symlink ? TarArchiveEntry.LF_SYMLINK : TarArchiveEntry.LF_LINK); entry.setLinkName(linkName); entry.setUserName(user); entry.setUserId(uid); entry.setGroupName(group); entry.setGroupId(gid); entry.setMode(mode); tarOutputStream.putArchiveEntry(entry); tarOutputStream.closeArchiveEntry(); console.info("link:" + entry.getName() + " mode:" + entry.getMode() + " linkname:" + entry.getLinkName() + " username:" + entry.getUserName() + " userid:" + entry.getUserId() + " groupname:" + entry.getGroupName() + " groupid:" + entry.getGroupId()); } private void createDirectory(String directory, String user, int uid, String group, int gid, int mode, long size) throws IOException { // All dirs should end with "/" when created, or the test DebAndTaskTestCase.testTarFileSet() thinks its a file // and so thinks it has the wrong permission. // This consistency also helps when checking if a directory already exists in addedDirectories. if (!directory.endsWith("/")) { directory += "/"; } if (!addedDirectories.contains(directory)) { TarArchiveEntry entry = new TarArchiveEntry(directory, true); entry.setUserName(user); entry.setUserId(uid); entry.setGroupName(group); entry.setGroupId(gid); entry.setMode(mode); entry.setSize(size); tarOutputStream.putArchiveEntry(entry); tarOutputStream.closeArchiveEntry(); addedDirectories.add(directory); // so addedDirectories consistently have "/" for finding duplicates. } } private void createParentDirectories(String filename, String user, int uid, String group, int gid) throws IOException { String dirname = fixPath(new File(filename).getParent()); // Debian packages must have parent directories created // before sub-directories or files can be installed. // For example, if an entry of ./usr/lib/foo/bar existed // in a .deb package, but the ./usr/lib/foo directory didn't // exist, the package installation would fail. The .deb must // then have an entry for ./usr/lib/foo and then ./usr/lib/foo/bar if (dirname == null) { return; } // The loop below will create entries for all parent directories // to ensure that .deb packages will install correctly. String[] pathParts = dirname.split("/"); String parentDir = "./"; for (int i = 1; i < pathParts.length; i++) { parentDir += pathParts[i] + "/"; // Make it so the dirs can be traversed by users. // We could instead try something more granular, like setting the directory // permission to 'rx' for each of the 3 user/group/other read permissions // found on the file being added (ie, only if "other" has read // permission on the main node, then add o+rx permission on all the containing // directories, same w/ user & group), and then also we'd have to // check the parentDirs collection of those already added to // see if those permissions need to be similarly updated. (Note, it hasn't // been demonstrated, but there might be a bug if a user specifically // requests a directory with certain permissions, // that has already been auto-created because it was a parent, and if so, go set // the user-requested mode on that directory instead of this automatic one.) // But for now, keeping it simple by making every dir a+rx. Examples are: // drw-r----- fs/fs # what you get with setMode(mode) // drwxr-xr-x fs/fs # Usable. Too loose? int mode = TarArchiveEntry.DEFAULT_DIR_MODE; createDirectory(parentDir, user, uid, group, gid, mode, 0); } } }; try { for (DataProducer data : producers) { data.produce(receiver); } } finally { tarOutputStream.close(); } console.info("Total size: " + dataSize); return dataSize.count; }