Java Code Examples for org.apache.commons.compress.archivers.tar.TarArchiveInputStream#getNextTarEntry()

The following examples show how to use org.apache.commons.compress.archivers.tar.TarArchiveInputStream#getNextTarEntry() . 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: TarfileTest.java    From camel-quarkus with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws Exception {
    final String encoding = "utf-8";

    byte[] body;

    ExtractableResponse response = RestAssured.given() //
            .contentType(ContentType.TEXT + "; charset=" + encoding).body("Hello World").post("/tarfile/post") //
            .then().extract();

    body = response.body().asByteArray();
    Assertions.assertNotNull(body);

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ByteArrayInputStream bis = new ByteArrayInputStream(body);
    TarArchiveInputStream tis = (TarArchiveInputStream) new ArchiveStreamFactory()
            .createArchiveInputStream(ArchiveStreamFactory.TAR, bis);

    TarArchiveEntry entry = tis.getNextTarEntry();
    if (entry != null) {
        IOHelper.copy(tis, bos);
    }

    String str = bos.toString(encoding);
    Assertions.assertEquals("Hello World", str);
}
 
Example 2
Source File: TarParser.java    From bubble with MIT License 6 votes vote down vote up
@Override
public void parse(File file) throws IOException {
    mEntries = new ArrayList<>();

    BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file));
    TarArchiveInputStream is = new TarArchiveInputStream(fis);
    TarArchiveEntry entry = is.getNextTarEntry();
    while (entry != null) {
        if (entry.isDirectory()) {
            continue;
        }
        if (Utils.isImage(entry.getName())) {
            mEntries.add(new TarEntry(entry, Utils.toByteArray(is)));
        }
        entry = is.getNextTarEntry();
    }

    Collections.sort(mEntries, new NaturalOrderComparator() {
        @Override
        public String stringValue(Object o) {
            return ((TarEntry) o).entry.getName();
        }
    });
}
 
Example 3
Source File: SinParser.java    From Flashtool with GNU General Public License v3.0 6 votes vote down vote up
public boolean isTared() {
	try {
		TarArchiveInputStream tarIn = new TarArchiveInputStream(new FileInputStream(sinfile));
		try {
			while ((tarIn.getNextTarEntry()) != null) {
				break;
			}
			tarIn.close();
			return true;
		} catch (IOException ioe) {
			try { tarIn.close(); } catch (Exception e) {}
			return false;
		}
	} catch (FileNotFoundException fne) {
		return false;
	}
}
 
Example 4
Source File: PSBuildReferenceTaxonomyUtils.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Gets a Reader for a file in a gzipped tarball
 * @param tarPath   Path to the tarball
 * @param fileName   File within the tarball
 * @return   The file's reader
 */
public static BufferedReader getBufferedReaderTarGz(final String tarPath, final String fileName) {
    try {
        InputStream result = null;
        final TarArchiveInputStream tarStream = new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(tarPath)));
        TarArchiveEntry entry = tarStream.getNextTarEntry();
        while (entry != null) {
            if (entry.getName().equals(fileName)) {
                result = tarStream;
                break;
            }
            entry = tarStream.getNextTarEntry();
        }
        if (result == null) {
            throw new UserException.BadInput("Could not find file " + fileName + " in tarball " + tarPath);
        }
        return new BufferedReader(new InputStreamReader(result));
    } catch (final IOException e) {
        throw new UserException.BadInput("Could not open compressed tarball file " + fileName + " in " + tarPath, e);
    }
}
 
Example 5
Source File: FileUtils.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Wraps the parent stream into TarInputStream 
 * and positions it to read the given entry (no wildcards are applicable).
 * 
 * If no entry is given, the stream is positioned to read the first file entry.
 * 
 * @param parentStream
 * @param entryName
 * @return
 * @throws IOException
 */
