Java Code Examples for org.apache.commons.compress.archivers.tar.TarArchiveOutputStream#putArchiveEntry()

The following examples show how to use org.apache.commons.compress.archivers.tar.TarArchiveOutputStream#putArchiveEntry() . 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 7 votes vote down vote up
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: DebianPackageWriter.java    From packagedrone with Eclipse Public License 1.0 6 votes vote down vote up
private void addControlContent ( final TarArchiveOutputStream out, final String name, final ContentProvider content, final int mode, final Supplier<Instant> timestampSupplier ) throws IOException
{
    if ( content == null || !content.hasContent () )
    {
        return;
    }

    final TarArchiveEntry entry = new TarArchiveEntry ( name );
    if ( mode >= 0 )
    {
        entry.setMode ( mode );
    }

    entry.setUserName ( "root" );
    entry.setGroupName ( "root" );
    entry.setSize ( content.getSize () );
    entry.setModTime ( timestampSupplier.get ().toEpochMilli () );
    out.putArchiveEntry ( entry );
    try ( InputStream stream = content.createInputStream () )
    {
        IOUtils.copy ( stream, out );
    }
    out.closeArchiveEntry ();
}
 
Example 3
Source File: TestDirectoryStructureUtil.java    From tracecompass with Eclipse Public License 2.0 6 votes vote down vote up
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 4
Source File: FileHelper.java    From incubator-heron with Apache License 2.0 6 votes vote down vote up
private static void addFileToArchive(TarArchiveOutputStream archiveOutputStream, File file,
                                     String base) throws IOException {
  final File absoluteFile = file.getAbsoluteFile();
  final String entryName = base + file.getName();
  final TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(file, entryName);
  archiveOutputStream.putArchiveEntry(tarArchiveEntry);

  if (absoluteFile.isFile()) {
    Files.copy(file.toPath(), archiveOutputStream);
    archiveOutputStream.closeArchiveEntry();
  } else {
    archiveOutputStream.closeArchiveEntry();
    if (absoluteFile.listFiles() != null) {
      for (File f : absoluteFile.listFiles()) {
        addFileToArchive(archiveOutputStream, f, entryName + "/");
      }
    }
  }
}
 
Example 5
Source File: Tar.java    From writelatex-git-bridge with MIT License 6 votes vote down vote up
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 6
Source File: FlowFilePackagerV1.java    From nifi with Apache License 2.0 6 votes vote down vote up
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 7
Source File: ProjectGeneratorHelper.java    From syndesis with Apache License 2.0 5 votes vote down vote up
public static void addTarEntry(TarArchiveOutputStream tos, String path, byte[] content) throws IOException {
    TarArchiveEntry entry = new TarArchiveEntry(path);
    entry.setSize(content.length);
    tos.putArchiveEntry(entry);
    tos.write(content);
    tos.closeArchiveEntry();
}
 
