List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry setMode
public void setMode(int mode)
From source file:org.gradle.caching.internal.packaging.impl.TarBuildCacheEntryPacker.java
private static void createTarEntry(String path, long size, int mode, TarArchiveOutputStream tarOutput) throws IOException { TarArchiveEntry entry = new TarArchiveEntry(path, true); entry.setSize(size);// w ww . j a va 2 s. co m entry.setMode(mode); tarOutput.putArchiveEntry(entry); }
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.ngrinder.common.util.CompressionUtils.java
/** * Add a folder into tar.// www . j av a2s . com * * @param tarStream TarArchive outputStream * @param path path to append * @throws IOException thrown when having IO problem. */ public static void addFolderToTar(TarArchiveOutputStream tarStream, String path) throws IOException { TarArchiveEntry archiveEntry = new TarArchiveEntry(path); archiveEntry.setMode(TarArchiveEntry.DEFAULT_DIR_MODE); tarStream.putArchiveEntry(archiveEntry); tarStream.closeArchiveEntry(); }
From source file:org.ngrinder.common.util.CompressionUtils.java
/** * Add the given input stream into tar.//w w w . j a va 2s . c o m * * @param tarStream TarArchive outputStream * @param inputStream input stream * @param path relative path to append * @param size size of stream * @param mode mode for this entry * @throws IOException thrown when having IO problem. */ public static void addInputStreamToTar(TarArchiveOutputStream tarStream, InputStream inputStream, String path, long size, int mode) throws IOException { TarArchiveEntry entry = new TarArchiveEntry(path); entry.setSize(size); entry.setMode(mode); try { tarStream.putArchiveEntry(entry); IOUtils.copy(inputStream, tarStream); } catch (IOException e) { throw processException("Error while adding File to Tar file", e); } finally { tarStream.closeArchiveEntry(); } }
From source file:org.ngrinder.common.util.CompressionUtils.java
/** * Add a file into tar.//from w ww. j av a 2s . c om * * @param tarStream TarArchive outputStream * @param file file * @param path relative path to append * @param mode mode for this entry * @throws IOException thrown when having IO problem. */ public static void addFileToTar(TarArchiveOutputStream tarStream, File file, String path, int mode) throws IOException { TarArchiveEntry entry = new TarArchiveEntry(path); entry.setSize(file.length()); entry.setMode(mode); BufferedInputStream bis = null; try { tarStream.putArchiveEntry(entry); bis = new BufferedInputStream(new FileInputStream(file)); IOUtils.copy(bis, tarStream); } catch (IOException e) { throw processException("Error while adding File to Tar file", e); } finally { IOUtils.closeQuietly(bis); tarStream.closeArchiveEntry(); } }
From source file:org.savantbuild.io.tar.TarBuilder.java
/** * Builds the TAR file using the fileSets and Directories provided. * * @return The number of entries added to the TAR file including the directories. * @throws IOException If the build fails. *///from www .j a v a2s. c o m public int build() throws IOException { if (Files.exists(file)) { Files.delete(file); } if (!Files.isDirectory(file.getParent())) { Files.createDirectories(file.getParent()); } // Sort the file infos and add the directories Set<FileInfo> fileInfos = new TreeSet<>(); for (FileSet fileSet : fileSets) { Set<Directory> dirs = fileSet.toDirectories(); dirs.removeAll(directories); for (Directory dir : dirs) { directories.add(dir); } fileInfos.addAll(fileSet.toFileInfos()); } int count = 0; OutputStream os = Files.newOutputStream(file); try (TarArchiveOutputStream tos = new TarArchiveOutputStream(compress ? new GZIPOutputStream(os) : os)) { tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); for (Directory directory : directories) { String name = directory.name; TarArchiveEntry entry = new TarArchiveEntry(name.endsWith("/") ? name : name + "/"); if (directory.lastModifiedTime != null) { entry.setModTime(directory.lastModifiedTime.toMillis()); } if (directory.mode != null) { entry.setMode(FileTools.toMode(directory.mode)); } if (storeGroupName && directory.groupName != null) { entry.setGroupName(directory.groupName); } if (storeUserName && directory.userName != null) { entry.setUserName(directory.userName); } tos.putArchiveEntry(entry); tos.closeArchiveEntry(); count++; } for (FileInfo fileInfo : fileInfos) { TarArchiveEntry entry = new TarArchiveEntry(fileInfo.relative.toString()); entry.setModTime(fileInfo.lastModifiedTime.toMillis()); if (storeGroupName) { entry.setGroupName(fileInfo.groupName); } if (storeUserName) { entry.setUserName(fileInfo.userName); } entry.setSize(fileInfo.size); entry.setMode(fileInfo.toMode()); tos.putArchiveEntry(entry); Files.copy(fileInfo.origin, tos); tos.closeArchiveEntry(); count++; } } return count; }
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);//from ww w .j a v a 2 s.co m 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(); }
From source file:org.vafer.jdeb.DataBuilder.java
/** * Build the data archive of the deb from the provided DataProducers * * @param producers//from ww w .j a v a 2 s. c om * @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; }
From source file:org.vafer.jdeb.mapping.LsMapper.java
private TarArchiveEntry readDir(final BufferedReader reader, final String base) throws IOException, ParseError { final String current = reader.readLine(); final Matcher currentMatcher = dirPattern.matcher(current); if (!currentMatcher.matches()) { throw new ParseError("expected dirline but got \"" + current + "\""); }/* w w w. j a v a2 s . com*/ final String parent = reader.readLine(); final Matcher parentMatcher = dirPattern.matcher(parent); if (!parentMatcher.matches()) { throw new ParseError("expected dirline but got \"" + parent + "\""); } final TarArchiveEntry entry = new TarArchiveEntry(base, true); entry.setMode(convertModeFromString(currentMatcher.group(1))); entry.setUserName(currentMatcher.group(3)); entry.setGroupName(currentMatcher.group(4)); return entry; }
From source file:org.vafer.jdeb.mapping.LsMapper.java
private TarArchiveEntry readFile(final BufferedReader reader, final String base) throws IOException, ParseError { while (true) { final String line = reader.readLine(); if (line == null) { return null; }/*from w w w . ja v a2 s . co m*/ final Matcher currentMatcher = filePattern.matcher(line); if (!currentMatcher.matches()) { final Matcher newlineMatcher = newlinePattern.matcher(line); if (newlineMatcher.matches()) { return null; } throw new ParseError("expected file line but got \"" + line + "\""); } final String type = currentMatcher.group(1); if (type.startsWith("-")) { final TarArchiveEntry entry = new TarArchiveEntry(base + "/" + currentMatcher.group(8), true); entry.setMode(convertModeFromString(currentMatcher.group(2))); entry.setUserName(currentMatcher.group(4)); entry.setGroupName(currentMatcher.group(5)); return entry; } } }