public static TarArchiveInputStream getTarInputStream(InputStream parentStream, String entryName) throws IOException {
    TarArchiveInputStream tis = new TarArchiveInputStream(parentStream) ;     
    TarArchiveEntry entry;

    // find a matching entry
    while ((entry = tis.getNextTarEntry()) != null) {
    	if (entry.isDirectory()) {
    		continue; // CLS-537: skip directories, we want to read the first file
    	}
    	// when url is given without anchor; first entry in tar file is used
        if (StringUtils.isEmpty(entryName) || entry.getName().equals(entryName)) {
           	return tis;
        }
    }
    
    //no entry found report
    throw new IOException("Wrong anchor (" + entryName + ") to tar file.");
}
 
Example 6
Source File: GeoLite2Geolocator.java    From Plan with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void findAndCopyFromTar(TarArchiveInputStream tarIn, FileOutputStream fos) throws IOException {
    // Breadth first search
    Queue<TarArchiveEntry> entries = new ArrayDeque<>();
    entries.add(tarIn.getNextTarEntry());
    while (!entries.isEmpty()) {
        TarArchiveEntry entry = entries.poll();
        if (entry.isDirectory()) {
            entries.addAll(Arrays.asList(entry.getDirectoryEntries()));
        }

        // Looking for this file
        if (entry.getName().endsWith("GeoLite2-Country.mmdb")) {
            IOUtils.copy(tarIn, fos);
            break; // Found it
        }

        TarArchiveEntry next = tarIn.getNextTarEntry();
        if (next != null) entries.add(next);
    }
}
 
Example 7
Source File: TarUtils.java    From Xndroid with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 文件 解归档
 *
 * @param destFile
 *            目标文件
 * @param tais
 *            ZipInputStream
 * @throws Exception
 */
private static void dearchive(File destFile, TarArchiveInputStream tais)
        throws Exception {

    TarArchiveEntry entry = null;
    while ((entry = tais.getNextTarEntry()) != null) {

        // 文件
        String dir = destFile.getPath() + File.separator + entry.getName();

        File dirFile = new File(dir);

        // 文件检查
        fileProber(dirFile);

        if (entry.isDirectory()) {
            dirFile.mkdirs();
        } else {
            dearchiveFile(dirFile, tais);
        }

    }
}
 
Example 8
Source File: RepositoryDispatcher.java    From steady with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public Path downloadArtifact(Artifact a) throws Exception {
	Path p = null;
	
	if(!a.isReadyForDownload())
		throw new IllegalArgumentException("Artifact not fully specified: " + a);

	// Already downloaded?
	if(a.isCached()) {
		log.debug(a.toString() + " available in local m2 repo: [" + a.getAbsM2Path() + "]");
		p = a.getAbsM2Path();
		
		try{
			if(a.getProgrammingLanguage()==ProgrammingLanguage.JAVA){
				JarFile j = new JarFile(p.toFile(), false, java.util.zip.ZipFile.OPEN_READ);
				j.close();
			}
			else if(a.getProgrammingLanguage()==ProgrammingLanguage.PY && a.getPackaging().equals("sdist") ){
				TarArchiveInputStream t =new TarArchiveInputStream(new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(p.toFile()))));
				t.getNextTarEntry();
				t.close();
			}
		}catch(Exception e){
			e.printStackTrace();
			log.error("Exception when opening archive file [" + p.toFile() + "]: " + e.getMessage());
			boolean deleted = p.toFile().delete();
			if(!deleted)
				log.warn("Couldn't delete presumibly corrupted archive [" + p +"]");
			p=this.downloadArtifactFile(a);
		}
	}
	// No, download!
	else {
		p=this.downloadArtifactFile(a);
			
	}
	return p;
	
}
 