Example 8
Source File: TestFSDownload.java    From hadoop with Apache License 2.0 5 votes vote down vote up
static LocalResource createTarFile(FileContext files, Path p, int len,
    Random r, LocalResourceVisibility vis) throws IOException,
    URISyntaxException {
  byte[] bytes = new byte[len];
  r.nextBytes(bytes);

  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(ConverterUtils.getYarnUrlFromPath(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;
}
 
Example 9
Source File: TestTarImportCommand.java    From kite with Apache License 2.0 5 votes vote down vote up
private static void writeToTarFile(TarArchiveOutputStream tos, String name,
                                   String content) throws IOException {
  TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(name);
  if (null != content) {
    tarArchiveEntry.setSize(content.length());
  }
  tarArchiveEntry.setModTime(System.currentTimeMillis());
  tos.putArchiveEntry(tarArchiveEntry);
  if (null != content) {
    byte[] buf = content.getBytes();
    tos.write(buf, 0, content.length());
    tos.flush();
  }
  tos.closeArchiveEntry();
}
 
Example 10
Source File: MountableFile.java    From testcontainers-java with MIT License 5 votes vote down vote up
private void recursiveTar(String entryFilename, String rootPath, String itemPath, TarArchiveOutputStream tarArchive) {
    try {
        final File sourceFile = new File(itemPath).getCanonicalFile();     // e.g. /foo/bar/baz
        final File sourceRootFile = new File(rootPath).getCanonicalFile();     // e.g. /foo
        final String relativePathToSourceFile = sourceRootFile.toPath().relativize(sourceFile.toPath()).toFile().toString();    // e.g. /bar/baz

        final String tarEntryFilename;
        if (relativePathToSourceFile.isEmpty()) {
            tarEntryFilename = entryFilename; // entry filename e.g. xyz => xyz
        } else {
            tarEntryFilename = entryFilename + "/" + relativePathToSourceFile; // entry filename e.g. /xyz/bar/baz => /foo/bar/baz
        }

        final TarArchiveEntry tarEntry = new TarArchiveEntry(sourceFile, tarEntryFilename.replaceAll("^/", ""));

        // TarArchiveEntry automatically sets the mode for file/directory, but we can update to ensure that the mode is set exactly (inc executable bits)
        tarEntry.setMode(getUnixFileMode(itemPath));
        tarArchive.putArchiveEntry(tarEntry);

        if (sourceFile.isFile()) {
            Files.copy(sourceFile.toPath(), tarArchive);
        }
        // a directory entry merely needs to exist in the TAR file - there is no data stored yet
        tarArchive.closeArchiveEntry();

        final File[] children = sourceFile.listFiles();
        if (children != null) {
            // recurse into child files/directories
            for (final File child : children) {
                recursiveTar(entryFilename, sourceRootFile.getCanonicalPath(), child.getCanonicalPath(), tarArchive);
            }
        }
    } catch (IOException e) {
        log.error("Error when copying TAR file entry: {}", itemPath, e);
        throw new UncheckedIOException(e); // fail fast
    }
}
 
Example 11
Source File: TarUtils.java    From Xndroid with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 数据归档
 *
 * @param data
 *            待归档数据
 * @param path
 *            归档数据的当前路径
 * @param name
 *            归档文件名
 * @param taos
 *            TarArchiveOutputStream
 * @throws Exception
 */
private static void archiveFile(File file, TarArchiveOutputStream taos,
                                String dir) throws Exception {

    /**
     * 归档内文件名定义
     *
     * <pre>
     * 如果有多级目录,那么这里就需要给出包含目录的文件名
     * 如果用WinRAR打开归档包,中文名将显示为乱码
     * </pre>
     */
    TarArchiveEntry entry = new TarArchiveEntry(dir + file.getName());

    entry.setSize(file.length());

    taos.putArchiveEntry(entry);

    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
            file));
    int count;
    byte data[] = new byte[BUFFER];
    while ((count = bis.read(data, 0, BUFFER)) != -1) {
        taos.write(data, 0, count);
    }

    bis.close();

    taos.closeArchiveEntry();
}
 
Example 12
Source File: MCRTarServlet.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void sendCompressedDirectory(MCRPath file, BasicFileAttributes attrs,
    TarArchiveOutputStream container) throws IOException {
    TarArchiveEntry entry = new TarArchiveEntry(getFilename(file), TarConstants.LF_DIR);
    entry.setModTime(attrs.lastModifiedTime().toMillis());
    container.putArchiveEntry(entry);
    container.closeArchiveEntry();
}
 
Example 13
Source File: MCRTarServlet.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void sendCompressedFile(MCRPath file, BasicFileAttributes attrs,
    TarArchiveOutputStream container) throws IOException {
    TarArchiveEntry entry = new TarArchiveEntry(getFilename(file));
    entry.setModTime(attrs.lastModifiedTime().toMillis());
    entry.setSize(attrs.size());
    container.putArchiveEntry(entry);
    try {
        Files.copy(file, container);
    } finally {
        container.closeArchiveEntry();
    }
}
 
Example 14
Source File: Gzip.java    From Export with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * ��һ���ļ��鵵
 * 
 * @param sources
 *            Ҫ�鵵��ԭ�ļ�����
 * @param target
 *            �鵵����ļ�
 * @return File ���ع鵵����ļ�
 * @throws Exception
 */
public static File tar(File[] sources, File target) throws Exception {
	FileOutputStream out = new FileOutputStream(target);
	TarArchiveOutputStream os = new TarArchiveOutputStream(out);
	for (File file : sources) {
		os.putArchiveEntry(new TarArchiveEntry(file));
		IOUtils.copy(new FileInputStream(file), os);
		os.closeArchiveEntry();
	}
	if (os != null) {
		os.flush();
		os.close();
	}
	return target;
}
 
