List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveOutputStream close
public void close() throws IOException
From source file:org.apache.hadoop.yarn.util.TestFSDownload.java
static LocalResource createTarFile(FileContext files, Path p, int len, Random r, LocalResourceVisibility vis) throws IOException, URISyntaxException { byte[] bytes = new byte[len]; r.nextBytes(bytes);/* w ww . j a v a 2 s. c om*/ File archiveFile = new File(p.toUri().getPath() + ".tar"); archiveFile.createNewFile(); TarArchiveOutputStream out = new TarArchiveOutputStream(new FileOutputStream(archiveFile)); TarArchiveEntry entry = new TarArchiveEntry(p.getName()); entry.setSize(bytes.length); out.putArchiveEntry(entry); out.write(bytes); out.closeArchiveEntry(); out.close(); LocalResource ret = recordFactory.newRecordInstance(LocalResource.class); ret.setResource(URL.fromPath(new Path(p.toString() + ".tar"))); ret.setSize(len); ret.setType(LocalResourceType.ARCHIVE); ret.setVisibility(vis); ret.setTimestamp(files.getFileStatus(new Path(p.toString() + ".tar")).getModificationTime()); return ret; }
From source file:org.apache.hadoop.yarn.util.TestFSDownload.java
static LocalResource createTgzFile(FileContext files, Path p, int len, Random r, LocalResourceVisibility vis) throws IOException, URISyntaxException { byte[] bytes = new byte[len]; r.nextBytes(bytes);// w ww . j av a 2 s . co m File gzipFile = new File(p.toUri().getPath() + ".tar.gz"); gzipFile.createNewFile(); TarArchiveOutputStream out = new TarArchiveOutputStream( new GZIPOutputStream(new FileOutputStream(gzipFile))); TarArchiveEntry entry = new TarArchiveEntry(p.getName()); entry.setSize(bytes.length); out.putArchiveEntry(entry); out.write(bytes); out.closeArchiveEntry(); out.close(); LocalResource ret = recordFactory.newRecordInstance(LocalResource.class); ret.setResource(URL.fromPath(new Path(p.toString() + ".tar.gz"))); ret.setSize(len); ret.setType(LocalResourceType.ARCHIVE); ret.setVisibility(vis); ret.setTimestamp(files.getFileStatus(new Path(p.toString() + ".tar.gz")).getModificationTime()); return ret; }
From source file:org.apache.reef.runtime.mesos.driver.REEFScheduler.java
private String getReefTarUri(final String jobIdentifier) { try {//from w ww . ja v a 2s .com // Create REEF_TAR final FileOutputStream fileOutputStream = new FileOutputStream(REEF_TAR); final TarArchiveOutputStream tarArchiveOutputStream = new TarArchiveOutputStream( new GZIPOutputStream(fileOutputStream)); final File globalFolder = new File(this.fileNames.getGlobalFolderPath()); final DirectoryStream<Path> directoryStream = Files.newDirectoryStream(globalFolder.toPath()); for (final Path path : directoryStream) { tarArchiveOutputStream.putArchiveEntry( new TarArchiveEntry(path.toFile(), globalFolder + "/" + path.getFileName())); final BufferedInputStream bufferedInputStream = new BufferedInputStream( new FileInputStream(path.toFile())); IOUtils.copy(bufferedInputStream, tarArchiveOutputStream); bufferedInputStream.close(); tarArchiveOutputStream.closeArchiveEntry(); } directoryStream.close(); tarArchiveOutputStream.close(); fileOutputStream.close(); // Upload REEF_TAR to HDFS final FileSystem fileSystem = FileSystem.get(new Configuration()); final org.apache.hadoop.fs.Path src = new org.apache.hadoop.fs.Path(REEF_TAR); final String reefTarUriValue = fileSystem.getUri().toString() + this.jobSubmissionDirectoryPrefix + "/" + jobIdentifier + "/" + REEF_TAR; final org.apache.hadoop.fs.Path dst = new org.apache.hadoop.fs.Path(reefTarUriValue); fileSystem.copyFromLocalFile(src, dst); return reefTarUriValue; } catch (final IOException e) { throw new RuntimeException(e); } }
From source file:org.apache.tika.server.TarWriter.java
public void writeTo(Map<String, byte[]> parts, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { TarArchiveOutputStream zip = new TarArchiveOutputStream(entityStream); for (Map.Entry<String, byte[]> entry : parts.entrySet()) { tarStoreBuffer(zip, entry.getKey(), entry.getValue()); }/*from www.ja v a 2s . co m*/ zip.close(); }
From source file:org.apache.whirr.util.Tarball.java
/** * Creates a tarball from the source directory and writes it into the target directory. * * @param sourceDirectory directory whose files will be added to the tarball * @param targetName directory where tarball will be written to * @throws IOException when an exception occurs on creating the tarball *///from w w w. ja va 2 s . c o m public static void createFromDirectory(String sourceDirectory, String targetName) throws IOException { FileOutputStream fileOutputStream = null; BufferedOutputStream bufferedOutputStream = null; GzipCompressorOutputStream gzipOutputStream = null; TarArchiveOutputStream tarArchiveOutputStream = null; try { fileOutputStream = new FileOutputStream(new File(targetName)); bufferedOutputStream = new BufferedOutputStream(fileOutputStream); gzipOutputStream = new GzipCompressorOutputStream(bufferedOutputStream); tarArchiveOutputStream = new TarArchiveOutputStream(gzipOutputStream); addFilesInDirectory(tarArchiveOutputStream, sourceDirectory); } finally { if (tarArchiveOutputStream != null) { tarArchiveOutputStream.finish(); } if (tarArchiveOutputStream != null) { tarArchiveOutputStream.close(); } if (gzipOutputStream != null) { gzipOutputStream.close(); } if (bufferedOutputStream != null) { bufferedOutputStream.close(); } if (fileOutputStream != null) { fileOutputStream.close(); } } }
From source file:org.cloudifysource.esc.util.TarGzUtils.java
/** * Create a tar.gz file./* w ww .j ava2 s . c o m*/ * * @param sourcePaths * Folders or files to add in the archive. * @param base * The name to be use in the archive. * @param addRoot * When <code>sourcePath</code> is a folder. if true, it will add the folder in the archive.<br/> * <i>i.e: if sourcepath=/tmp/folderToInclude, archive.tar.gz will include the folder * <b>folderToInclude</b> in the archive.</i> * @return The created archive. * @throws IOException * If the archive cannot be create. */ public static File createTarGz(final String[] sourcePaths, final String base, final boolean addRoot) throws IOException { File tarGzFile = createTempTarGzFile(); if (!FilenameUtils.getExtension(tarGzFile.getName().toLowerCase()).equals("gz")) { throw new IllegalArgumentException("Expecting tar.gz file: " + tarGzFile.getAbsolutePath()); } FileOutputStream fOut = null; BufferedOutputStream bOut = null; GzipCompressorOutputStream gzOut = null; TarArchiveOutputStream tOut = null; try { fOut = new FileOutputStream(tarGzFile); bOut = new BufferedOutputStream(fOut); gzOut = new GzipCompressorOutputStream(bOut); tOut = new TarArchiveOutputStream(gzOut); for (String path : sourcePaths) { addFileToTarGz(tOut, path, base, addRoot); } } finally { if (tOut != null) { tOut.close(); } if (gzOut != null) { gzOut.close(); } if (bOut != null) { bOut.close(); } if (fOut != null) { fOut.close(); } } return tarGzFile; }
From source file:org.codehaus.plexus.archiver.tar.TarRoundTripTest.java
/** * test round-tripping long (GNU) entries *///from ww w .j a v a2 s . c o m public void testLongRoundTripping() throws IOException { TarArchiveEntry original = new TarArchiveEntry(LONG_NAME); assertEquals("over 100 chars", true, LONG_NAME.length() > 100); assertEquals("original name", LONG_NAME, original.getName()); ByteArrayOutputStream buff = new ByteArrayOutputStream(); TarArchiveOutputStream tos = new TarArchiveOutputStream(buff); tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); tos.putArchiveEntry(original); tos.closeArchiveEntry(); tos.close(); TarArchiveInputStream tis = new TarArchiveInputStream(new ByteArrayInputStream(buff.toByteArray())); TarArchiveEntry tripped = tis.getNextTarEntry(); assertEquals("round-tripped name", LONG_NAME, tripped.getName()); assertNull("no more entries", tis.getNextEntry()); tis.close(); }
From source file:org.dspace.pack.bagit.Bag.java
public void deflate(OutputStream out, String fmt) throws IOException { if (!filled) { throw new IllegalStateException("Cannot deflate unfilled bag"); }/*from ww w.ja v a 2 s. com*/ if ("zip".equals(fmt)) { ZipOutputStream zout = new ZipOutputStream(new BufferedOutputStream(out)); fillZip(baseDir, baseDir.getName(), zout); zout.close(); } else if ("tgz".equals(fmt)) { TarArchiveOutputStream tout = new TarArchiveOutputStream( new BufferedOutputStream(new GzipCompressorOutputStream(out))); fillArchive(baseDir, baseDir.getName(), tout); tout.close(); } }
From source file:org.eclipse.che.api.vfs.TarArchiverTest.java
private byte[] createTestTarArchive() throws IOException { ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); TarArchiveOutputStream tarOut = new TarArchiveOutputStream(byteOut); addDirectoryEntry(tarOut, new TarArchiveEntry("arc/")); addDirectoryEntry(tarOut, new TarArchiveEntry("arc/a/")); addFileEntry(tarOut, "arc/a/_a.txt"); addDirectoryEntry(tarOut, new TarArchiveEntry("arc/b/")); addFileEntry(tarOut, "arc/b/_b.txt"); addDirectoryEntry(tarOut, new TarArchiveEntry("arc/c/")); addFileEntry(tarOut, "arc/c/_c.txt"); tarOut.close(); return byteOut.toByteArray(); }
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. //from w w w . j av a 2s . 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; }