Example 9
Source File: FlowFileUnpackagerV1.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, String> unpackageFlowFile(final InputStream in, final OutputStream out) throws IOException {
    flowFilesRead++;
    final TarArchiveInputStream tarIn = new TarArchiveInputStream(in);
    final TarArchiveEntry attribEntry = tarIn.getNextTarEntry();
    if (attribEntry == null) {
        return null;
    }

    final Map<String, String> attributes;
    if (attribEntry.getName().equals(FlowFilePackagerV1.FILENAME_ATTRIBUTES)) {
        attributes = getAttributes(tarIn);
    } else {
        throw new IOException("Expected two tar entries: "
                + FlowFilePackagerV1.FILENAME_CONTENT + " and "
                + FlowFilePackagerV1.FILENAME_ATTRIBUTES);
    }

    final TarArchiveEntry contentEntry = tarIn.getNextTarEntry();

    if (contentEntry != null && contentEntry.getName().equals(FlowFilePackagerV1.FILENAME_CONTENT)) {
        final byte[] buffer = new byte[512 << 10];//512KB
        int bytesRead = 0;
        while ((bytesRead = tarIn.read(buffer)) != -1) { //still more data to read
            if (bytesRead > 0) {
                out.write(buffer, 0, bytesRead);
            }
        }
        out.flush();
    } else {
        throw new IOException("Expected two tar entries: "
                + FlowFilePackagerV1.FILENAME_CONTENT + " and "
                + FlowFilePackagerV1.FILENAME_ATTRIBUTES);
    }

    return attributes;
}
 
Example 10
Source File: ArchiveUtils.java    From gradle-golang-plugin with Mozilla Public License 2.0 5 votes vote down vote up
public static void unTarGz(Path file, Path target) throws IOException {
    try (final InputStream is = newInputStream(file)) {
        final InputStream gzip = new GZIPInputStream(is);
        final TarArchiveInputStream archive = new TarArchiveInputStream(gzip);
        TarArchiveEntry entry = archive.getNextTarEntry();
        while (entry != null) {
            final Path entryFile = target.resolve(REMOVE_LEADING_GO_PATH_PATTERN.matcher(entry.getName()).replaceFirst("")).toAbsolutePath();
            if (entry.isDirectory()) {
                createDirectoriesIfRequired(entryFile);
            } else {
                ensureParentOf(entryFile);
                try (final OutputStream os = newOutputStream(entryFile)) {
                    copy(archive, os);
                }
                final PosixFileAttributeView view = getFileAttributeView(entryFile, PosixFileAttributeView.class);
                if (view != null) {
                    final int mode = entry.getMode();
                    final Set<PosixFilePermission> perms = new HashSet<>();
                    perms.add(PosixFilePermission.OWNER_READ);
                    perms.add(PosixFilePermission.GROUP_READ);
                    perms.add(PosixFilePermission.OTHERS_READ);
                    //noinspection OctalInteger,ResultOfMethodCallIgnored,IncompatibleBitwiseMaskOperation
                    if ((mode | 0001) > 0) {
                        perms.add(PosixFilePermission.OWNER_EXECUTE);
                    }
                    //noinspection OctalInteger,ResultOfMethodCallIgnored,IncompatibleBitwiseMaskOperation
                    if ((mode | 0100) > 0) {
                        perms.add(PosixFilePermission.GROUP_EXECUTE);
                        perms.add(PosixFilePermission.OTHERS_EXECUTE);
                    }
                    view.setPermissions(perms);
                }
            }
            entry = archive.getNextTarEntry();
        }
    }
}
 
Example 11
Source File: TarFileSystem.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public InputStream getInputStream(GFile file, TaskMonitor monitor)
		throws IOException, CancelledException {

	TarMetadata tmd = fsih.getMetadata(file);
	if (tmd == null) {
		throw new IOException("Unknown file " + file);
	}

	// Open a new instance of the tar file, seek to the requested embedded file,
	// and return the inputstream to the caller, who will close it when done.
	TarArchiveInputStream tarInput =
		new TarArchiveInputStream(new FileInputStream(containerFile));

	int fileNum = 0;
	TarArchiveEntry tarEntry;
	while ((tarEntry = tarInput.getNextTarEntry()) != null) {
		if (fileNum == tmd.fileNum) {
			if (!tmd.tarArchiveEntry.getName().equals(tarEntry.getName())) {
				throw new IOException("Mismatch between filenum and tarEntry for " + file);
			}
			return tarInput;
		}
		fileNum++;
	}
	throw new IOException("Could not find requested file " + file);
}
 
