List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveOutputStream closeArchiveEntry
public void closeArchiveEntry() throws IOException
From source file:org.eclipse.orion.server.docker.server.DockerFile.java
/** * Get the tar file containing the Dockerfile that can be sent to the Docker * build API to create an image. // ww w . j a va 2 s. c o m * * @return The tar file. */ public File getTarFile() { try { // get the temporary folder location. String tmpDirName = System.getProperty("java.io.tmpdir"); File tmpDir = new File(tmpDirName); if (!tmpDir.exists() || !tmpDir.isDirectory()) { if (logger.isDebugEnabled()) { logger.error("Cannot find the default temporary-file directory: " + tmpDirName); } return null; } // get a temporary folder name long n = random.nextLong(); n = (n == Long.MIN_VALUE) ? 0 : Math.abs(n); String tmpDirStr = Long.toString(n); tempFolder = new File(tmpDir, tmpDirStr); if (!tempFolder.mkdir()) { if (logger.isDebugEnabled()) { logger.error("Cannot create a temporary directory at " + tempFolder.toString()); } return null; } if (logger.isDebugEnabled()) { logger.debug("Dockerfile: Created a temporary directory at " + tempFolder.toString()); } // create the Dockerfile dockerfile = new File(tempFolder, "Dockerfile"); FileOutputStream fileOutputStream = new FileOutputStream(dockerfile); Charset utf8 = Charset.forName("UTF-8"); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, utf8); outputStreamWriter.write(getDockerfileContent()); outputStreamWriter.flush(); outputStreamWriter.close(); fileOutputStream.close(); dockerTarFile = new File(tempFolder, "Dockerfile.tar"); TarArchiveOutputStream tarArchiveOutputStream = new TarArchiveOutputStream( new FileOutputStream(dockerTarFile)); tarArchiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); TarArchiveEntry tarEntry = new TarArchiveEntry(dockerfile); tarEntry.setName(dockerfile.getName()); tarArchiveOutputStream.putArchiveEntry(tarEntry); FileInputStream fileInputStream = new FileInputStream(dockerfile); BufferedInputStream inputStream = new BufferedInputStream(fileInputStream); byte[] buffer = new byte[4096]; int bytes_read; while ((bytes_read = inputStream.read(buffer)) != -1) { tarArchiveOutputStream.write(buffer, 0, bytes_read); } inputStream.close(); tarArchiveOutputStream.closeArchiveEntry(); tarArchiveOutputStream.close(); if (logger.isDebugEnabled()) { logger.debug("Dockerfile: Created a docker tar file at " + dockerTarFile.toString()); } return dockerTarFile; } catch (IOException e) { logger.error(e.getLocalizedMessage(), e); } return null; }
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 va2 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(); }
From source file:org.eclipse.scada.utils.pkg.deb.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 a2 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()); entry.setModTime(this.getTimestampProvider().getModTime()); out.putArchiveEntry(entry); try (InputStream stream = content.createInputStream()) { ByteStreams.copy(stream, out); } out.closeArchiveEntry(); }
From source file:org.eclipse.tracecompass.integration.swtbot.tests.projectexplorer.TestDirectoryStructureUtil.java
private static void addToArchive(TarArchiveOutputStream taos, File dir, File root) throws IOException { byte[] buffer = new byte[1024]; int length;/*w w w .j a v a2 s . c om*/ int index = root.getAbsolutePath().length(); for (File file : dir.listFiles()) { String name = file.getAbsolutePath().substring(index); if (file.isDirectory()) { if (file.listFiles().length != 0) { addToArchive(taos, file, root); } else { taos.putArchiveEntry(new TarArchiveEntry(name + File.separator)); taos.closeArchiveEntry(); } } else { try (FileInputStream fis = new FileInputStream(file)) { TarArchiveEntry entry = new TarArchiveEntry(name); entry.setSize(file.length()); taos.putArchiveEntry(entry); while ((length = fis.read(buffer)) > 0) { taos.write(buffer, 0, length); } taos.closeArchiveEntry(); } } } }
From source file:org.eclipse.tycho.plugins.tar.TarGzArchiver.java
private void addToTarRecursively(File tarRootDir, File source, TarArchiveOutputStream tarStream) throws IOException { TarArchiveEntry tarEntry = createTarEntry(tarRootDir, source); tarStream.putArchiveEntry(tarEntry); if (source.isFile() && !tarEntry.isSymbolicLink()) { copyFileContentToTarStream(source, tarStream); }// w w w .j a v a2 s . com tarStream.closeArchiveEntry(); if (source.isDirectory()) { File[] children = source.listFiles(); if (children != null) { for (File child : children) { addToTarRecursively(tarRootDir, child, tarStream); } } } }
From source file:org.finra.herd.service.helper.TarHelper.java
/** * Adds a TAR archive entry to the specified TAR archive stream. The method calls itself recursively for all directories/files found. * * @param tarArchiveOutputStream the TAR output stream that writes a UNIX tar archive as an output stream * @param path the path relative to the base for a directory or file to be added to the TAR archive stream * @param base the base for the directory or file path * * @throws IOException on error/*from w w w.j ava 2 s. c om*/ */ private void addEntryToTarArchive(TarArchiveOutputStream tarArchiveOutputStream, Path path, Path base) throws IOException { File file = path.toFile(); Path entry = Paths.get(base.toString(), file.getName()); TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(file, entry.toString()); tarArchiveOutputStream.putArchiveEntry(tarArchiveEntry); if (file.isFile()) { try (FileInputStream fileInputStream = new FileInputStream(file)) { // TODO: This method uses a default buffer size of 8K. // TODO: Taking a file size in consideration, a bigger buffer size we might increase the performance of the tar. IOUtils.copy(fileInputStream, tarArchiveOutputStream); } finally { tarArchiveOutputStream.closeArchiveEntry(); } } else { tarArchiveOutputStream.closeArchiveEntry(); File[] children = file.listFiles(); if (children != null) { for (File child : children) { addEntryToTarArchive(tarArchiveOutputStream, Paths.get(child.getAbsolutePath()), entry); } } } }
From source file:org.gradle.caching.internal.packaging.impl.TarBuildCacheEntryPacker.java
private void packMetadata(OriginWriter writeMetadata, TarArchiveOutputStream tarOutput) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); writeMetadata.execute(baos);//from w ww . j a v a 2 s.c o m createTarEntry(METADATA_PATH, baos.size(), UnixPermissions.FILE_FLAG | UnixPermissions.DEFAULT_FILE_PERM, tarOutput); tarOutput.write(baos.toByteArray()); tarOutput.closeArchiveEntry(); }
From source file:org.gradle.caching.internal.tasks.CommonsTarPacker.java
@Override public void pack(List<DataSource> inputs, DataTarget output) throws IOException { TarArchiveOutputStream tarOutput = new TarArchiveOutputStream(output.openOutput()); for (DataSource input : inputs) { TarArchiveEntry entry = new TarArchiveEntry(input.getName()); entry.setSize(input.getLength()); tarOutput.putArchiveEntry(entry); PackerUtils.packEntry(input, tarOutput, buffer); tarOutput.closeArchiveEntry(); }/*from w ww.j av a 2 s . c om*/ tarOutput.close(); }
From source file:org.hyperledger.fabric.sdk.helper.SDKUtil.java
/** * Compress the given directory src to target tar.gz file * @param src The source directory// w w w.jav a 2s. c om * @param target The target tar.gz file * @throws IOException */ public static void generateTarGz(String src, String target) throws IOException { File sourceDirectory = new File(src); File destinationArchive = new File(target); String sourcePath = sourceDirectory.getAbsolutePath(); FileOutputStream destinationOutputStream = new FileOutputStream(destinationArchive); TarArchiveOutputStream archiveOutputStream = new TarArchiveOutputStream( new GzipCompressorOutputStream(new BufferedOutputStream(destinationOutputStream))); archiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); try { Collection<File> childrenFiles = org.apache.commons.io.FileUtils.listFiles(sourceDirectory, null, true); childrenFiles.remove(destinationArchive); ArchiveEntry archiveEntry; FileInputStream fileInputStream; for (File childFile : childrenFiles) { String childPath = childFile.getAbsolutePath(); String relativePath = childPath.substring((sourcePath.length() + 1), childPath.length()); 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); } }
From source file:org.hyperledger.fabric.sdk.helper.Utils.java
/** * Compress the contents of given directory using Tar and Gzip to an in-memory byte array. * * @param sourceDirectory the source directory. * @param pathPrefix a path to be prepended to every file name in the .tar.gz output, or {@code null} if no prefix is required. * @param chaincodeMetaInf//from w ww . j a v a 2 s . c o m * @return the compressed directory contents. * @throws IOException */ public static byte[] generateTarGz(File sourceDirectory, String pathPrefix, File chaincodeMetaInf) throws IOException { logger.trace(format("generateTarGz: sourceDirectory: %s, pathPrefix: %s, chaincodeMetaInf: %s", sourceDirectory == null ? "null" : sourceDirectory.getAbsolutePath(), pathPrefix, chaincodeMetaInf == null ? "null" : chaincodeMetaInf.getAbsolutePath())); ByteArrayOutputStream bos = new ByteArrayOutputStream(500000); String sourcePath = sourceDirectory.getAbsolutePath(); TarArchiveOutputStream archiveOutputStream = new TarArchiveOutputStream( new GzipCompressorOutputStream(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); if (TRACE_ENABED) { logger.trace(format("generateTarGz: Adding '%s' entry from source '%s' to archive.", relativePath, childFile.getAbsolutePath())); } archiveEntry = new TarArchiveEntry(childFile, relativePath); fileInputStream = new FileInputStream(childFile); archiveOutputStream.putArchiveEntry(archiveEntry); try { IOUtils.copy(fileInputStream, archiveOutputStream); } finally { IOUtils.closeQuietly(fileInputStream); archiveOutputStream.closeArchiveEntry(); } } if (null != chaincodeMetaInf) { childrenFiles = org.apache.commons.io.FileUtils.listFiles(chaincodeMetaInf, null, true); final URI metabase = chaincodeMetaInf.toURI(); for (File childFile : childrenFiles) { final String relativePath = Paths .get("META-INF", metabase.relativize(childFile.toURI()).getPath()).toString(); if (TRACE_ENABED) { logger.trace(format("generateTarGz: Adding '%s' entry from source '%s' to archive.", relativePath, childFile.getAbsolutePath())); } 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 bos.toByteArray(); }