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

The following examples show how to use org.apache.commons.compress.archivers.tar.TarArchiveEntry#getSize() . 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: TarUnpackerSequenceFileWriter.java    From localization_nifi with Apache License 2.0 7 votes vote down vote up
@Override
protected void processInputStream(final InputStream stream, final FlowFile tarArchivedFlowFile, final Writer writer) throws IOException {
    try (final TarArchiveInputStream tarIn = new TarArchiveInputStream(new BufferedInputStream(stream))) {
        TarArchiveEntry tarEntry;
        while ((tarEntry = tarIn.getNextTarEntry()) != null) {
            if (tarEntry.isDirectory()) {
                continue;
            }
            final String key = tarEntry.getName();
            final long fileSize = tarEntry.getSize();
            final InputStreamWritable inStreamWritable = new InputStreamWritable(tarIn, (int) fileSize);
            writer.append(new Text(key), inStreamWritable);
            logger.debug("Appending FlowFile {} to Sequence File", new Object[]{key});
        }
    }
}
 
Example 2
Source File: LifecycleChaincodePackage.java    From fabric-sdk-java with Apache License 2.0 6 votes vote down vote up
public JsonObject getMetaInfJson() throws IOException {

        try (TarArchiveInputStream tarInput = new TarArchiveInputStream(new GzipCompressorInputStream(new ByteArrayInputStream(pBytes)))) {

            TarArchiveEntry currentEntry = tarInput.getNextTarEntry();
            while (currentEntry != null) {
                if (currentEntry.getName().equals("metadata.json")) {
                    byte[] buf = new byte[(int) currentEntry.getSize()];
                    tarInput.read(buf, 0, (int) currentEntry.getSize());

                    try (InputStream stream = new ByteArrayInputStream(buf)) {
                        try (JsonReader reader = Json.createReader(stream)) {

                            return (JsonObject) reader.read();
                        }
                    }

                }
                currentEntry = tarInput.getNextTarEntry();

            }
        }

        return null;
    }
 
Example 3
Source File: LifecycleChaincodePackage.java    From fabric-sdk-java with Apache License 2.0 6 votes vote down vote up
public byte[] getChaincodePayloadBytes() throws IOException {

        try (TarArchiveInputStream tarInput = new TarArchiveInputStream(new GzipCompressorInputStream(new ByteArrayInputStream(pBytes)))) {

            TarArchiveEntry currentEntry = tarInput.getNextTarEntry();
            while (currentEntry != null) {
                if (!currentEntry.getName().equals("metadata.json")) { // right now anything but this
                    byte[] buf = new byte[(int) currentEntry.getSize()];
                    tarInput.read(buf, 0, (int) currentEntry.getSize());

                    return buf;

                }
                currentEntry = tarInput.getNextTarEntry();
            }
        }

        return null;
    }
 
Example 4
Source File: IOHelper.java    From spring-boot-cookbook with Apache License 2.0 6 votes vote down vote up
public static void printTarGzFile(File tarFile) throws IOException {
    BufferedInputStream bin = new BufferedInputStream(FileUtils.openInputStream(tarFile));
    CompressorInputStream cis = new GzipCompressorInputStream(bin);

    try (TarArchiveInputStream tais = new TarArchiveInputStream(cis)) {
        TarArchiveEntry entry;
        while ((entry = tais.getNextTarEntry()) != null) {
            if (entry.isDirectory()) {
                LOGGER.warn("dir:{}", entry.getName());
            } else {
                int size = (int) entry.getSize();
                byte[] content = new byte[size];
                int readCount = tais.read(content, 0, size);
                LOGGER.info("fileName:{}", entry.getName());
                ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(content, 0, readCount);
                try (LineIterator iterator = IOUtils.lineIterator(byteArrayInputStream, Charset.forName("utf-8"))) {
                    while (iterator.hasNext()) {
                        LOGGER.info("line:{}", iterator.nextLine());
                    }
                }
            }
        }
        LOGGER.info("===============finish===============");
    }
}
 
Example 5
Source File: ArchiveUtil.java    From tracecompass with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Get the archive size
 *
 * @param archivePath
 *            Path of the archive
 * @return Size of the archive in byte or -1 if there is an error
 */
public static long getArchiveSize(String archivePath) {
    TarFile tarFile = getSpecifiedTarSourceFile(archivePath);
    long archiveSize = 0;
    if (tarFile != null) {
        ArrayList<TarArchiveEntry> entries = Collections.list(tarFile.entries());
        for (TarArchiveEntry tarArchiveEntry : entries) {
            archiveSize += tarArchiveEntry.getSize();
        }
        closeTarFile(tarFile);
        return archiveSize;
    }

    ZipFile zipFile = getSpecifiedZipSourceFile(archivePath);
    if (zipFile != null) {
        for (Enumeration<? extends ZipEntry> e = zipFile.entries(); e.hasMoreElements();) {
            archiveSize += Objects.requireNonNull(e.nextElement()).getSize();
        }
        closeZipFile(zipFile);
        return archiveSize;
    }

    return -1;
}
 