Example 12
Source File: TarUtils.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
/** @apiNote Caller should close `in` after calling this method. */
public static void untar(InputStream in, File targetDir) throws IOException {
  final TarArchiveInputStream tarIn = new TarArchiveInputStream(in);
  byte[] b = new byte[BUF_SIZE];
  TarArchiveEntry tarEntry;
  while ((tarEntry = tarIn.getNextTarEntry()) != null) {
    final File file = new File(targetDir, tarEntry.getName());
    if (tarEntry.isDirectory()) {
      if (!file.mkdirs()) {
        throw new IOException("Unable to create folder " + file.getAbsolutePath());
      }
    } else {
      final File parent = file.getParentFile();
      if (!parent.exists()) {
        if (!parent.mkdirs()) {
          throw new IOException("Unable to create folder " + parent.getAbsolutePath());
        }
      }
      try (FileOutputStream fos = new FileOutputStream(file)) {
        int r;
        while ((r = tarIn.read(b)) != -1) {
          fos.write(b, 0, r);
        }
      }
    }
  }
}
 
Example 13
Source File: TarGzArchiveFileProvider.java    From spring-boot-actuator-logview with MIT License 5 votes vote down vote up
@Override
public List<FileEntry> getFileEntries(Path folder) throws IOException {
    TarArchiveInputStream inputStream = new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(folder.toFile())));
    TarArchiveEntry entry;
    List<FileEntry> files = new ArrayList<>();
    while ((entry = inputStream.getNextTarEntry()) != null) {
        files.add(createFileEntry(entry));
    }
    return files;
}
 
Example 14
Source File: CompressionFileUtil.java    From Jpom with MIT License 5 votes vote down vote up
private static List<String> unTar(InputStream inputStream, File destDir) throws Exception {
    List<String> fileNames = new ArrayList<>();
    TarArchiveInputStream tarIn = new TarArchiveInputStream(inputStream, BUFFER_SIZE, CharsetUtil.GBK);
    TarArchiveEntry entry;
    try {
        while ((entry = tarIn.getNextTarEntry()) != null) {
            fileNames.add(entry.getName());
            if (entry.isDirectory()) {
                //是目录
                FileUtil.mkdir(new File(destDir, entry.getName()));
                //创建空目录
            } else {
                //是文件
                File tmpFile = new File(destDir, entry.getName());
                //创建输出目录
                FileUtil.mkParentDirs(destDir);
                OutputStream out = null;
                try {
                    out = new FileOutputStream(tmpFile);
                    int length;
                    byte[] b = new byte[2048];
                    while ((length = tarIn.read(b)) != -1) {
                        out.write(b, 0, length);
                    }
                } finally {
                    IOUtils.closeQuietly(out);
                }
            }
        }
    } finally {
        IOUtils.closeQuietly(tarIn);
    }
    return fileNames;
}
 
