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

The following examples show how to use org.apache.commons.compress.archivers.tar.TarArchiveOutputStream#close() . 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: EnvironmentTestsBase.java    From xodus with Apache License 2.0 6 votes vote down vote up
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 2
Source File: ArchiveUtils.java    From support-diagnostics with Apache License 2.0 6 votes vote down vote up
public static boolean createTarArchive(String dir, String archiveFileName) {

      try {
         File srcDir = new File(dir);
         String filename = dir + "-" + archiveFileName + ".tar.gz";

         FileOutputStream fout = new FileOutputStream(filename);
         CompressorOutputStream cout = new GzipCompressorOutputStream(fout);
         TarArchiveOutputStream taos = new TarArchiveOutputStream(cout);

         taos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR);
         taos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
         archiveResultsTar(archiveFileName, taos, srcDir, "", true);
         taos.close();

         logger.info(Constants.CONSOLE,  "Archive: " + filename + " was created");

      } catch (Exception ioe) {
         logger.error( "Couldn't create archive.", ioe);
         return false;
      }

      return true;

   }
 
Example 3
Source File: CompressArchiveUtil.java    From docker-java with Apache License 2.0 6 votes vote down vote up
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 4
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 5
Source File: TestFSDownload.java    From hadoop with Apache License 2.0 5 votes vote down vote up
static LocalResource createTgzFile(FileContext files, Path p, int len,
    Random r, LocalResourceVisibility vis) throws IOException,
    URISyntaxException {
  byte[] bytes = new byte[len];
  r.nextBytes(bytes);

  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(ConverterUtils.getYarnUrlFromPath(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;
}
 
Example 6
Source File: TarUtils.java    From Xndroid with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 归档
 *
 * @param srcFile
 *            源路径
 * @param destPath
 *            目标路径
 * @throws Exception
 */
public static void archive(File srcFile, File destFile) throws Exception {

    TarArchiveOutputStream taos = new TarArchiveOutputStream(
            new FileOutputStream(destFile));

    archive(srcFile, taos, BASE_DIR);

    taos.flush();
    taos.close();
}
 
Example 7
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 8
Source File: TestFSDownload.java    From big-c with Apache License 2.0 5 votes vote down vote up
static LocalResource createTgzFile(FileContext files, Path p, int len,
    Random r, LocalResourceVisibility vis) throws IOException,
    URISyntaxException {
  byte[] bytes = new byte[len];
  r.nextBytes(bytes);

  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(ConverterUtils.getYarnUrlFromPath(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;
}
 
Example 9
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 10
Source File: MountableFileTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
private TarArchiveInputStream intoTarArchive(Consumer<TarArchiveOutputStream> consumer) throws IOException {
    @Cleanup final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    @Cleanup final TarArchiveOutputStream taos = new TarArchiveOutputStream(baos);
    consumer.accept(taos);
    taos.close();

    return new TarArchiveInputStream(new ByteArrayInputStream(baos.toByteArray()));
}
 
Example 11
Source File: ExtractArchiveWorkspaceManagerTest.java    From digdag with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp()
        throws IOException
{
    extractArchiveWorkspaceManager = new ExtractArchiveWorkspaceManager(new TempFileManager(temporaryFolder.getRoot().toPath()));

    File digFile = temporaryFolder.newFile();

    String wf = "+task:\n" +
            "  echo>: hello\n" +
            "";

    Files.write(digFile.toPath(), wf.getBytes(), StandardOpenOption.CREATE);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream);
    TarArchiveOutputStream tarArchiveOutputStream = new TarArchiveOutputStream(gzipOutputStream);
    tarArchiveOutputStream.createArchiveEntry(digFile, "mydig.dig");
    tarArchiveOutputStream.close();

    archived = outputStream.toByteArray();

    taskRequest = mock(TaskRequest.class);
    when(taskRequest.getTaskName()).thenReturn("zzz");

    storageObject = mock(StorageObject.class);
    when(storageObject.getContentLength()).thenReturn((long) archived.length);
    when(storageObject.getContentInputStream()).thenReturn(new ByteArrayInputStream(archived));

    byte[] wrongArchive = "This isn't a proper archive content".getBytes();
    wrongStorageObject = mock(StorageObject.class);
    when(wrongStorageObject.getContentLength()).thenReturn((long) wrongArchive.length);
    when(wrongStorageObject.getContentInputStream()).thenReturn(new ByteArrayInputStream(wrongArchive));
}
 
Example 12
Source File: SourceBlobPackager.java    From heroku-maven-plugin with MIT License 5 votes vote down vote up
public static Path pack(SourceBlobDescriptor sourceBlobDescriptor, OutputAdapter outputAdapter) throws IOException {
    Path tarFilePath = Files.createTempFile("heroku-deploy", "source-blob.tgz");

    TarArchiveOutputStream tarArchiveOutputStream = new TarArchiveOutputStream(
            new GzipCompressorOutputStream(new FileOutputStream(tarFilePath.toFile())));

    tarArchiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

    outputAdapter.logInfo("-----> Packaging application...");
    for (Path sourceBlobPath : sourceBlobDescriptor.getContents().keySet()) {
        SourceBlobDescriptor.SourceBlobContent content = sourceBlobDescriptor.getContents().get(sourceBlobPath);

        if (content.isHidden()) {
            outputAdapter.logDebug("       - including: " + sourceBlobPath + " (hidden)");
        } else {
            outputAdapter.logInfo("       - including: " + sourceBlobPath);
        }

        addIncludedPathToArchive(sourceBlobPath.toString(), content, tarArchiveOutputStream);
    }

    tarArchiveOutputStream.close();

    outputAdapter.logInfo("-----> Creating build...");
    outputAdapter.logInfo("       - file: " + tarFilePath);
    outputAdapter.logInfo(String.format("       - size: %dMB", Files.size(tarFilePath) / 1024 / 1024));

    return tarFilePath;
}
 
Example 13
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 14
Source File: LargeTarTestCase.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
protected void createLargeFile(final String path, final String name) throws Exception {
    final long _1K = 1024;
    final long _1M = 1024 * _1K;
    // long _256M = 256 * _1M;
    // long _512M = 512 * _1M;
    final long _1G = 1024 * _1M;

    // File size of 3 GB
    final long fileSize = 3 * _1G;

    final File tarGzFile = new File(path + name + ".tar.gz");

    if (!tarGzFile.exists()) {
        System.out.println(
                "This test is a bit slow. It needs to write 3GB of data as a compressed file (approx. 3MB) to your hard drive");

        final PipedOutputStream outTarFileStream = new PipedOutputStream();
        final PipedInputStream inTarFileStream = new PipedInputStream(outTarFileStream);

        final Thread source = new Thread() {

            @Override
            public void run() {
                final byte ba_1k[] = new byte[(int) _1K];
                for (int i = 0; i < ba_1k.length; i++) {
                    ba_1k[i] = 'a';
                }
                try {
                    final TarArchiveOutputStream outTarStream = (TarArchiveOutputStream) new ArchiveStreamFactory()
                            .createArchiveOutputStream(ArchiveStreamFactory.TAR, outTarFileStream);
                    // Create archive contents
                    final TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(name + ".txt");
                    tarArchiveEntry.setSize(fileSize);

                    outTarStream.putArchiveEntry(tarArchiveEntry);
                    for (long i = 0; i < fileSize; i += ba_1k.length) {
                        outTarStream.write(ba_1k);
                    }
                    outTarStream.closeArchiveEntry();
                    outTarStream.close();
                    outTarFileStream.close();
                } catch (final Exception e) {
                    e.printStackTrace();
                }
            }

        };
        source.start();

        // Create compressed archive
        final OutputStream outGzipFileStream = new FileOutputStream(path + name + ".tar.gz");

        final GzipCompressorOutputStream outGzipStream = (GzipCompressorOutputStream) new CompressorStreamFactory()
                .createCompressorOutputStream(CompressorStreamFactory.GZIP, outGzipFileStream);

        IOUtils.copy(inTarFileStream, outGzipStream);
        inTarFileStream.close();

        outGzipStream.close();
        outGzipFileStream.close();

    }
}