Java Code Examples for org.apache.commons.compress.archivers.tar.TarArchiveEntry#setSize()

The following examples show how to use org.apache.commons.compress.archivers.tar.TarArchiveEntry#setSize() . 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: ExpProc.java    From Export with Apache License 2.0 6 votes vote down vote up
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 2
Source File: ImageArchiveUtilTest.java    From docker-maven-plugin with Apache License 2.0 6 votes vote down vote up
@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 3
Source File: TarGzipPacker.java    From twister2 with Apache License 2.0 6 votes vote down vote up
/**
 * add one file to tar.gz file
 * file is created from the given byte array
 *
 * @param filename file to be added to the tar.gz
 */
public boolean addFileToArchive(String filename, byte[] contents) {

  String filePathInTar = JOB_ARCHIVE_DIRECTORY + "/" + filename;
  try {
    TarArchiveEntry entry = new TarArchiveEntry(filePathInTar);
    entry.setSize(contents.length);
    tarOutputStream.putArchiveEntry(entry);
    IOUtils.copy(new ByteArrayInputStream(contents), tarOutputStream);
    tarOutputStream.closeArchiveEntry();

    return true;
  } catch (IOException e) {
    LOG.log(Level.SEVERE, "File can not be added: " + filePathInTar, e);
    return false;
  }
}
 
Example 4
Source File: TarGzipPacker.java    From twister2 with Apache License 2.0 6 votes vote down vote up
/**
 * add one file to tar.gz file
 *
 * @param file file to be added to the tar.gz
 */
public boolean addFileToArchive(File file, String dirPrefixForTar) {
  try {
    String filePathInTar = dirPrefixForTar + file.getName();

    TarArchiveEntry entry = new TarArchiveEntry(file, filePathInTar);
    entry.setSize(file.length());
    tarOutputStream.putArchiveEntry(entry);
    IOUtils.copy(new FileInputStream(file), tarOutputStream);
    tarOutputStream.closeArchiveEntry();

    return true;
  } catch (IOException e) {
    LOG.log(Level.SEVERE, "File can not be added: " + file.getName(), e);
    return false;
  }
}
 
Example 5
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 6
Source File: CompressedDirectory.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {

  final Path relativePath = root.relativize(file);

  if (exclude(ignoreMatchers, relativePath)) {
    return FileVisitResult.CONTINUE;
  }

  final TarArchiveEntry entry = new TarArchiveEntry(file.toFile());
  entry.setName(relativePath.toString());
  entry.setMode(getFileMode(file));
  entry.setSize(attrs.size());
  tarStream.putArchiveEntry(entry);
  Files.copy(file, tarStream);
  tarStream.closeArchiveEntry();
  return FileVisitResult.CONTINUE;
}
 
Example 7
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 8
Source File: NPMPackageGenerator.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
public void loadFiles(String root, File dir, String... noload) throws IOException {
  for (File f : dir.listFiles()) {
    if (!Utilities.existsInList(f.getName(), noload)) {
      if (f.isDirectory()) {
        loadFiles(root, f);
      } else {
        String path = f.getAbsolutePath().substring(root.length()+1);
        byte[] content = TextFile.fileToBytes(f);
        if (created.contains(path)) 
          System.out.println("Duplicate package file "+path);
        else {
          created.add(path);
          TarArchiveEntry entry = new TarArchiveEntry(path);
          entry.setSize(content.length);
          tar.putArchiveEntry(entry);
          tar.write(content);
          tar.closeArchiveEntry();
        }
      }
    }
  }
}
 
Example 9
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 10
Source File: NPMPackageGenerator.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
public void loadFiles(String root, File dir, String... noload) throws IOException {
  for (File f : dir.listFiles()) {
    if (!Utilities.existsInList(f.getName(), noload)) {
      if (f.isDirectory()) {
        loadFiles(root, f);
      } else {
        String path = f.getAbsolutePath().substring(root.length()+1);
        byte[] content = TextFile.fileToBytes(f);
        if (created.contains(path)) 
          System.out.println("Duplicate package file "+path);
        else {
          created.add(path);
          TarArchiveEntry entry = new TarArchiveEntry(path);
          entry.setSize(content.length);
          tar.putArchiveEntry(entry);
          tar.write(content);
          tar.closeArchiveEntry();
        }
      }
    }
  }
}
 
Example 11
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 12
Source File: Packaging.java    From dekorate with Apache License 2.0 5 votes vote down vote up
public static void putTarEntry(TarArchiveOutputStream tarArchiveOutputStream, TarArchiveEntry tarArchiveEntry,
                               Path inputPath) throws IOException {
  tarArchiveEntry.setSize(Files.size(inputPath));
  tarArchiveOutputStream.putArchiveEntry(tarArchiveEntry);
  Files.copy(inputPath, tarArchiveOutputStream);
  tarArchiveOutputStream.closeArchiveEntry();
}
 
Example 13
Source File: ArchiveWorkItemHandler.java    From jbpm-work-items with Apache License 2.0 5 votes vote down vote up
public void executeWorkItem(WorkItem workItem,
                            WorkItemManager manager) {
    String archive = (String) workItem.getParameter("Archive");
    List<File> files = (List<File>) workItem.getParameter("Files");

    try {
        RequiredParameterValidator.validate(this.getClass(),
                                            workItem);

        OutputStream outputStream = new FileOutputStream(new File(archive));
        ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream("tar",
                                                                                      outputStream);

        if (files != null) {
            for (File file : files) {
                final TarArchiveEntry entry = new TarArchiveEntry("testdata/test1.xml");
                entry.setModTime(0);
                entry.setSize(file.length());
                entry.setUserId(0);
                entry.setGroupId(0);
                entry.setMode(0100000);
                os.putArchiveEntry(entry);
                IOUtils.copy(new FileInputStream(file),
                             os);
            }
        }
        os.closeArchiveEntry();
        os.close();
        manager.completeWorkItem(workItem.getId(),
                                 null);
    } catch (Exception e) {
        handleException(e);
        manager.abortWorkItem(workItem.getId());
    }
}
 
