Example usage for org.apache.commons.compress.archivers.tar TarArchiveEntry getSize

List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry getSize

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.tar TarArchiveEntry getSize.

Prototype

public long getSize() 

Source Link

Document

Get this entry's file size.

Usage

From source file:com.espringtran.compressor4j.processor.TarGzProcessor.java

/**
 * Read from compressed file/* ww  w.jav a2  s .com*/
 * 
 * @param srcPath
 *            path of compressed file
 * @param fileCompressor
 *            FileCompressor object
 * @throws Exception
 */
@Override
public void read(String srcPath, FileCompressor fileCompressor) throws Exception {
    long t1 = System.currentTimeMillis();
    byte[] data = FileUtil.convertFileToByte(srcPath);
    ByteArrayInputStream bais = new ByteArrayInputStream(data);
    GzipCompressorInputStream cis = new GzipCompressorInputStream(bais);
    TarArchiveInputStream ais = new TarArchiveInputStream(cis);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        byte[] buffer = new byte[1024];
        int readByte;
        TarArchiveEntry entry = ais.getNextTarEntry();
        while (entry != null && entry.getSize() > 0) {
            long t2 = System.currentTimeMillis();
            baos = new ByteArrayOutputStream();
            readByte = ais.read(buffer);
            while (readByte != -1) {
                baos.write(buffer, 0, readByte);
                readByte = ais.read(buffer);
            }
            BinaryFile binaryFile = new BinaryFile(entry.getName(), baos.toByteArray());
            fileCompressor.addBinaryFile(binaryFile);
            LogUtil.createAddFileLog(fileCompressor, binaryFile, t2, System.currentTimeMillis());
            entry = ais.getNextTarEntry();
        }
    } catch (Exception e) {
        FileCompressor.LOGGER.error("Error on get compressor file", e);
    } finally {
        baos.close();
        ais.close();
        cis.close();
        bais.close();
    }
    LogUtil.createReadLog(fileCompressor, srcPath, data.length, t1, System.currentTimeMillis());
}

From source file:com.espringtran.compressor4j.processor.XzProcessor.java

/**
 * Read from compressed file/*  ww  w  .  ja  va  2s.  c o m*/
 * 
 * @param srcPath
 *            path of compressed file
 * @param fileCompressor
 *            FileCompressor object
 * @throws Exception
 */
@Override
public void read(String srcPath, FileCompressor fileCompressor) throws Exception {
    long t1 = System.currentTimeMillis();
    byte[] data = FileUtil.convertFileToByte(srcPath);
    ByteArrayInputStream bais = new ByteArrayInputStream(data);
    XZCompressorInputStream cis = new XZCompressorInputStream(bais);
    TarArchiveInputStream ais = new TarArchiveInputStream(cis);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        byte[] buffer = new byte[1024];
        int readByte;
        TarArchiveEntry entry = ais.getNextTarEntry();
        while (entry != null && entry.getSize() > 0) {
            long t2 = System.currentTimeMillis();
            baos = new ByteArrayOutputStream();
            readByte = ais.read(buffer);
            while (readByte != -1) {
                baos.write(buffer, 0, readByte);
                readByte = ais.read(buffer);
            }
            BinaryFile binaryFile = new BinaryFile(entry.getName(), baos.toByteArray());
            fileCompressor.addBinaryFile(binaryFile);
            LogUtil.createAddFileLog(fileCompressor, binaryFile, t2, System.currentTimeMillis());
            entry = ais.getNextTarEntry();
        }
    } catch (Exception e) {
        FileCompressor.LOGGER.error("Error on get compressor file", e);
    } finally {
        baos.close();
        ais.close();
        cis.close();
        bais.close();
    }
    LogUtil.createReadLog(fileCompressor, srcPath, data.length, t1, System.currentTimeMillis());
}

From source file:com.cloudbees.clickstack.util.Files2.java

/**
 * TODO recopy file permissions//from w w  w  .  j  a  v  a  2  s  . c  o m
 * <p/>
 * Uncompress the specified tgz file to the specified destination directory.
 * Replaces any files in the destination, if they already exist.
 *
 * @param tgzFile the name of the zip file to extract
 * @param destDir the directory to unzip to
 * @throws RuntimeIOException
 */
public static void untgz(@Nonnull Path tgzFile, @Nonnull Path destDir) throws RuntimeIOException {
    try {
        //if the destination doesn't exist, create it
        if (Files.notExists(destDir)) {
            logger.trace("Create dir: {}", destDir);
            Files.createDirectories(destDir);
        }

        TarArchiveInputStream in = new TarArchiveInputStream(
                new GzipCompressorInputStream(Files.newInputStream(tgzFile)));

        TarArchiveEntry entry;
        while ((entry = in.getNextTarEntry()) != null) {
            if (entry.isDirectory()) {
                Path dir = destDir.resolve(entry.getName());
                logger.trace("Create dir {}", dir);
                Files.createDirectories(dir);
            } else {
                Path file = destDir.resolve(entry.getName());
                logger.trace("Create file {}: {} bytes", file, entry.getSize());
                OutputStream out = Files.newOutputStream(file);
                IOUtils.copy(in, out);
                out.close();
            }
        }

        in.close();
    } catch (IOException e) {
        throw new RuntimeIOException("Exception expanding " + tgzFile + " to " + destDir, e);
    }
}

