Example usage for org.apache.commons.compress.archivers ArchiveInputStream getBytesRead

List of usage examples for org.apache.commons.compress.archivers ArchiveInputStream getBytesRead

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers ArchiveInputStream getBytesRead.

Prototype

public long getBytesRead() 

Source Link

Document

Returns the current number of bytes read from this stream.

Usage

From source file:com.github.wnameless.linereader.ArchiveLineReader.java

private void setEntry(ArchiveInputStream archiveIS) throws IOException {
    if (archiveIS.getBytesRead() == 0)
        archiveIS.getNextEntry();/*from w w  w.j a v a  2 s  .  c o m*/
}

From source file:net.orpiske.ssps.common.archive.TarArchiveUtils.java

/**
 * Unpacks a file//w  ww .  jav a 2  s.c  o  m
 * @param source source file
 * @param destination destination directory. If the directory does not 
 * exists, it will be created
 * @param format archive format
 * @return the number of bytes processed
 * @throws SspsException
 * @throws ArchiveException
 * @throws IOException
 */
public static long unpack(File source, File destination, String format)
        throws SspsException, ArchiveException, IOException {

    if (!destination.exists()) {
        if (!destination.mkdirs()) {
            throw new IOException("Unable to create destination directory: " + destination.getPath());
        }
    } else {
        if (!destination.isDirectory()) {
            throw new SspsException(
                    "The provided destination " + destination.getPath() + " is not a directory");
        }
    }

    ArchiveStreamFactory factory = new ArchiveStreamFactory();

    FileInputStream inputFileStream = new FileInputStream(source);

    ArchiveInputStream archiveStream;
    try {
        archiveStream = factory.createArchiveInputStream(format, inputFileStream);
    } catch (ArchiveException e) {
        IOUtils.closeQuietly(inputFileStream);
        throw e;
    }

    OutputStream outStream = null;
    try {
        TarArchiveEntry entry = (TarArchiveEntry) archiveStream.getNextEntry();

        while (entry != null) {
            File outFile = new File(destination, entry.getName());

            if (entry.isDirectory()) {
                if (!outFile.exists()) {
                    if (!outFile.mkdirs()) {
                        throw new SspsException("Unable to create directory: " + outFile.getPath());
                    }
                } else {
                    logger.warn("Directory " + outFile.getPath() + " already exists. Ignoring ...");
                }
            } else {
                File parent = outFile.getParentFile();
                if (!parent.exists()) {
                    if (!parent.mkdirs()) {
                        throw new IOException("Unable to create parent directories " + parent.getPath());
                    }
                }

                outStream = new FileOutputStream(outFile);

                IOUtils.copy(archiveStream, outStream);
                outStream.close();
            }

            int mode = entry.getMode();

            PermissionsUtils.setPermissions(mode, outFile);

            entry = (TarArchiveEntry) archiveStream.getNextEntry();
        }

        inputFileStream.close();
        archiveStream.close();
    } finally {
        IOUtils.closeQuietly(outStream);
        IOUtils.closeQuietly(inputFileStream);
        IOUtils.closeQuietly(archiveStream);
    }

    return archiveStream.getBytesRead();
}