Example 14
Source File: NPMPackageGenerator.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
public void addFile(Category cat, String name, byte[] content) throws IOException {
  String path = cat.getDirectory()+name;
  if (!path.startsWith("package/")) {
    path = "package/" +path;
  }
  if (path.length() > 100) {
    name = name.substring(0, name.indexOf("-"))+"-"+UUID.randomUUID().toString();
    path = cat.getDirectory()+name;
    if (!path.startsWith("package/")) {
      path = "package/" +path;
    }
    
  }
    
  if (created.contains(path)) {
    System.out.println("Duplicate package file "+path);
  } else {
    created.add(path);
    TarArchiveEntry entry = new TarArchiveEntry(path);
    entry.setSize(content.length);
    tar.putArchiveEntry(entry);
    tar.write(content);
    tar.closeArchiveEntry();
    if(cat == Category.RESOURCE) {
      indexer.seeFile(name, content);
    }
  }
}
 
Example 15
Source File: DockerComputerSSHConnector.java    From docker-plugin with MIT License 5 votes vote down vote up
@Override
public void beforeContainerStarted(DockerAPI api, String workdir, String containerId) throws IOException, InterruptedException {
    final String key = sshKeyStrategy.getInjectedKey();
    if (key != null) {
        final String authorizedKeysCommand = "#!/bin/sh\n"
                + "[ \"$1\" = \"" + sshKeyStrategy.getUser() + "\" ] "
                + "&& echo '" + key + "'"
                + "|| :";
        final byte[] authorizedKeysCommandAsBytes = authorizedKeysCommand.getBytes(StandardCharsets.UTF_8);
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
             TarArchiveOutputStream tar = new TarArchiveOutputStream(bos)) {
            TarArchiveEntry entry = new TarArchiveEntry("authorized_key");
            entry.setSize(authorizedKeysCommandAsBytes.length);
            entry.setMode(0700);
            tar.putArchiveEntry(entry);
            tar.write(authorizedKeysCommandAsBytes);
            tar.closeArchiveEntry();
            tar.close();
            try (InputStream is = new ByteArrayInputStream(bos.toByteArray());
                 DockerClient client = api.getClient()) {
                client.copyArchiveToContainerCmd(containerId)
                        .withTarInputStream(is)
                        .withRemotePath("/root")
                        .exec();
            }
        }
    }
}
 
Example 16
Source File: AbstractBaseArchiver.java    From cantor with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
static void writeArchiveEntry(final ArchiveOutputStream archive, final String name, final byte[] bytes) throws IOException {
    final TarArchiveEntry entry = new TarArchiveEntry(name);
    entry.setSize(bytes.length);
    archive.putArchiveEntry(entry);
    archive.write(bytes);
    archive.closeArchiveEntry();
}
 
Example 17
Source File: GzipCborFileWriter.java    From ache with Apache License 2.0 5 votes vote down vote up
public synchronized void writeTargetModel(TargetModelCbor target) throws IOException {

        byte[] byteData = mapper.writeValueAsBytes(target);
        
        TarArchiveEntry tarEntry = new TarArchiveEntry(target.key);
        tarEntry.setSize(byteData.length);
        
        tarOutput.putArchiveEntry(tarEntry);
        tarOutput.write(byteData);
        tarOutput.closeArchiveEntry();
    }
 
Example 18
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 19
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 20
Source File: ArtifactUploader.java    From buck with Apache License 2.0 5 votes vote down vote up
/** Archive and compress 'pathsToIncludeInArchive' into 'out', using tar+zstandard. */
@VisibleForTesting
static long compress(
    ProjectFilesystem projectFilesystem, Collection<Path> pathsToIncludeInArchive, Path out)
    throws IOException {
  long fullSize = 0L;
  try (OutputStream o = new BufferedOutputStream(Files.newOutputStream(out));
      OutputStream z = new ZstdCompressorOutputStream(o);
      TarArchiveOutputStream archive = new TarArchiveOutputStream(z)) {
    archive.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
    for (Path path : pathsToIncludeInArchive) {
      boolean isRegularFile = !projectFilesystem.isDirectory(path);

      // Add a file entry.
      TarArchiveEntry e = new TarArchiveEntry(path.toString() + (isRegularFile ? "" : "/"));
      int mode = (int) projectFilesystem.getPosixFileMode(path);
      // If permissions don't allow for owner to r or w, update to u+=rw and g+=r
      e.setMode((mode & 384) == 0 ? (mode | 416) : mode);
      e.setModTime((long) ObjectFileCommonModificationDate.COMMON_MODIFICATION_TIME_STAMP * 1000);

      if (isRegularFile) {
        long pathSize = projectFilesystem.getFileSize(path);
        e.setSize(pathSize);
        fullSize += pathSize;
        archive.putArchiveEntry(e);
        try (InputStream input = projectFilesystem.newFileInputStream(path)) {
          ByteStreams.copy(input, archive);
        }
      } else {
        archive.putArchiveEntry(e);
      }
      archive.closeArchiveEntry();
    }
    archive.finish();
  }

  return fullSize;
}