Example 6
Source File: DeployOvfTemplateService.java    From cs-actions with Apache License 2.0 6 votes vote down vote up
private ITransferVmdkFrom getTransferVmdK(final String templateFilePathStr, final String vmdkName) throws IOException {
    final Path templateFilePath = Paths.get(templateFilePathStr);
    if (isOva(templateFilePath)) {
        final TarArchiveInputStream tar = new TarArchiveInputStream(new FileInputStream(templateFilePathStr));
        TarArchiveEntry entry;
        while ((entry = tar.getNextTarEntry()) != null) {
            if (new File(entry.getName()).getName().startsWith(vmdkName)) {
                return new TransferVmdkFromInputStream(tar, entry.getSize());
            }
        }
    } else if (isOvf(templateFilePath)) {
        final Path vmdkPath = templateFilePath.getParent().resolve(vmdkName);
        return new TransferVmdkFromFile(vmdkPath.toFile());
    }
    throw new RuntimeException(NOT_OVA_OR_OVF);
}
 
Example 7
Source File: TarUnpackerSequenceFileWriter.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
protected void processInputStream(final InputStream stream, final FlowFile tarArchivedFlowFile, final Writer writer) throws IOException {
    try (final TarArchiveInputStream tarIn = new TarArchiveInputStream(new BufferedInputStream(stream))) {
        TarArchiveEntry tarEntry;
        while ((tarEntry = tarIn.getNextTarEntry()) != null) {
            if (tarEntry.isDirectory()) {
                continue;
            }
            final String key = tarEntry.getName();
            final long fileSize = tarEntry.getSize();
            final InputStreamWritable inStreamWritable = new InputStreamWritable(tarIn, (int) fileSize);
            writer.append(new Text(key), inStreamWritable);
            logger.debug("Appending FlowFile {} to Sequence File", new Object[]{key});
        }
    }
}
 
Example 8
Source File: OmnisharpStreamConnectionProvider.java    From aCute with Eclipse Public License 2.0 5 votes vote down vote up
/**
 *
 * @return path to server, unzipping it if necessary. Can be null is fragment is missing.
 */
private @Nullable File getServer() throws IOException {
	File serverPath = new File(AcutePlugin.getDefault().getStateLocation().toFile(), "omnisharp-roslyn"); //$NON-NLS-1$
	if (!serverPath.exists()) {
		serverPath.mkdirs();
		try (
			InputStream stream = FileLocator.openStream(AcutePlugin.getDefault().getBundle(), new Path("omnisharp-roslyn.tar"), true); //$NON-NLS-1$
			TarArchiveInputStream tarStream = new TarArchiveInputStream(stream);
		) {
			TarArchiveEntry entry = null;
			while ((entry = tarStream.getNextTarEntry()) != null) {
				if (!entry.isDirectory()) {
					File targetFile = new File(serverPath, entry.getName());
					targetFile.getParentFile().mkdirs();
					InputStream in = new BoundedInputStream(tarStream, entry.getSize()); // mustn't be closed
					try (
						FileOutputStream out = new FileOutputStream(targetFile);
					) {
						IOUtils.copy(in, out);
						if (!Platform.OS_WIN32.equals(Platform.getOS())) {
							int xDigit = entry.getMode() % 10;
							targetFile.setExecutable(xDigit > 0, (xDigit & 1) == 1);
							int wDigit = (entry.getMode() / 10) % 10;
							targetFile.setWritable(wDigit > 0, (wDigit & 1) == 1);
							int rDigit = (entry.getMode() / 100) % 10;
							targetFile.setReadable(rDigit > 0, (rDigit & 1) == 1);
						}
					}
				}
			}
		}
	}
	return serverPath;
}
 
Example 9
Source File: NpmExtractor.java    From packagedrone with Eclipse Public License 1.0 5 votes vote down vote up
private void perform ( final Path file, final Map<String, String> metadata ) throws IOException
{
    try ( final GZIPInputStream gis = new GZIPInputStream ( new FileInputStream ( file.toFile () ) );
          final TarArchiveInputStream tis = new TarArchiveInputStream ( gis ) )
    {
        TarArchiveEntry entry;
        while ( ( entry = tis.getNextTarEntry () ) != null )
        {
            if ( entry.getName ().equals ( "package/package.json" ) )
            {
                final byte[] data = new byte[(int)entry.getSize ()];
                ByteStreams.read ( tis, data, 0, data.length );

                final String str = StandardCharsets.UTF_8.decode ( ByteBuffer.wrap ( data ) ).toString ();

                try
                {
                    // test parse
                    new JsonParser ().parse ( str );
                    // store
                    metadata.put ( "package.json", str );
                }
                catch ( final JsonParseException e )
                {
                    // ignore
                }

                break; // stop parsing the archive
            }
        }

    }
}
 
Example 10
Source File: ProjectResource.java    From digdag with Apache License 2.0 5 votes vote down vote up
private void validateTarEntry(TarArchiveEntry entry)
{
    if (entry.getSize() > MAX_ARCHIVE_FILE_SIZE_LIMIT) {
        throw new IllegalArgumentException(String.format(ENGLISH,
                    "Size of a file in the archive exceeds limit (%d > %d bytes): %s",
                    entry.getSize(), MAX_ARCHIVE_FILE_SIZE_LIMIT, entry.getName()));
    }
}
 
Example 11
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;
    }
}