From source file:com.vmware.content.samples.ImportOva.java

private void uploadOva(String ovaPath, String sessionId) throws Exception {
    SSLContextBuilder builder = new SSLContextBuilder();
    builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
    CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
    IOUtil.print("Streaming OVF to update session " + sessionId);
    try (TarArchiveInputStream tar = new TarArchiveInputStream(new FileInputStream(ovaPath))) {
        TarArchiveEntry entry;
        while ((entry = tar.getNextTarEntry()) != null) {
            long bytes = entry.getSize();
            IOUtil.print("Uploading " + entry.getName() + " (" + entry.getSize() + " bytes)");
            URI uploadUri = generateUploadUri(sessionId, entry.getName());
            HttpPut request = new HttpPut(uploadUri);
            HttpEntity content = new TarBasedInputStreamEntity(tar, bytes);
            request.setEntity(content);/*from  ww w . j  a v  a 2 s. c o  m*/
            HttpResponse response = httpclient.execute(request);
            EntityUtils.consumeQuietly(response.getEntity());
        }
    }
}

From source file:de.dentrassi.pm.npm.aspect.NpmExtractor.java

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
                }// w w  w. j a  va  2 s .  c om

                break; // stop parsing the archive
            }
        }

    }
}

From source file:cpcc.vvrte.services.TarArchiveDemo.java

@Test
public void shouldWriteTarFile() throws IOException, ArchiveException {
    byte[] c1 = "content1\n".getBytes("UTF-8");
    byte[] c2 = "content2 text\n".getBytes("UTF-8");

    Date t1 = new Date(1000L * (System.currentTimeMillis() / 1000L));
    Date t2 = new Date(t1.getTime() - 30000);

    FileOutputStream fos = new FileOutputStream("bugger1.tar");

    ArchiveStreamFactory factory = new ArchiveStreamFactory("UTF-8");
    ArchiveOutputStream outStream = factory.createArchiveOutputStream("tar", fos);

    TarArchiveEntry archiveEntry1 = new TarArchiveEntry("entry1");
    archiveEntry1.setModTime(t1);//from  w ww. j a  v  a2  s.co  m
    archiveEntry1.setSize(c1.length);
    archiveEntry1.setIds(STORAGE_ID_ONE, CHUNK_ID_ONE);
    archiveEntry1.setNames(USER_NAME_ONE, GROUP_NAME_ONE);

    outStream.putArchiveEntry(archiveEntry1);
    outStream.write(c1);
    outStream.closeArchiveEntry();

    TarArchiveEntry archiveEntry2 = new TarArchiveEntry("data/entry2");
    archiveEntry2.setModTime(t2);
    archiveEntry2.setSize(c2.length);
    archiveEntry2.setIds(STORAGE_ID_TWO, CHUNK_ID_TWO);
    archiveEntry2.setNames(USER_NAME_TWO, GROUP_NAME_TWO);

    outStream.putArchiveEntry(archiveEntry2);
    outStream.write(c2);
    outStream.closeArchiveEntry();

    outStream.close();

    FileInputStream fis = new FileInputStream("bugger1.tar");
    ArchiveInputStream inStream = factory.createArchiveInputStream("tar", fis);

    TarArchiveEntry entry1 = (TarArchiveEntry) inStream.getNextEntry();
    assertThat(entry1.getModTime()).isEqualTo(t1);
    assertThat(entry1.getSize()).isEqualTo(c1.length);
    assertThat(entry1.getLongUserId()).isEqualTo(STORAGE_ID_ONE);
    assertThat(entry1.getLongGroupId()).isEqualTo(CHUNK_ID_ONE);
    assertThat(entry1.getUserName()).isEqualTo(USER_NAME_ONE);
    assertThat(entry1.getGroupName()).isEqualTo(GROUP_NAME_ONE);
    ByteArrayOutputStream b1 = new ByteArrayOutputStream();
    IOUtils.copy(inStream, b1);
    b1.close();
    assertThat(b1.toByteArray().length).isEqualTo(c1.length);
    assertThat(b1.toByteArray()).isEqualTo(c1);

    TarArchiveEntry entry2 = (TarArchiveEntry) inStream.getNextEntry();
    assertThat(entry2.getModTime()).isEqualTo(t2);
    assertThat(entry2.getSize()).isEqualTo(c2.length);
    assertThat(entry2.getLongUserId()).isEqualTo(STORAGE_ID_TWO);
    assertThat(entry2.getLongGroupId()).isEqualTo(CHUNK_ID_TWO);
    assertThat(entry2.getUserName()).isEqualTo(USER_NAME_TWO);
    assertThat(entry2.getGroupName()).isEqualTo(GROUP_NAME_TWO);
    ByteArrayOutputStream b2 = new ByteArrayOutputStream();
    IOUtils.copy(inStream, b2);
    b2.close();
    assertThat(b2.toByteArray().length).isEqualTo(c2.length);
    assertThat(b2.toByteArray()).isEqualTo(c2);

    TarArchiveEntry entry3 = (TarArchiveEntry) inStream.getNextEntry();
    assertThat(entry3).isNull();

    inStream.close();
}