Example 15
Source File: TestFSDownload.java    From big-c with Apache License 2.0 5 votes vote down vote up
static LocalResource createTarFile(FileContext files, Path p, int len,
    Random r, LocalResourceVisibility vis) throws IOException,
    URISyntaxException {
  byte[] bytes = new byte[len];
  r.nextBytes(bytes);

  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(ConverterUtils.getYarnUrlFromPath(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;
}
 
Example 16
Source File: REEFScheduler.java    From reef with Apache License 2.0 5 votes vote down vote up
private String getReefTarUri(final String jobIdentifier) {
  try {
    // 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);
  }
}
 
Example 17
Source File: BackupService.java    From halyard with Apache License 2.0 5 votes vote down vote up
private void addFileToTar(
    TarArchiveOutputStream tarArchiveOutputStream, String path, String base) {
  File file = new File(path);
  String fileName = file.getName();

  if (Arrays.stream(omitPaths).anyMatch(s -> s.equals(fileName))) {
    return;
  }

  String tarEntryName = String.join("/", base, fileName);
  try {
    if (file.isFile()) {
      TarArchiveEntry tarEntry = new TarArchiveEntry(file, tarEntryName);
      tarArchiveOutputStream.putArchiveEntry(tarEntry);
      IOUtils.copy(new FileInputStream(file), tarArchiveOutputStream);
      tarArchiveOutputStream.closeArchiveEntry();
    } else if (file.isDirectory()) {
      Arrays.stream(file.listFiles())
          .filter(Objects::nonNull)
          .forEach(f -> addFileToTar(tarArchiveOutputStream, f.getAbsolutePath(), tarEntryName));
    } else {
      log.warn("Unknown file type: " + file + " - skipping addition to tar archive");
    }
  } catch (IOException e) {
    throw new HalException(
        Problem.Severity.FATAL,
        "Unable to file "
            + file.getName()
            + " to archive entry: "
            + tarEntryName
            + " "
            + e.getMessage(),
        e);
  }
}
 
Example 18
Source File: ArchiveUtils.java    From support-diagnostics with Apache License 2.0 5 votes vote down vote up
public static void archiveResultsTar(String archiveFilename, TarArchiveOutputStream taos, File file, String path, boolean append) {
   String relPath = "";

   try {
      if (append) {
         relPath = path + "/" + file.getName() + "-" + archiveFilename;
      } else {
         relPath = path + "/" + file.getName();
      }
      TarArchiveEntry tae = new TarArchiveEntry(file, relPath);
      taos.putArchiveEntry(tae);

      if (file.isFile()) {
         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
         IOUtils.copy(bis, taos);
         taos.closeArchiveEntry();
         bis.close();

      } else if (file.isDirectory()) {
         taos.closeArchiveEntry();
         for (File childFile : file.listFiles()) {
            archiveResultsTar(archiveFilename, taos, childFile, relPath, false);
         }
      }
   } catch (IOException e) {
      logger.error(Constants.CONSOLE,"Archive Error", e);
   }
}
 
Example 19
Source File: StreamUtils.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method for {@link #tar(FileSystem, FileSystem, Path, Path)} that adds a file entry to a given
 * {@link TarArchiveOutputStream} and copies the contents of the file to the new entry.
 */
private static void fileToTarArchiveOutputStream(FileStatus fileStatus, FSDataInputStream fsDataInputStream,
    Path destFile, TarArchiveOutputStream tarArchiveOutputStream) throws IOException {
  TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(formatPathToFile(destFile));
  tarArchiveEntry.setSize(fileStatus.getLen());
  tarArchiveEntry.setModTime(System.currentTimeMillis());
  tarArchiveOutputStream.putArchiveEntry(tarArchiveEntry);

  try {
    IOUtils.copy(fsDataInputStream, tarArchiveOutputStream);
  } finally {
    tarArchiveOutputStream.closeArchiveEntry();
  }
}
 
Example 20
Source File: MCRTransferPackagePacker.java    From mycore with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Writes a file to a *.tar archive.
 *
 * @param tarOutStream the stream of the *.tar.
 * @param fileName the file name to write
 * @param data of the file
 *
 * @throws IOException some writing to the stream went wrong
 */
private void writeFile(TarArchiveOutputStream tarOutStream, String fileName, byte[] data) throws IOException {
    TarArchiveEntry tarEntry = new TarArchiveEntry(fileName);
    tarEntry.setSize(data.length);
    tarOutStream.putArchiveEntry(tarEntry);
    tarOutStream.write(data);
    tarOutStream.closeArchiveEntry();
}