Example 15
Source File: CompressedTarFunction.java    From bazel with Apache License 2.0 4 votes vote down vote up
@Override
public Path decompress(DecompressorDescriptor descriptor)
    throws InterruptedException, IOException {
  if (Thread.interrupted()) {
    throw new InterruptedException();
  }
  Optional<String> prefix = descriptor.prefix();
  boolean foundPrefix = false;
  Set<String> availablePrefixes = new HashSet<>();

  try (InputStream decompressorStream = getDecompressorStream(descriptor)) {
    TarArchiveInputStream tarStream = new TarArchiveInputStream(decompressorStream);
    TarArchiveEntry entry;
    while ((entry = tarStream.getNextTarEntry()) != null) {
      StripPrefixedPath entryPath = StripPrefixedPath.maybeDeprefix(entry.getName(), prefix);
      foundPrefix = foundPrefix || entryPath.foundPrefix();

      if (prefix.isPresent() && !foundPrefix) {
        Optional<String> suggestion =
            CouldNotFindPrefixException.maybeMakePrefixSuggestion(entryPath.getPathFragment());
        if (suggestion.isPresent()) {
          availablePrefixes.add(suggestion.get());
        }
      }

      if (entryPath.skip()) {
        continue;
      }

      Path filePath = descriptor.repositoryPath().getRelative(entryPath.getPathFragment());
      FileSystemUtils.createDirectoryAndParents(filePath.getParentDirectory());
      if (entry.isDirectory()) {
        FileSystemUtils.createDirectoryAndParents(filePath);
      } else {
        if (entry.isSymbolicLink() || entry.isLink()) {
          PathFragment targetName = PathFragment.create(entry.getLinkName());
          targetName = maybeDeprefixSymlink(targetName, prefix, descriptor.repositoryPath());
          if (entry.isSymbolicLink()) {
            if (filePath.exists()) {
              filePath.delete();
            }
            FileSystemUtils.ensureSymbolicLink(filePath, targetName);
          } else {
            Path targetPath = descriptor.repositoryPath().getRelative(targetName);
            if (filePath.equals(targetPath)) {
              // The behavior here is semantically different, depending on whether the underlying
              // filesystem is case-sensitive or case-insensitive. However, it is effectively the
              // same: we drop the link entry.
              // * On a case-sensitive filesystem, this is a hardlink to itself, such as GNU tar
              //   creates when given repeated files. We do nothing since the link already exists.
              // * On a case-insensitive filesystem, we may be extracting a differently-cased
              //   hardlink to the same file (such as when extracting an archive created on a
              //   case-sensitive filesystem). GNU tar, for example, will drop the new link entry.
              //   BSD tar on MacOS X (by default case-insensitive) errors and aborts extraction.
            } else {
              if (filePath.exists()) {
                filePath.delete();
              }
              FileSystemUtils.createHardLink(filePath, targetPath);
            }
          }
        } else {
          try (OutputStream out = filePath.getOutputStream()) {
            ByteStreams.copy(tarStream, out);
          }
          filePath.chmod(entry.getMode());

          // This can only be done on real files, not links, or it will skip the reader to
          // the next "real" file to try to find the mod time info.
          Date lastModified = entry.getLastModifiedDate();
          filePath.setLastModifiedTime(lastModified.getTime());
        }
      }
      if (Thread.interrupted()) {
        throw new InterruptedException();
      }
    }

    if (prefix.isPresent() && !foundPrefix) {
      throw new CouldNotFindPrefixException(prefix.get(), availablePrefixes);
    }
  }

  return descriptor.repositoryPath();
}
 
Example 16
Source File: GeoIPService.java    From proxylive with MIT License 4 votes vote down vote up
@Scheduled(fixedDelay = 86400 * 1000) //Every 24H
@PostConstruct
private void downloadIPLocationDatabase() throws Exception {
    if(config.getGeoIP().isEnabled()){
        logger.info("Downloading GEOIP Database from: "+config.getGeoIP().getUrl());

        File tmpGEOIPFileRound = File.createTempFile("geoIP", "mmdb");
        FileOutputStream fos = new FileOutputStream(tmpGEOIPFileRound);
        HttpURLConnection connection = getURLConnection(config.getGeoIP().getUrl());
        if (connection.getResponseCode() != 200) {
            return;
        }
        TarArchiveInputStream tarGzGeoIPStream = new TarArchiveInputStream(new GZIPInputStream(connection.getInputStream()));
        TarArchiveEntry entry= null;
        int offset;
        long pointer=0;
        while ((entry = tarGzGeoIPStream.getNextTarEntry()) != null) {
            pointer+=entry.getSize();
            if(entry.getName().endsWith("GeoLite2-City.mmdb")){
                byte[] content = new byte[(int) entry.getSize()];
                offset=0;
                //FileInputStream fis = new FileInputStream(entry.getFile());
                //IOUtils.copy(fis,fos);
                //tarGzGeoIPStream.skip(pointer);
                //tarGzGeoIPStream.read(content,offset,content.length-offset);
                //IOUtils.write(content,fos);
                int r;
                byte[] b = new byte[1024];
                while ((r = tarGzGeoIPStream.read(b)) != -1) {
                    fos.write(b, 0, r);
                }
                //fis.close();
                break;
            }
        }
        tarGzGeoIPStream.close();
        fos.flush();
        fos.close();
        connection.disconnect();
        geoIPDB = new DatabaseReader.Builder(tmpGEOIPFileRound).withCache(new CHMCache()).build();
        if (tmpGEOIPFile != null && tmpGEOIPFile.exists()) {
            tmpGEOIPFile.delete();
        }
        tmpGEOIPFile = tmpGEOIPFileRound;
    }
}
 
