org.apache.commons.compress.archivers.tar.TarArchiveOutputStream Java Examples
The following examples show how to use
org.apache.commons.compress.archivers.tar.TarArchiveOutputStream.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: IOUtils.java From gatk with BSD 3-Clause "New" or "Revised" License | 6 votes |
private static void addToTar(TarArchiveOutputStream out, File file, String dir) throws IOException { String entry = dir + File.separator + file.getName(); if (file.isFile()){ out.putArchiveEntry(new TarArchiveEntry(file, entry)); try (FileInputStream in = new FileInputStream(file)){ org.apache.commons.compress.utils.IOUtils.copy(in, out); } out.closeArchiveEntry(); } else if (file.isDirectory()) { File[] children = file.listFiles(); if (children != null){ for (File child : children){ addToTar(out, child, entry); } } } else { System.out.println(file.getName() + " is not supported"); } }
Example #2
Source File: CompressExtension.java From jphp with Apache License 2.0 | 6 votes |
@Override public void onRegister(CompileScope scope) { // register classes ... registerWrapperClass(scope, ArchiveEntry.class, PArchiveEntry.class); registerWrapperClass(scope, TarArchiveEntry.class, PTarArchiveEntry.class); registerWrapperClass(scope, ZipArchiveEntry.class, PZipArchiveEntry.class); registerWrapperClass(scope, ArchiveInputStream.class, PArchiveInput.class); registerWrapperClass(scope, TarArchiveInputStream.class, PTarArchiveInput.class); registerWrapperClass(scope, ZipArchiveInputStream.class, PZipArchiveInput.class); registerWrapperClass(scope, ArchiveOutputStream.class, PArchiveOutput.class); registerWrapperClass(scope, TarArchiveOutputStream.class, PTarArchiveOutput.class); registerWrapperClass(scope, ZipArchiveOutputStream.class, PZipArchiveOutput.class); registerClass(scope, PGzipOutputStream.class); registerClass(scope, PGzipInputStream.class); registerClass(scope, PBzip2OutputStream.class); registerClass(scope, PBZip2InputStream.class); registerClass(scope, PLz4OutputStream.class); registerClass(scope, PLz4InputStream.class); registerClass(scope, PArchive.class); registerClass(scope, PTarArchive.class); registerClass(scope, PZipArchive.class); }
Example #3
Source File: CompressArchiveUtil.java From docker-java with Apache License 2.0 | 6 votes |
public static File archiveTARFiles(File base, Iterable<File> files, String archiveNameWithOutExtension) throws IOException { File tarFile = new File(FileUtils.getTempDirectoryPath(), archiveNameWithOutExtension + ".tar"); tarFile.deleteOnExit(); try (TarArchiveOutputStream tos = new TarArchiveOutputStream(new GZIPOutputStream(new BufferedOutputStream( new FileOutputStream(tarFile))))) { tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); for (File file : files) { // relativize with method using Path otherwise method with File resolves the symlinks // and this is not want we want. If the file is a symlink, the relativized path should // keep the symlink name and not the target it points to. addFileToTar(tos, file.toPath(), relativize(base.toPath(), file.toPath())); } } return tarFile; }
Example #4
Source File: DebianPackageWriter.java From neoscada with Eclipse Public License 1.0 | 6 votes |
private void buildAndAddControlFile () throws IOException, FileNotFoundException { final File controlFile = File.createTempFile ( "control", null ); try { try ( GZIPOutputStream gout = new GZIPOutputStream ( new FileOutputStream ( controlFile ) ); TarArchiveOutputStream tout = new TarArchiveOutputStream ( gout ) ) { tout.setLongFileMode ( TarArchiveOutputStream.LONGFILE_GNU ); addControlContent ( tout, "control", createControlContent (), -1 ); addControlContent ( tout, "md5sums", createChecksumContent (), -1 ); addControlContent ( tout, "conffiles", createConfFilesContent (), -1 ); addControlContent ( tout, "preinst", this.preinstScript, EntryInformation.DEFAULT_FILE_EXEC.getMode () ); addControlContent ( tout, "prerm", this.prermScript, EntryInformation.DEFAULT_FILE_EXEC.getMode () ); addControlContent ( tout, "postinst", this.postinstScript, EntryInformation.DEFAULT_FILE_EXEC.getMode () ); addControlContent ( tout, "postrm", this.postrmScript, EntryInformation.DEFAULT_FILE_EXEC.getMode () ); } addArFile ( controlFile, "control.tar.gz" ); } finally { controlFile.delete (); } }
Example #5
Source File: DebianPackageWriter.java From neoscada with Eclipse Public License 1.0 | 6 votes |
public DebianPackageWriter ( final OutputStream stream, final GenericControlFile packageControlFile, final TimestampProvider timestampProvider ) throws IOException { this.packageControlFile = packageControlFile; this.timestampProvider = timestampProvider; if ( getTimestampProvider () == null ) { throw new IllegalArgumentException ( "'timestampProvider' must not be null" ); } BinaryPackageControlFile.validate ( packageControlFile ); this.ar = new ArArchiveOutputStream ( stream ); this.ar.putArchiveEntry ( new ArArchiveEntry ( "debian-binary", this.binaryHeader.length, 0, 0, AR_ARCHIVE_DEFAULT_MODE, getTimestampProvider ().getModTime () / 1000 ) ); this.ar.write ( this.binaryHeader ); this.ar.closeArchiveEntry (); this.dataTemp = File.createTempFile ( "data", null ); this.dataStream = new TarArchiveOutputStream ( new GZIPOutputStream ( new FileOutputStream ( this.dataTemp ) ) ); this.dataStream.setLongFileMode ( TarArchiveOutputStream.LONGFILE_GNU ); }
Example #6
Source File: TarUtils.java From che with Eclipse Public License 2.0 | 6 votes |
private static void addFileEntry( TarArchiveOutputStream tarOut, String entryName, File file, long modTime) throws IOException { final TarArchiveEntry tarEntry = new TarArchiveEntry(file, entryName); if (modTime >= 0) { tarEntry.setModTime(modTime); } tarOut.putArchiveEntry(tarEntry); try (InputStream in = new BufferedInputStream(new FileInputStream(file))) { final byte[] buf = new byte[BUF_SIZE]; int r; while ((r = in.read(buf)) != -1) { tarOut.write(buf, 0, r); } } tarOut.closeArchiveEntry(); }
Example #7
Source File: DefaultJavaLibraryIntegrationTest.java From buck with Apache License 2.0 | 6 votes |
/** * writeTarZst writes a .tar.zst file to 'file'. * * <p>For each key:value in archiveContents, a file named 'key' with contents 'value' will be * created in the archive. File names ending with "/" are considered directories. */ private void writeTarZst(Path file, Map<String, byte[]> archiveContents) throws IOException { try (OutputStream o = new BufferedOutputStream(Files.newOutputStream(file)); OutputStream z = new ZstdCompressorOutputStream(o); TarArchiveOutputStream archive = new TarArchiveOutputStream(z)) { archive.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX); for (Entry<String, byte[]> mapEntry : archiveContents.entrySet()) { String fileName = mapEntry.getKey(); byte[] fileContents = mapEntry.getValue(); boolean isRegularFile = !fileName.endsWith("/"); TarArchiveEntry e = new TarArchiveEntry(fileName); if (isRegularFile) { e.setSize(fileContents.length); archive.putArchiveEntry(e); archive.write(fileContents); } else { archive.putArchiveEntry(e); } archive.closeArchiveEntry(); } archive.finish(); } }
Example #8
Source File: FlowFilePackagerV1.java From localization_nifi with Apache License 2.0 | 6 votes |
private void writeContentEntry(final TarArchiveOutputStream tarOut, final InputStream inStream, final long fileSize) throws IOException { final TarArchiveEntry entry = new TarArchiveEntry(FILENAME_CONTENT); entry.setMode(tarPermissions); entry.setSize(fileSize); tarOut.putArchiveEntry(entry); final byte[] buffer = new byte[512 << 10];//512KB int bytesRead = 0; while ((bytesRead = inStream.read(buffer)) != -1) { //still more data to read if (bytesRead > 0) { tarOut.write(buffer, 0, bytesRead); } } copy(inStream, tarOut); tarOut.closeArchiveEntry(); }
Example #9
Source File: FlowFilePackagerV1.java From localization_nifi with Apache License 2.0 | 6 votes |
private void writeAttributesEntry(final Map<String, String> attributes, final TarArchiveOutputStream tout) throws IOException { final StringBuilder sb = new StringBuilder(); sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE properties\n SYSTEM \"http://java.sun.com/dtd/properties.dtd\">\n"); sb.append("<properties>"); for (final Map.Entry<String, String> entry : attributes.entrySet()) { final String escapedKey = StringEscapeUtils.escapeXml11(entry.getKey()); final String escapedValue = StringEscapeUtils.escapeXml11(entry.getValue()); sb.append("\n <entry key=\"").append(escapedKey).append("\">").append(escapedValue).append("</entry>"); } sb.append("</properties>"); final byte[] metaBytes = sb.toString().getBytes(StandardCharsets.UTF_8); final TarArchiveEntry attribEntry = new TarArchiveEntry(FILENAME_ATTRIBUTES); attribEntry.setMode(tarPermissions); attribEntry.setSize(metaBytes.length); tout.putArchiveEntry(attribEntry); tout.write(metaBytes); tout.closeArchiveEntry(); }
Example #10
Source File: DefaultArchiveExtractorTest.java From flow with Apache License 2.0 | 6 votes |
@Test(expected = ArchiveExtractionException.class) public void extractTarAsZip_ArchiveExtractionExceptionIsThrown() throws IOException, ArchiveExtractionException{ File archiveFile = new File(baseDir, "archive.zip"); archiveFile.createNewFile(); Path tempArchive = archiveFile.toPath(); try (OutputStream fo = Files.newOutputStream( tempArchive); OutputStream gzo = new GzipCompressorOutputStream( fo); ArchiveOutputStream o = new TarArchiveOutputStream(gzo)) { o.putArchiveEntry( o.createArchiveEntry(new File(ROOT_FILE), ROOT_FILE)); o.closeArchiveEntry(); } new DefaultArchiveExtractor().extract(archiveFile, targetDir); }
Example #11
Source File: CompressBackupUtil.java From xodus with Apache License 2.0 | 6 votes |
/** * Compresses the content of source and stores newly created archive in dest. * In case source is a directory, it will be compressed recursively. * * @param source file or folder to be archived. Should exist on method call. * @param dest path to the archive to be created. Should not exist on method call. * @throws IOException in case of any issues with underlying store. * @throws FileNotFoundException in case source does not exist. */ public static void tar(@NotNull File source, @NotNull File dest) throws IOException { if (!source.exists()) { throw new IllegalArgumentException("No source file or folder exists: " + source.getAbsolutePath()); } if (dest.exists()) { throw new IllegalArgumentException("Destination refers to existing file or folder: " + dest.getAbsolutePath()); } try (TarArchiveOutputStream tarOut = new TarArchiveOutputStream(new GZIPOutputStream( new BufferedOutputStream(new FileOutputStream(dest)), 0x1000))) { doTar("", source, tarOut); } catch (IOException e) { IOUtil.deleteFile(dest); // operation filed, let's remove the destination archive throw e; } }
Example #12
Source File: Tar.java From writelatex-git-bridge with MIT License | 6 votes |
private static void addTarDir( TarArchiveOutputStream tout, Path base, File dir ) throws IOException { Preconditions.checkArgument(dir.isDirectory()); String name = base.relativize( Paths.get(dir.getAbsolutePath()) ).toString(); ArchiveEntry entry = tout.createArchiveEntry(dir, name); tout.putArchiveEntry(entry); tout.closeArchiveEntry(); for (File f : dir.listFiles()) { addTarEntry(tout, base, f); } }
Example #13
Source File: TarGzCompressionUtils.java From incubator-pinot with Apache License 2.0 | 6 votes |
/** * Creates a tar entry for the path specified with a name built from the base * passed in and the file/directory name. If the path is a directory, a * recursive call is made such that the full directory is added to the tar. * * @param tOut * The tar file's output stream * @param path * The filesystem path of the file/directory being added * @param base * The base prefix to for the name of the tar file entry * * @throws IOException * If anything goes wrong */ private static void addFileToTarGz(TarArchiveOutputStream tOut, String path, String base) throws IOException { File f = new File(path); String entryName = base + f.getName(); TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName); tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); tOut.putArchiveEntry(tarEntry); if (f.isFile()) { IOUtils.copy(new FileInputStream(f), tOut); tOut.closeArchiveEntry(); } else { tOut.closeArchiveEntry(); File[] children = f.listFiles(); if (children != null) { for (File child : children) { addFileToTarGz(tOut, child.getAbsolutePath(), entryName + "/"); } } } }
Example #14
Source File: CompressionTools.java From aws-codepipeline-plugin-for-jenkins with Apache License 2.0 | 6 votes |
public static void compressTarFile( final File temporaryTarFile, final Path pathToCompress, final BuildListener listener) throws IOException { try (final TarArchiveOutputStream tarArchiveOutputStream = new TarArchiveOutputStream( new BufferedOutputStream( new FileOutputStream(temporaryTarFile)))) { compressArchive( pathToCompress, tarArchiveOutputStream, new ArchiveEntryFactory(CompressionType.Tar), CompressionType.Tar, listener); } }
Example #15
Source File: ProjectGenerator.java From syndesis with Apache License 2.0 | 6 votes |
private void addExtensions(TarArchiveOutputStream tos, Integration integration) throws IOException { final Set<String> extensions = resourceManager.collectDependencies(integration).stream() .filter(d-> d.isExtension() || d.isExtensionTag()) .map(Dependency::getId) .collect(Collectors.toCollection(TreeSet::new)); if (!extensions.isEmpty()) { addTarEntry(tos, "src/main/resources/loader.properties", generateExtensionLoader(extensions)); for (String extensionId : extensions) { try (InputStream is = resourceManager.loadExtensionBLOB(extensionId).orElseThrow( () -> new IllegalStateException("No extension blob for extension with id:" + extensionId) )) { addTarEntry( tos, "extensions/" + Names.sanitize(extensionId) + ".jar", IOUtils.toByteArray(is) ); } } } }
Example #16
Source File: Tar.java From writelatex-git-bridge with MIT License | 6 votes |
private static void addTarFile( TarArchiveOutputStream tout, Path base, File file ) throws IOException { Preconditions.checkArgument( file.isFile(), "given file" + " is not file: %s", file); checkFileSize(file.length()); String name = base.relativize( Paths.get(file.getAbsolutePath()) ).toString(); ArchiveEntry entry = tout.createArchiveEntry(file, name); tout.putArchiveEntry(entry); try (InputStream in = new FileInputStream(file)) { IOUtils.copy(in, tout); } tout.closeArchiveEntry(); }
Example #17
Source File: ExpProc.java From Export with Apache License 2.0 | 6 votes |
private void addArchive(File file, TarArchiveOutputStream aos, String basepath) throws Exception { if (file.exists()) { TarArchiveEntry entry = new TarArchiveEntry(basepath + "/" + file.getName()); entry.setSize(file.length()); aos.putArchiveEntry(entry); BufferedInputStream bis = new BufferedInputStream( new FileInputStream(file)); int count; byte data[] = new byte[1024]; while ((count = bis.read(data, 0, data.length)) != -1) { aos.write(data, 0, count); } bis.close(); aos.closeArchiveEntry(); } }
Example #18
Source File: FlowFilePackagerV1.java From nifi with Apache License 2.0 | 6 votes |
private void writeContentEntry(final TarArchiveOutputStream tarOut, final InputStream inStream, final long fileSize) throws IOException { final TarArchiveEntry entry = new TarArchiveEntry(FILENAME_CONTENT); entry.setMode(tarPermissions); entry.setSize(fileSize); tarOut.putArchiveEntry(entry); final byte[] buffer = new byte[512 << 10];//512KB int bytesRead = 0; while ((bytesRead = inStream.read(buffer)) != -1) { //still more data to read if (bytesRead > 0) { tarOut.write(buffer, 0, bytesRead); } } copy(inStream, tarOut); tarOut.closeArchiveEntry(); }
Example #19
Source File: TarGzipPacker.java From twister2 with Apache License 2.0 | 6 votes |
/** * create TarGzipPacker object */ public static TarGzipPacker createTarGzipPacker(String targetDir, Config config) { // this should be received from config String archiveFilename = SchedulerContext.jobPackageFileName(config); Path archiveFile = Paths.get(targetDir + "/" + archiveFilename); try { // construct output stream OutputStream outStream = Files.newOutputStream(archiveFile); GzipCompressorOutputStream gzipOutputStream = new GzipCompressorOutputStream(outStream); TarArchiveOutputStream tarOutputStream = new TarArchiveOutputStream(gzipOutputStream); return new TarGzipPacker(archiveFile, tarOutputStream); } catch (IOException ioe) { LOG.log(Level.SEVERE, "Archive file can not be created: " + archiveFile, ioe); return null; } }
Example #20
Source File: CompressedXmiWriter.java From argument-reasoning-comprehension-task with Apache License 2.0 | 6 votes |
@Override public void initialize(UimaContext aContext) throws ResourceInitializationException { super.initialize(aContext); // some param check if (!outputFile.getName().endsWith(".tar.gz")) { throw new ResourceInitializationException( new IllegalArgumentException("Output file must have .tar.gz extension")); } typeSystemWritten = false; try { outputStream = new TarArchiveOutputStream(new GzipCompressorOutputStream( new BufferedOutputStream(new FileOutputStream(outputFile)))); } catch (IOException ex) { throw new ResourceInitializationException(ex); } }
Example #21
Source File: DebianPackageWriter.java From packagedrone with Eclipse Public License 1.0 | 6 votes |
public DebianPackageWriter ( final OutputStream stream, final BinaryPackageControlFile packageControlFile, final Supplier<Instant> timestampSupplier ) throws IOException { Objects.requireNonNull ( timestampSupplier ); this.timestampSupplier = timestampSupplier; this.packageControlFile = packageControlFile; BinaryPackageControlFile.validate ( packageControlFile ); this.ar = new ArArchiveOutputStream ( stream ); this.ar.putArchiveEntry ( new ArArchiveEntry ( "debian-binary", this.binaryHeader.length, 0, 0, AR_ARCHIVE_DEFAULT_MODE, timestampSupplier.get ().getEpochSecond () ) ); this.ar.write ( this.binaryHeader ); this.ar.closeArchiveEntry (); this.dataTemp = File.createTempFile ( "data", null ); this.dataStream = new TarArchiveOutputStream ( new GZIPOutputStream ( new FileOutputStream ( this.dataTemp ) ) ); this.dataStream.setLongFileMode ( TarArchiveOutputStream.LONGFILE_GNU ); }
Example #22
Source File: TestDirectoryStructureUtil.java From tracecompass with Eclipse Public License 2.0 | 6 votes |
private static void addToArchive(TarArchiveOutputStream taos, File dir, File root) throws IOException { byte[] buffer = new byte[1024]; int length; 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(); } } } }
Example #23
Source File: ImageArchiveUtilTest.java From docker-maven-plugin with Apache License 2.0 | 6 votes |
@Test public void readInvalidJsonInArchive() throws IOException { byte[] archiveBytes; try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); TarArchiveOutputStream tarOutput = new TarArchiveOutputStream(baos)) { final byte[] entryData = ("}" + UUID.randomUUID().toString() + "{").getBytes(); TarArchiveEntry tarEntry = new TarArchiveEntry("not-the-" + ImageArchiveUtil.MANIFEST_JSON); tarEntry.setSize(entryData.length); tarOutput.putArchiveEntry(tarEntry); tarOutput.write(entryData); tarOutput.closeArchiveEntry(); tarOutput.finish(); archiveBytes = baos.toByteArray(); } ImageArchiveManifest manifest = ImageArchiveUtil.readManifest(new ByteArrayInputStream(archiveBytes)); Assert.assertNull(manifest); }
Example #24
Source File: EnvironmentTestsBase.java From xodus with Apache License 2.0 | 6 votes |
public static void archiveDB(final String location, final String target) { try { System.out.println("Dumping " + location + " to " + target); final File root = new File(location); final File targetFile = new File(target); TarArchiveOutputStream tarGz = new TarArchiveOutputStream(new GZIPOutputStream( new BufferedOutputStream(new FileOutputStream(targetFile)), 0x1000)); for (final File file : IOUtil.listFiles(root)) { final long fileSize = file.length(); if (file.isFile() && fileSize != 0) { CompressBackupUtil.archiveFile(tarGz, new BackupStrategy.FileDescriptor(file, ""), fileSize); } } tarGz.close(); } catch (IOException ioe) { System.out.println("Can't create backup"); } }
Example #25
Source File: StreamUtils.java From incubator-gobblin with Apache License 2.0 | 6 votes |
/** * Helper method for {@link #tar(FileSystem, FileSystem, Path, Path)} that recursively adds a directory to a given * {@link TarArchiveOutputStream}. */ private static void dirToTarArchiveOutputStreamRecursive(FileStatus dirFileStatus, FileSystem fs, Optional<Path> destDir, TarArchiveOutputStream tarArchiveOutputStream) throws IOException { Preconditions.checkState(fs.isDirectory(dirFileStatus.getPath())); Path dir = destDir.isPresent() ? new Path(destDir.get(), dirFileStatus.getPath().getName()) : new Path(dirFileStatus.getPath().getName()); dirToTarArchiveOutputStream(dir, tarArchiveOutputStream); for (FileStatus childFileStatus : fs.listStatus(dirFileStatus.getPath())) { Path childFile = new Path(dir, childFileStatus.getPath().getName()); if (fs.isDirectory(childFileStatus.getPath())) { dirToTarArchiveOutputStreamRecursive(childFileStatus, fs, Optional.of(childFile), tarArchiveOutputStream); } else { try (FSDataInputStream fsDataInputStream = fs.open(childFileStatus.getPath())) { fileToTarArchiveOutputStream(childFileStatus, fsDataInputStream, childFile, tarArchiveOutputStream); } } } }
Example #26
Source File: TarUtils.java From Xndroid with GNU General Public License v3.0 | 6 votes |
/** * 目录归档 * * @param dir * @param taos * TarArchiveOutputStream * @param basePath * @throws Exception */ private static void archiveDir(File dir, TarArchiveOutputStream taos, String basePath) throws Exception { File[] files = dir.listFiles(); if (files.length < 1) { TarArchiveEntry entry = new TarArchiveEntry(basePath + dir.getName() + PATH); taos.putArchiveEntry(entry); taos.closeArchiveEntry(); } for (File file : files) { // 递归归档 archive(file, taos, basePath + dir.getName() + PATH); } }
Example #27
Source File: TarDirectoryStreamTest.java From CloverETL-Engine with GNU Lesser General Public License v2.1 | 6 votes |
@Override protected byte[] prepareData() throws IOException { try ( ByteArrayOutputStream os = new ByteArrayOutputStream(); TarArchiveOutputStream tos = new TarArchiveOutputStream(os); ) { byte[] bytes = "hello world".getBytes(); TarArchiveEntry entry = new TarArchiveEntry("file.txt"); entry.setSize(bytes.length); tos.putArchiveEntry(entry); tos.write(bytes); tos.closeArchiveEntry(); tos.close(); return os.toByteArray(); } }
Example #28
Source File: CompressArchiveUtil.java From docker-java with Apache License 2.0 | 6 votes |
public static File archiveTARFiles(File base, Iterable<File> files, String archiveNameWithOutExtension) throws IOException { File tarFile = new File(FileUtils.getTempDirectoryPath(), archiveNameWithOutExtension + ".tar"); TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(tarFile)); try { tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); for (File file : files) { TarArchiveEntry tarEntry = new TarArchiveEntry(file); tarEntry.setName(relativize(base, file)); tos.putArchiveEntry(tarEntry); if (!file.isDirectory()) { FileUtils.copyFile(file, tos); } tos.closeArchiveEntry(); } } finally { tos.close(); } return tarFile; }
Example #29
Source File: FlowFilePackagerV1.java From nifi with Apache License 2.0 | 6 votes |
private void writeAttributesEntry(final Map<String, String> attributes, final TarArchiveOutputStream tout) throws IOException { final StringBuilder sb = new StringBuilder(); sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE properties\n SYSTEM \"http://java.sun.com/dtd/properties.dtd\">\n"); sb.append("<properties>"); for (final Map.Entry<String, String> entry : attributes.entrySet()) { final String escapedKey = StringEscapeUtils.escapeXml11(entry.getKey()); final String escapedValue = StringEscapeUtils.escapeXml11(entry.getValue()); sb.append("\n <entry key=\"").append(escapedKey).append("\">").append(escapedValue).append("</entry>"); } sb.append("</properties>"); final byte[] metaBytes = sb.toString().getBytes(StandardCharsets.UTF_8); final TarArchiveEntry attribEntry = new TarArchiveEntry(FILENAME_ATTRIBUTES); attribEntry.setMode(tarPermissions); attribEntry.setSize(metaBytes.length); tout.putArchiveEntry(attribEntry); tout.write(metaBytes); tout.closeArchiveEntry(); }
Example #30
Source File: CompressedDirectory.java From docker-client with Apache License 2.0 | 5 votes |
/** * This method creates a gzip tarball of the specified directory. File permissions will be * retained. The file will be created in a temporary directory using the {@link * Files#createTempFile(String, String, java.nio.file.attribute.FileAttribute[])} method. The * returned object is auto-closeable, and upon closing it, the archive file will be deleted. * * @param directory the directory to compress * @return a Path object representing the compressed directory * @throws IOException if the compressed directory could not be created. */ public static CompressedDirectory create(final Path directory) throws IOException { final Path file = Files.createTempFile("docker-client-", ".tar.gz"); final Path dockerIgnorePath = directory.resolve(".dockerignore"); final ImmutableList<DockerIgnorePathMatcher> ignoreMatchers = parseDockerIgnore(dockerIgnorePath); try (final OutputStream fileOut = Files.newOutputStream(file); final GzipCompressorOutputStream gzipOut = new GzipCompressorOutputStream(fileOut); final TarArchiveOutputStream tarOut = new TarArchiveOutputStream(gzipOut)) { tarOut.setLongFileMode(LONGFILE_POSIX); tarOut.setBigNumberMode(BIGNUMBER_POSIX); Files.walkFileTree(directory, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new Visitor(directory, ignoreMatchers, tarOut)); } catch (Throwable t) { // If an error occurs, delete temporary file before rethrowing exclude. try { Files.delete(file); } catch (IOException e) { // So we don't lose track of the reason the file was deleted... might be important t.addSuppressed(e); } throw t; } return new CompressedDirectory(file); }