List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveOutputStream setLongFileMode
public void setLongFileMode(int longFileMode)
From source file:org.hyperledger.fabric.sdkintegration.Util.java
/** * Generate a targz inputstream from source folder. * * @param src Source location//w ww. j av a 2s . c o m * @param pathPrefix prefix to add to the all files found. * @return return inputstream. * @throws IOException */ public static InputStream generateTarGzInputStream(File src, String pathPrefix) throws IOException { File sourceDirectory = src; ByteArrayOutputStream bos = new ByteArrayOutputStream(500000); String sourcePath = sourceDirectory.getAbsolutePath(); TarArchiveOutputStream archiveOutputStream = new TarArchiveOutputStream( new GzipCompressorOutputStream(new BufferedOutputStream(bos))); archiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); try { Collection<File> childrenFiles = org.apache.commons.io.FileUtils.listFiles(sourceDirectory, null, true); ArchiveEntry archiveEntry; FileInputStream fileInputStream; for (File childFile : childrenFiles) { String childPath = childFile.getAbsolutePath(); String relativePath = childPath.substring((sourcePath.length() + 1), childPath.length()); if (pathPrefix != null) { relativePath = Utils.combinePaths(pathPrefix, relativePath); } relativePath = FilenameUtils.separatorsToUnix(relativePath); archiveEntry = new TarArchiveEntry(childFile, relativePath); fileInputStream = new FileInputStream(childFile); archiveOutputStream.putArchiveEntry(archiveEntry); try { IOUtils.copy(fileInputStream, archiveOutputStream); } finally { IOUtils.closeQuietly(fileInputStream); archiveOutputStream.closeArchiveEntry(); } } } finally { IOUtils.closeQuietly(archiveOutputStream); } return new ByteArrayInputStream(bos.toByteArray()); }
From source file:org.jclouds.docker.compute.features.internal.Archives.java
public static File tar(File baseDir, String archivePath) throws IOException { // Check that the directory is a directory, and get its contents checkArgument(baseDir.isDirectory(), "%s is not a directory", baseDir); File[] files = baseDir.listFiles(); File tarFile = new File(archivePath); String token = getLast(Splitter.on("/").split(archivePath.substring(0, archivePath.lastIndexOf("/")))); byte[] buf = new byte[1024]; int len;//w w w. j a v a 2s . co m TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(tarFile)); tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); for (File file : files) { TarArchiveEntry tarEntry = new TarArchiveEntry(file); tarEntry.setName("/" + getLast(Splitter.on(token).split(file.toString()))); tos.putArchiveEntry(tarEntry); if (!file.isDirectory()) { FileInputStream fin = new FileInputStream(file); BufferedInputStream in = new BufferedInputStream(fin); while ((len = in.read(buf)) != -1) { tos.write(buf, 0, len); } in.close(); } tos.closeArchiveEntry(); } tos.close(); return tarFile; }
From source file:org.jclouds.docker.compute.features.internal.Archives.java
public static File tar(File baseDir, File tarFile) throws IOException { // Check that the directory is a directory, and get its contents checkArgument(baseDir.isDirectory(), "%s is not a directory", baseDir); File[] files = baseDir.listFiles(); String token = getLast(Splitter.on("/").split(baseDir.getAbsolutePath())); byte[] buf = new byte[1024]; int len;/*w w w . j a v a2 s .c om*/ TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(tarFile)); tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); for (File file : files) { TarArchiveEntry tarEntry = new TarArchiveEntry(file); tarEntry.setName("/" + getLast(Splitter.on(token).split(file.toString()))); tos.putArchiveEntry(tarEntry); if (!file.isDirectory()) { FileInputStream fin = new FileInputStream(file); BufferedInputStream in = new BufferedInputStream(fin); while ((len = in.read(buf)) != -1) { tos.write(buf, 0, len); } in.close(); } tos.closeArchiveEntry(); } tos.close(); return tarFile; }
From source file:org.jclouds.docker.features.internal.Archives.java
public static File tar(File baseDir, File tarFile) throws IOException { // Check that the directory is a directory, and get its contents checkArgument(baseDir.isDirectory(), "%s is not a directory", baseDir); File[] files = baseDir.listFiles(); String token = getLast(Splitter.on("/").split(baseDir.getAbsolutePath())); TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(tarFile)); tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); try {/*w w w . j a va 2s. c o m*/ for (File file : files) { TarArchiveEntry tarEntry = new TarArchiveEntry(file); tarEntry.setName("/" + getLast(Splitter.on(token).split(file.toString()))); tos.putArchiveEntry(tarEntry); if (!file.isDirectory()) { Files.asByteSource(file).copyTo(tos); } tos.closeArchiveEntry(); } } finally { tos.close(); } return tarFile; }
From source file:org.jenkinsci.plugins.os_ci.utils.CompressUtils.java
public static void tarGzDirectory(String baseDir, String targetFile) throws FileNotFoundException, IOException { FileOutputStream fOut = null; BufferedOutputStream bOut = null; TarArchiveOutputStream tOut = null; GzipCompressorOutputStream gzOut = null; try {//from w w w .ja v a 2 s . c om System.out.println(new File(".").getAbsolutePath()); fOut = new FileOutputStream(new File(targetFile)); bOut = new BufferedOutputStream(fOut); gzOut = new GzipCompressorOutputStream(bOut); tOut = new TarArchiveOutputStream(gzOut); tOut.setLongFileMode(LONGFILE_POSIX); addFileToTarGz(tOut, baseDir, ""); } finally { if (tOut != null) { tOut.finish(); tOut.close(); } if (gzOut != null) { gzOut.close(); } if (bOut != null) { bOut.close(); } if (fOut != null) { fOut.close(); } } }
From source file:org.vafer.jdeb.ControlBuilder.java
/** * Build control archive of the deb/*from w w w. j a v a 2s . com*/ * * @param packageControlFile the package control file * @param controlFiles the other control information files (maintainer scripts, etc) * @param dataSize the size of the installed package * @param checksums the md5 checksums of the files in the data archive * @param output * @return * @throws java.io.FileNotFoundException * @throws java.io.IOException * @throws java.text.ParseException */ void buildControl(BinaryPackageControlFile packageControlFile, File[] controlFiles, StringBuilder checksums, File output) throws IOException, ParseException { final File dir = output.getParentFile(); if (dir != null && (!dir.exists() || !dir.isDirectory())) { throw new IOException("Cannot write control file at '" + output.getAbsolutePath() + "'"); } final TarArchiveOutputStream outputStream = new TarArchiveOutputStream( new GZIPOutputStream(new FileOutputStream(output))); outputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); // create the final package control file out of the "control" file, copy all other files, ignore the directories for (File file : controlFiles) { if (file.isDirectory()) { // warn about the misplaced directory, except for directories ignored by default (.svn, cvs, etc) if (!isDefaultExcludes(file)) { console.info("Found directory '" + file + "' in the control directory. Maybe you are pointing to wrong dir?"); } continue; } if (CONFIGURATION_FILENAMES.contains(file.getName()) || MAINTAINER_SCRIPTS.contains(file.getName())) { FilteredFile configurationFile = new FilteredFile(new FileInputStream(file), resolver); addControlEntry(file.getName(), configurationFile.toString(), outputStream); } else if (!"control".equals(file.getName())) { // initialize the information stream to guess the type of the file InformationInputStream infoStream = new InformationInputStream(new FileInputStream(file)); Utils.copy(infoStream, NullOutputStream.NULL_OUTPUT_STREAM); infoStream.close(); // fix line endings for shell scripts InputStream in = new FileInputStream(file); if (infoStream.isShell() && !infoStream.hasUnixLineEndings()) { byte[] buf = Utils.toUnixLineEndings(in); in = new ByteArrayInputStream(buf); } addControlEntry(file.getName(), IOUtils.toString(in), outputStream); in.close(); } } if (packageControlFile == null) { throw new FileNotFoundException("No 'control' file found in " + Arrays.toString(controlFiles)); } addControlEntry("control", packageControlFile.toString(), outputStream); addControlEntry("md5sums", checksums.toString(), outputStream); outputStream.close(); }
From source file:org.vafer.jdeb.DataBuilder.java
/** * Build the data archive of the deb from the provided DataProducers * * @param producers// w w w . j a va 2 s . c o m * @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.waarp.common.tar.TarUtility.java
/** * Create a new Tar from a root directory * /*from ww w.ja v a 2 s . c o m*/ * @param directory * the base directory * @param filename * the output filename * @param absolute * store absolute filepath (from directory) or only filename * @return True if OK */ public static boolean createTarFromDirectory(String directory, String filename, boolean absolute) { File rootDir = new File(directory); File saveFile = new File(filename); // recursive call TarArchiveOutputStream taos; try { taos = new TarArchiveOutputStream(new FileOutputStream(saveFile)); } catch (FileNotFoundException e) { return false; } taos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); try { recurseFiles(rootDir, rootDir, taos, absolute); } catch (IOException e2) { try { taos.close(); } catch (IOException e) { // ignore } return false; } try { taos.finish(); } catch (IOException e1) { // ignore } try { taos.flush(); } catch (IOException e) { // ignore } try { taos.close(); } catch (IOException e) { // ignore } return true; }
From source file:org.waarp.common.tar.TarUtility.java
/** * Create a new Tar from an array of Files (only name of files will be used) * // w ww . j ava2 s .co m * @param files * array of files to add * @param filename * the output filename * @return True if OK */ public static boolean createTarFromFiles(File[] files, String filename) { File saveFile = new File(filename); // recursive call TarArchiveOutputStream taos; try { taos = new TarArchiveOutputStream(new FileOutputStream(saveFile)); } catch (FileNotFoundException e) { return false; } taos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); for (File file : files) { try { addFile(file, taos); } catch (IOException e) { try { taos.close(); } catch (IOException e1) { // ignore } return false; } } try { taos.finish(); } catch (IOException e1) { // ignore } try { taos.flush(); } catch (IOException e) { // ignore } try { taos.close(); } catch (IOException e) { // ignore } return true; }
From source file:org.wso2.carbon.connector.util.FileCompressUtil.java
/** * Compress the files based on the archive type * // w ww . j a va2s .c om * * @param files * @param file * @param archiveType * @throws IOException */ public void compressFiles(Collection files, File file, ArchiveType archiveType) throws IOException { log.info("Compressing " + files.size() + " to " + file.getAbsoluteFile()); // Create the output stream for the output file FileOutputStream fos; switch (archiveType) { case TAR_GZIP: fos = new FileOutputStream(new File(file.getCanonicalPath() + ".tar" + ".gz")); // Wrap the output file stream in streams that will tar and gzip // everything TarArchiveOutputStream taos = new TarArchiveOutputStream( new GZIPOutputStream(new BufferedOutputStream(fos))); // TAR has an 8 gig file limit by default, this gets around that taos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR); // to get past the 8 gig limit; TAR originally didn't support // long file names, so enable the // support for it taos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); // Get to putting all the files in the compressed output file Iterator iterator = files.iterator(); while (iterator.hasNext()) { File f = (File) iterator.next(); addFilesToCompression(taos, f, ".", ArchiveType.TAR_GZIP); // do something to object here... } // Close everything up taos.close(); fos.close(); break; case ZIP: fos = new FileOutputStream(new File(file.getCanonicalPath() + ".zip")); // Wrap the output file stream in streams that will tar and zip // everything ZipArchiveOutputStream zaos = new ZipArchiveOutputStream(new BufferedOutputStream(fos)); zaos.setEncoding("UTF-8"); zaos.setCreateUnicodeExtraFields(ZipArchiveOutputStream.UnicodeExtraFieldPolicy.ALWAYS); // Get to putting all the files in the compressed output file Iterator iterator1 = files.iterator(); while (iterator1.hasNext()) { File f = (File) iterator1.next(); addFilesToCompression(zaos, f, ".", ArchiveType.ZIP); // do something to object here... } // Close everything up zaos.close(); fos.close(); break; } }