From source file:drawnzer.anurag.tarHelper.TarObj.java

public TarObj(TarArchiveEntry entry, String fname, String pname) {
    // TODO Auto-generated constructor stub
    this.ent = entry;
    this.name = fname;
    this.path = pname;
    this.isFile = checkForFile();
    this.size = Utils.size(entry.getSize());
    this.type = Utils.getType(this.name, isFile);
}

From source file:com.hortonworks.streamline.streams.service.CustomProcessorUploadHandler.java

private byte[] getFileAsByteArray(File tarFile, String fileName) {
    BufferedInputStream bis = null;
    TarArchiveInputStream tarArchiveInputStream = null;
    byte[] data = null;
    try {/*from w w  w .  j av  a2  s .  co  m*/
        LOG.info("Getting file " + fileName + " from " + tarFile);
        bis = new BufferedInputStream(new FileInputStream(tarFile));
        tarArchiveInputStream = new TarArchiveInputStream(bis);
        TarArchiveEntry tarArchiveEntry = tarArchiveInputStream.getNextTarEntry();
        while (tarArchiveEntry != null) {
            if (tarArchiveEntry.getName().equals(fileName)) {
                long size = tarArchiveEntry.getSize();
                data = new byte[(int) size];
                tarArchiveInputStream.read(data, 0, data.length);
                break;
            }
            tarArchiveEntry = tarArchiveInputStream.getNextTarEntry();
        }
    } catch (IOException e) {
        LOG.warn("Exception occured while getting file: " + fileName + " from " + tarFile, e);
    } finally {
        try {
            if (bis != null) {
                bis.close();
            }
            if (tarArchiveInputStream != null) {
                tarArchiveInputStream.close();
            }
        } catch (IOException e) {
            LOG.warn("Error closing input stream for the tar file: " + tarFile, e);
        }
    }
    return data;
}

From source file:de.flapdoodle.embedmongo.extract.TgzExtractor.java

@Override
public void extract(RuntimeConfig runtime, File source, File destination, Pattern file) throws IOException {

    IProgressListener progressListener = runtime.getProgressListener();
    String progressLabel = "Extract " + source;
    progressListener.start(progressLabel);

    FileInputStream fin = new FileInputStream(source);
    BufferedInputStream in = new BufferedInputStream(fin);
    GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);

    TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn);
    try {//from   w ww .  j  a va2s .  com
        TarArchiveEntry entry;
        while ((entry = tarIn.getNextTarEntry()) != null) {
            if (file.matcher(entry.getName()).matches()) {
                //               System.out.println("File: " + entry.getName());
                if (tarIn.canReadEntryData(entry)) {
                    //                  System.out.println("Can Read: " + entry.getName());
                    long size = entry.getSize();
                    Files.write(tarIn, size, destination);
                    destination.setExecutable(true);
                    //                  System.out.println("DONE");
                    progressListener.done(progressLabel);
                }
                break;

            } else {
                //               System.out.println("SKIP File: " + entry.getName());
            }
        }

    } finally {
        tarIn.close();
        gzIn.close();
    }
}

From source file:com.amaze.filemanager.filesystem.compressed.extractcontents.helpers.TarExtractor.java

@Override
protected void extractWithFilter(@NonNull Filter filter) throws IOException {
    long totalBytes = 0;
    List<TarArchiveEntry> archiveEntries = new ArrayList<>();
    TarArchiveInputStream inputStream = new TarArchiveInputStream(new FileInputStream(filePath));

    TarArchiveEntry tarArchiveEntry;

    while ((tarArchiveEntry = inputStream.getNextTarEntry()) != null) {
        if (CompressedHelper.isEntryPathValid(tarArchiveEntry.getName())) {
            if (filter.shouldExtract(tarArchiveEntry.getName(), tarArchiveEntry.isDirectory())) {
                archiveEntries.add(tarArchiveEntry);
                totalBytes += tarArchiveEntry.getSize();
            }// w w  w .  j  a v  a 2s.  c om
        } else {
            invalidArchiveEntries.add(tarArchiveEntry.getName());
        }
    }

    listener.onStart(totalBytes, archiveEntries.get(0).getName());

    inputStream.close();
    inputStream = new TarArchiveInputStream(new FileInputStream(filePath));

    for (TarArchiveEntry entry : archiveEntries) {
        if (!listener.isCancelled()) {
            listener.onUpdate(entry.getName());
            //TAR is sequential, you need to walk all the way to the file you want
            while (entry.hashCode() != inputStream.getNextTarEntry().hashCode())
                ;
            extractEntry(context, inputStream, entry, outputPath);
        }
    }
    inputStream.close();

    listener.onFinish();
}