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

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

Introduction

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

Prototype

int MAGICLEN

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

Click Source Link

Document

The length of the magic field in a header buffer.

Usage

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

/**
 * eyefi sends the files tarred w will have to untar it first.
 * //from  ww  w  . ja  v  a  2 s .  co  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;
}