Example 17
Source File: TestUtils.java    From fabric-sdk-java with Apache License 2.0 4 votes vote down vote up
public static ArrayList tarBytesToEntryArrayList(byte[] bytes) throws Exception {

        ArrayList<String> ret = new ArrayList<>();

        TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(new GZIPInputStream(new ByteArrayInputStream(bytes)));

        for (TarArchiveEntry ta = tarArchiveInputStream.getNextTarEntry(); null != ta; ta = tarArchiveInputStream.getNextTarEntry()) {

            Assert.assertTrue(format("Tar entry %s is not a file.", ta.getName()), ta.isFile()); //we only expect files.
            ret.add(ta.getName());

        }

        return ret;

    }
 
Example 18
Source File: ProjectArchives.java    From digdag with Apache License 2.0 4 votes vote down vote up
private static void extractArchive(Path destDir, TarArchiveInputStream archive, ExtractListener listener)
      throws IOException
  {
      String prefix = destDir.toString();
      TarArchiveEntry entry;
      while (true) {
          entry = archive.getNextTarEntry();
          if (entry == null) {
              break;
          }
          Path path = destDir.resolve(entry.getName()).normalize();
          if (!path.toString().startsWith(prefix)) {
              throw new RuntimeException("Archive includes an invalid entry: " + entry.getName());
          }
          if (entry.isDirectory()) {
              Files.createDirectories(path);
          }
          else if (entry.isSymbolicLink()) {
              Files.createDirectories(path.getParent());
              String dest = entry.getLinkName();
              Path destAbsPath = path.getParent().resolve(dest).normalize();
              if (!destAbsPath.normalize().toString().startsWith(prefix)) {
                  throw new RuntimeException("Archive includes an invalid symlink: " + entry.getName() + " -> " + dest);
              }
              if (listener != null) {
                  listener.symlink(destDir.relativize(path), dest);
              }
              Files.createSymbolicLink(path, Paths.get(dest));
          }
          else {
              Files.createDirectories(path.getParent());
              if (listener != null) {
                  listener.file(destDir.relativize(path));
              }
              try (OutputStream out = Files.newOutputStream(path)) {
                  ByteStreams.copy(archive, out);
              }
          }
          if (!Files.isSymbolicLink(path) && isPosixCompliant()) {
// Files.setPosixFilePermissions doesn't work on Windows: java.lang.UnsupportedOperationException
              Files.setPosixFilePermissions(path, getPosixFilePermissions(entry));
          }
      }
  }
 
