Example usage for org.apache.commons.compress.archivers.tar TarConstants MAGIC_POSIX

List of usage examples for org.apache.commons.compress.archivers.tar TarConstants MAGIC_POSIX

Introduction

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

Prototype

String MAGIC_POSIX

To view the source code for org.apache.commons.compress.archivers.tar TarConstants MAGIC_POSIX.

Click Source Link

Document

The magic tag representing a POSIX tar archive.

Usage

From source file:org.jwifisd.eyefi.EyeFiServer.java

/**
 * eyefi sends the files tarred w will have to untar it first.
 * //from ww w.  j  av a  2  s. c  o  m
 * @param tarFile
 *            the tar file
 * @param out
 *            output stream to write the untarred contents.
 * @return true if successful
 * @throws Exception
 *             if the file could not be untarred.
 */
private boolean extractTarFile(File tarFile, OutputStream out) throws Exception {
    boolean result = false;
    byte[] tarBytes = IOUtils.toByteArray(new FileInputStream(tarFile));
    // This is stangege but posix was not correctly detected by the apache
    // lib, so we change some bytes so the detection will work.
    System.arraycopy(TarConstants.MAGIC_POSIX.getBytes("US-ASCII"), 0, tarBytes, TarConstants.MAGIC_OFFSET,
            TarConstants.MAGICLEN);

    TarArchiveInputStream tar = new TarArchiveInputStream(new ByteArrayInputStream(tarBytes));

    final byte[] buffer = new byte[BUFFER_SIZE];
    try {
        ArchiveEntry tarEntry = tar.getNextEntry();
        if (tarEntry != null) {
            int n = 0;
            while ((n = tar.read(buffer)) >= 0) {
                out.write(buffer, 0, n);
            }
            result = true;
        }
    } finally {
        tar.close();
        out.close();
    }
    return result;
}