Search in sources :

Example 16 with TarArchiveEntry

use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project nifi by apache.

the class FlowFileUnpackagerV1 method unpackageFlowFile.

@Override
public Map<String, String> unpackageFlowFile(final InputStream in, final OutputStream out) throws IOException {
    flowFilesRead++;
    final TarArchiveInputStream tarIn = new TarArchiveInputStream(in);
    final TarArchiveEntry attribEntry = tarIn.getNextTarEntry();
    if (attribEntry == null) {
        return null;
    }
    final Map<String, String> attributes;
    if (attribEntry.getName().equals(FlowFilePackagerV1.FILENAME_ATTRIBUTES)) {
        attributes = getAttributes(tarIn);
    } else {
        throw new IOException("Expected two tar entries: " + FlowFilePackagerV1.FILENAME_CONTENT + " and " + FlowFilePackagerV1.FILENAME_ATTRIBUTES);
    }
    final TarArchiveEntry contentEntry = tarIn.getNextTarEntry();
    if (contentEntry != null && contentEntry.getName().equals(FlowFilePackagerV1.FILENAME_CONTENT)) {
        // 512KB
        final byte[] buffer = new byte[512 << 10];
        int bytesRead = 0;
        while ((bytesRead = tarIn.read(buffer)) != -1) {
            // still more data to read
            if (bytesRead > 0) {
                out.write(buffer, 0, bytesRead);
            }
        }
        out.flush();
    } else {
        throw new IOException("Expected two tar entries: " + FlowFilePackagerV1.FILENAME_CONTENT + " and " + FlowFilePackagerV1.FILENAME_ATTRIBUTES);
    }
    return attributes;
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) IOException(java.io.IOException) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 17 with TarArchiveEntry

use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project coprhd-controller by CoprHD.

the class WorkflowHelper method makeArchiveEntry.

private static TarArchiveEntry makeArchiveEntry(final String name, final Date modTime, final byte[] bytes) throws IOException {
    final TarArchiveEntry entry = new TarArchiveEntry(name);
    entry.setSize(bytes.length);
    entry.setMode(0444);
    entry.setModTime(modTime);
    entry.setUserName("storageos");
    entry.setGroupName("storageos");
    return entry;
}
Also used : TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 18 with TarArchiveEntry

use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project incubator-systemml by apache.

the class ValidateLicAndNotice method extractFileFromTGZ.

/**
 * This will return the file from tgz file and store it in specified location.
 *
 * @param	tgzFileName is the name of tgz file from which file to be extracted.
 * @param	fileName is the name of the file to be extracted.
 * @param	strDestLoc is the location where file will be extracted.
 * @param 	bFirstDirLevel to indicate to get file from first directory level.
 * @return	Sucess or Failure
 */
public static boolean extractFileFromTGZ(String tgzFileName, String fileName, String strDestLoc, boolean bFirstDirLevel) {
    boolean bRetCode = Constants.bFAILURE;
    TarArchiveInputStream tarIn = null;
    try {
        tarIn = new TarArchiveInputStream(new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(tgzFileName))));
    } catch (Exception e) {
        Utility.debugPrint(Constants.DEBUG_ERROR, "Exception in unzipping tar file: " + e);
        return bRetCode;
    }
    try {
        BufferedOutputStream bufOut = null;
        BufferedInputStream bufIn = null;
        TarArchiveEntry tarEntry = null;
        while ((tarEntry = tarIn.getNextTarEntry()) != null) {
            if (!tarEntry.getName().endsWith(fileName))
                continue;
            // Get file at root (in single directory) level. This is for License in root location.
            if (bFirstDirLevel && (tarEntry.getName().indexOf('/') != tarEntry.getName().lastIndexOf('/')))
                continue;
            bufIn = new BufferedInputStream(tarIn);
            int count;
            byte[] data = new byte[Constants.BUFFER];
            String strOutFileName = strDestLoc == null ? tarEntry.getName() : strDestLoc + "/" + fileName;
            FileOutputStream fos = new FileOutputStream(strOutFileName);
            bufOut = new BufferedOutputStream(fos, Constants.BUFFER);
            while ((count = bufIn.read(data, 0, Constants.BUFFER)) != -1) {
                bufOut.write(data, 0, count);
            }
            bufOut.flush();
            bufOut.close();
            bufIn.close();
            bRetCode = Constants.bSUCCESS;
            break;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bRetCode;
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 19 with TarArchiveEntry

use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project incubator-systemml by apache.

the class ValidateLicAndNotice method getFilesFromTGZ.

/**
 * This will return the list of files from tgz file.
 *
 * @param	tgzFileName is the name of tgz file from which list of files with specified file extension will be returned.
 * @param	fileExt is the file extension to be used to get list of files to be returned.
 * @return	Returns list of files having specified extension from tgz file.
 */
public static List<String> getFilesFromTGZ(String tgzFileName, String fileExt) {
    TarArchiveInputStream tarIn = null;
    try {
        tarIn = new TarArchiveInputStream(new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(tgzFileName))));
    } catch (Exception e) {
        Utility.debugPrint(Constants.DEBUG_ERROR, "Exception in unzipping tar file: " + e);
        return null;
    }
    List<String> files = new ArrayList<String>();
    try {
        TarArchiveEntry tarEntry = null;
        while ((tarEntry = tarIn.getNextTarEntry()) != null) {
            if (tarEntry.getName().endsWith("." + fileExt)) {
                int iPos = tarEntry.getName().lastIndexOf("/");
                if (iPos == 0)
                    --iPos;
                String strFileName = tarEntry.getName().substring(iPos + 1);
                files.add(strFileName);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return (files);
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) BufferedInputStream(java.io.BufferedInputStream) ArrayList(java.util.ArrayList) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 20 with TarArchiveEntry

use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project gradle by gradle.

the class TarTaskOutputPacker method createTarEntry.

private static void createTarEntry(String path, long size, int mode, TarArchiveOutputStream tarOutput) throws IOException {
    TarArchiveEntry entry = new TarArchiveEntry(path, true);
    entry.setSize(size);
    entry.setMode(mode);
    tarOutput.putArchiveEntry(entry);
}
Also used : TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Aggregations

TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)200 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)99 File (java.io.File)86 FileInputStream (java.io.FileInputStream)55 IOException (java.io.IOException)55 FileOutputStream (java.io.FileOutputStream)45 GzipCompressorInputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream)38 InputStream (java.io.InputStream)31 BufferedInputStream (java.io.BufferedInputStream)29 TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)25 ByteArrayInputStream (java.io.ByteArrayInputStream)22 Path (java.nio.file.Path)21 BufferedOutputStream (java.io.BufferedOutputStream)20 ByteArrayOutputStream (java.io.ByteArrayOutputStream)20 Test (org.junit.Test)20 ArrayList (java.util.ArrayList)18 OutputStream (java.io.OutputStream)17 ArchiveStreamFactory (org.apache.commons.compress.archivers.ArchiveStreamFactory)15 HashMap (java.util.HashMap)11 GZIPInputStream (java.util.zip.GZIPInputStream)11