Example 19
Source File: TestTarContainerPacker.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
@Test
public void pack() throws IOException, CompressorException {

  //GIVEN
  OzoneConfiguration conf = new OzoneConfiguration();

  KeyValueContainerData sourceContainerData =
      createContainer(SOURCE_CONTAINER_ROOT);

  KeyValueContainer sourceContainer =
      new KeyValueContainer(sourceContainerData, conf);

  //sample db file in the metadata directory
  writeDbFile(sourceContainerData, TEST_DB_FILE_NAME);

  //sample chunk file in the chunk directory
  writeChunkFile(sourceContainerData, TEST_CHUNK_FILE_NAME);

  //sample container descriptor file
  writeDescriptor(sourceContainer);

  Path targetFile = TEMP_DIR.resolve("container.tar.gz");

  //WHEN: pack it
  try (FileOutputStream output = new FileOutputStream(targetFile.toFile())) {
    packer.pack(sourceContainer, output);
  }

  //THEN: check the result
  try (FileInputStream input = new FileInputStream(targetFile.toFile())) {
    CompressorInputStream uncompressed = new CompressorStreamFactory()
        .createCompressorInputStream(GZIP, input);
    TarArchiveInputStream tarStream = new TarArchiveInputStream(uncompressed);

    TarArchiveEntry entry;
    Map<String, TarArchiveEntry> entries = new HashMap<>();
    while ((entry = tarStream.getNextTarEntry()) != null) {
      entries.put(entry.getName(), entry);
    }

    Assert.assertTrue(
        entries.containsKey("container.yaml"));

  }

  //read the container descriptor only
  try (FileInputStream input = new FileInputStream(targetFile.toFile())) {
    String containerYaml = new String(packer.unpackContainerDescriptor(input),
        Charset.forName(UTF_8.name()));
    Assert.assertEquals(TEST_DESCRIPTOR_FILE_CONTENT, containerYaml);
  }

  KeyValueContainerData destinationContainerData =
      createContainer(DEST_CONTAINER_ROOT);

  KeyValueContainer destinationContainer =
      new KeyValueContainer(destinationContainerData, conf);

  String descriptor;

  //unpackContainerData
  try (FileInputStream input = new FileInputStream(targetFile.toFile())) {
    descriptor =
        new String(packer.unpackContainerData(destinationContainer, input),
            Charset.forName(UTF_8.name()));
  }

  assertExampleMetadataDbIsGood(
      destinationContainerData.getDbFile().toPath(),
      TEST_DB_FILE_NAME);
  assertExampleChunkFileIsGood(
      Paths.get(destinationContainerData.getChunksPath()),
      TEST_CHUNK_FILE_NAME);
  Assert.assertFalse(
      "Descriptor file should not have been extracted by the "
          + "unpackContainerData Call",
      destinationContainer.getContainerFile().exists());
  Assert.assertEquals(TEST_DESCRIPTOR_FILE_CONTENT, descriptor);
}
 
Example 20
Source File: FileUtils.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Creates a list of names of matching entries.
 * @param parentStream
 * @param pattern
 * 
 * @return resolved anchors
 * @throws IOException
 */
public static List<String> getMatchingTarEntries(InputStream parentStream, String pattern) throws IOException {
	if (pattern == null) {
		pattern = "";
	}
	pattern = URLDecoder.decode(pattern, UTF8); // CL-2579
	List<String> resolvedAnchors = new ArrayList<String>();

    Matcher matcher;
    Pattern WILDCARD_PATTERN = null;
    boolean containsWildcard = pattern.contains("?") || pattern.contains("*");
    if (containsWildcard) {
    	WILDCARD_PATTERN = Pattern.compile(pattern.replaceAll("\\\\", "\\\\\\\\").replaceAll("\\.", "\\\\.").replaceAll("\\?", "\\.").replaceAll("\\*", ".*"));
    }

    //resolve url format for zip files
    TarArchiveInputStream tis = new TarArchiveInputStream(parentStream) ;     
    TarArchiveEntry entry;

    while ((entry = tis.getNextTarEntry()) != null) {
    	if (entry.isDirectory()) {
    		continue; // CLS-537: skip directories, we want to read the first file
    	}
        // wild cards
        if (containsWildcard) {
       		matcher = WILDCARD_PATTERN.matcher(entry.getName());
       		if (matcher.matches()) {
       			resolvedAnchors.add(entry.getName());
            }
    	// without wild cards
        } else if (pattern.isEmpty() || entry.getName().equals(pattern)) { //url is given without anchor; first entry in zip file is used
           	resolvedAnchors.add(pattern);
        }
    }
    
    // if no wild carded entry found, it is ok
    if (!pattern.isEmpty() && !containsWildcard && resolvedAnchors.isEmpty()) {
    	throw new IOException("Wrong anchor (" + pattern + ") to zip file.");
    }

	return resolvedAnchors; 
}